Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,376 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,625 people online right now. Registration is fast and FREE... Join Now!




C-String Vs. String

 
Reply to this topicStart new topic

C-String Vs. String

captainhampton
23 Jan, 2008 - 09:46 AM
Post #1

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
Ok, so I did this assignment using strictly STRINGS, however....unfortunately upon re-reading what I must do, I need to change the String to a C-string. I have already poured a good amount of effort and time finishing this program

In short my question is this: Is there any easy way to take my code below, and transpose all of the string elements to c-strings without drastically changing the program? And if so, how would I go about it? As always any and all help is greatly appreciated

Here is what I have:

CODE

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <string>

using namespace std;
int main(int argc, char *argv[])
{
    int pick_word1, pick_word2, x;
    string word, scrambled_word, attempt;
    int num_attempts = 1;
  
     srand ( time(NULL) );                                                      //Seed random number generator
     pick_word1 = rand() % 3;
     pick_word2 = rand() % 3;
  
    char word_array[3][3] ={                                                  //Word 2-D Array
           { "Red", "Green", "Black"},
           { "Blue", "Yellow", "Pink"},
           { "Orange", "Purple", "White"},
           };
          
    char word_hint[3][3] = {                                                  //Hint 2-D Array
           { "BLANK Riding Hood", "Color of grass", "The night sky"},
           { "Color of the sky", "Color of Big Bird", "Color for girls"},
           { "Color of basketball", "The band Deep BLANK", "The color of nothing"},
           };
      
        word = word_array[pick_word1][pick_word2];                              //Randomly selects word from 2-D Array & Stores in char var
        scrambled_word = word_array[pick_word1][pick_word2];
        x = scrambled_word.length();
      
        for ( int y = x; y > 0; y-- ){                                          //Swaping loop
            swap( scrambled_word[rand()%x],scrambled_word[y-1] );
            }//end for
      
        do{
        cout << "The word that you must unscramble is: " << scrambled_word << endl;
        cout << "Go ahead and enter the word, type exit to leave, or hint for a Hint"<<endl;
        cin >> attempt;
      
        if ( attempt == word ){
             cout << "Congratulations! You got it correct!"<<endl;
             cout << "It took you " << num_attempts << " tries"<<endl;
             break;
             }//end if
            
        if ( attempt != word && attempt !="hint" ){
             cout << "Nope, try again" <<endl <<endl;
             num_attempts++;
             }//end if
            
        if ( attempt == "exit" ){
             cout << "Thanks for playing!"<<endl;
             break;
             }//end if
            
        if ( attempt == "hint" ){
             cout << word_hint[pick_word1][pick_word2]<<endl <<endl;
             }//end if
      
        }while ( attempt != word );
      
              

    system("PAUSE");
    return EXIT_SUCCESS;
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: C-String Vs. String
23 Jan, 2008 - 10:29 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,654



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Please stand by while processing commences.....

....

....

Processing complete!

CODE

#include <cstdlib>
#include <iostream>
#include <ctime>
//#include <string> // Hey hey, gooooodbyeeeee

using namespace std;
int main(int argc, char *argv[])
{
    int pick_word1, pick_word2, x;

    // Convert to C-strings
    char word[20], scrambled_word[20], attempt[20];
    int num_attempts = 1;
  
    srand ( (int)time(NULL) );                                                      //Seed random number generator
    pick_word1 = rand() % 3;
    pick_word2 = rand() % 3;
  
    // Create an array of string pointers
    char *word_array[3][3] ={                                                  //Word 2-D Array
           { "Red", "Green", "Black"},
           { "Blue", "Yellow", "Pink"},
           { "Orange", "Purple", "White"}
           };

    // Create an array of string pointers to hints  
    char *word_hint[3][3] = {                                                  //Hint 2-D Array
           { "BLANK Riding Hood", "Color of grass", "The night sky"},
           { "Color of the sky", "Color of Big Bird", "Color for girls"},
           { "Color of basketball", "The band Deep BLANK", "The color of nothing"}
           };

    // Copy the word and scrambled word via C-string copy
    strcpy(word,word_array[pick_word1][pick_word2]);                              //Randomly selects word from 2-D Array & Stores in char var
    strcpy(scrambled_word,word_array[pick_word1][pick_word2]);

    // Not in Kansas anymore Toto... string.length is out, strlen is in!
    x = (int) strlen(scrambled_word);
  
    for ( int y = x; y > 0; y-- ){                                          //Swaping loop
        swap( scrambled_word[rand()%x],scrambled_word[y-1] );
        }//end for
  
    do {
        cout << "The word that you must unscramble is: " << scrambled_word << endl;
        cout << "Go ahead and enter the word, type exit to leave, or hint for a Hint"<<endl;
        cin >> attempt;
          
        // Strcmp is where it is at for comparing C-strings
        if ( strcmp(attempt,word) == 0 ){
             cout << "Congratulations! You got it correct!"<<endl;
             cout << "It took you " << num_attempts << " tries"<<endl;
             break;
             }//end if
            
        if ( strcmp(attempt,word) != 0 && strcmp(attempt,"hint") != 0 ){
             cout << "Nope, try again" <<endl <<endl;
             num_attempts++;
             }//end if
            
        if ( strcmp(attempt,"exit") == 0 ){
             cout << "Thanks for playing!"<<endl;
             break;
             }//end if
            
        if ( strcmp(attempt,"hint") == 0 ){
             cout << word_hint[pick_word1][pick_word2]<<endl <<endl;
             }//end if
  
    }while ( strcmp(attempt,word) != 0 );
      
              

    system("PAUSE");
    return EXIT_SUCCESS;
}


Next time you have a problem that keeps you up at night, call on your experts at DIC to solve the toughest problems. We will have an expert on it same day, accurate quotes, superb job and great service! We work weekdays, weekends, even during the holidays. You can't beat the incredible service! Now remember, *music* Whennnnn you need a geek, and you can't sleeeeeep, don't be a noobie... Call 1....800... ASK .... D...I...C...C! *music*

icon_up.gif
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: C-String Vs. String
23 Jan, 2008 - 10:44 AM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(Martyr2 @ 23 Jan, 2008 - 11:29 AM) *

Next time you have a problem that keeps you up at night, call on your experts at DIC to solve the toughest problems. We will have an expert on it same day, accurate quotes, superb job and great service! We work weekdays, weekends, even during the holidays. You can't beat the incredible service! Now remember, *music* Whennnnn you need a geek, and you can't sleeeeeep, don't be a noobie... Call 1....800... ASK .... D...I...C...C! *music*

icon_up.gif

LMAO
this one is evergreen now!
User is offlineProfile CardPM
+Quote Post

captainhampton
RE: C-String Vs. String
23 Jan, 2008 - 10:53 AM
Post #4

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
QUOTE(Martyr2 @ 23 Jan, 2008 - 11:29 AM) *

Please stand by while processing commences.....

....

....

Processing complete!

Next time you have a problem that keeps you up at night, call on your experts at DIC to solve the toughest problems. We will have an expert on it same day, accurate quotes, superb job and great service! We work weekdays, weekends, even during the holidays. You can't beat the incredible service! Now remember, *music* Whennnnn you need a geek, and you can't sleeeeeep, don't be a noobie... Call 1....800... ASK .... D...I...C...C! *music*

icon_up.gif


Thank you so much Martyr2, and if I do say so myself that is quite the commercial jingle.

A potential plot if you will for the commercial:

Imagine some kid who is hopelessly lost on his code, cursing and yelling when all of a sudden a blazing guitar solo starts and excessive explosions follow. Then the camera pans to a shot of Gary Buesy who is totally pissed off and yelling furiously at everyone about absolutley nothing.

Camera pans back to the kid and 5 DIC members who assemble themselves into a mechanical dinosaur of sorts. Finally, Jeff Goldbloom gives a speech on Chaos Theory.

End Result: Happy kid, finished code

Proposed Budget: $80,000,000

Let me know what you guys think!
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: C-String Vs. String
23 Jan, 2008 - 11:02 AM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,654



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
QUOTE(captainhampton @ 23 Jan, 2008 - 10:53 AM) *


Thank you so much Martyr2, and if I do say so myself that is quite the commercial jingle.

A potential plot if you will for the commercial:

Imagine some kid who is hopelessly lost on his code, cursing and yelling when all of a sudden a blazing guitar solo starts and excessive explosions follow. Then the camera pans to a shot of Gary Buesy who is totally pissed off and yelling furiously at everyone about absolutley nothing.

Camera pans back to the kid and 5 DIC members who assemble themselves into a mechanical dinosaur of sorts. Finally, Jeff Goldbloom gives a speech on Chaos Theory.

End Result: Happy kid, finished code

Proposed Budget: $80,000,000

Let me know what you guys think!


Commercial idea has now been patented! Skyhawk, sloth, Psycho, Capty, and Amadeus have been cast in the role of the mechanical dinosaur. Jeff Goldbloom wasn't available, so I have casted myself as the speech giver, Penny is casted as Gary Buesy and special guest Axel is making a cameo by playing "the hopelessly lost kid".

Thanks for the idea! Look for it to be shown on all the cool channels... Food network, Women's network, and TLC. Now if those other guys... (ABC, NBC, CBS and Fox) want a piece of our kick ass commercial, they are going to have to pay!

biggrin.gif

User is offlineProfile CardPM
+Quote Post

captainhampton
RE: C-String Vs. String
23 Jan, 2008 - 11:07 AM
Post #6

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions

[/quote]

Commercial idea has now been patented! Skyhawk, sloth, Psycho, Capty, and Amadeus have been cast in the role of the mechanical dinosaur. Jeff Goldbloom wasn't available, so I have casted myself as the speech giver, Penny is casted as Gary Buesy and special guest Axel is making a cameo by playing "the hopelessly lost kid".

Thanks for the idea! Look for it to be shown on all the cool channels... Food network, Women's network, and TLC. Now if those other guys... (ABC, NBC, CBS and Fox) want a piece of our kick ass commercial, they are going to have to pay!

biggrin.gif
[/quote]

Hahaha DIC FTW! If you're gonna put it on Food Network, see if Alton Brown from Good Eats wants to be cast as the rival dinosaur. Thanks again for all of your help Martyr2!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:10AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month