Thread: String Help

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Exclamation String Help

    Hello, I am working on a simple text-based game, and I need some help with strings. My code compiles, but the name and age get mixed up. Look at bottom of post. Here is my code:

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    
       int main()
        {
        char name[50];
        char age[4];
        int answer;
        int response;
        cout << "----------------------Welcome to Biotech: Visitor Registry----------------------" << endl << endl;
        cout << "Secretary: We need your fullname and age to let you in" << endl << endl;
        cout << "Secretarty: Go to that computer over there" << endl << endl;
        cout << "Name:";
        cin.getline(name,50);
        cout << "Age:";
        cin.getline(age,4);
        system("PAUSE");
        system("CLS");
        cout << "Name:" << name << endl; 
        cout << "Age:" << age << endl;;
        cout << "Is this information correct?" << endl << endl;
        cout << "1.Yes" << endl << endl;
        cout << "2.No" << endl;   
        cin >> response;
        switch(response){
                         case 1:
                              system("CLS");
                              cout << "Visitor ID:34528" << endl;
                              system("PAUSE");
                              //go to start
                              break;
                         case 2:
                              system("CLS");
                              cout << "You have requested that your information is incorrect" << endl;
                              system("PAUSE");
                              system("CLS");
                              main();
                              break;
                         default:
                                 system("CLS");
                                 cout << "ERROR:INCORRECT INPUT" << endl << endl;
                                 system("PAUSE");
                                 return 0;
                                 break;
                                 }
                                 }
    My problem is when it asks you if the info is correct,and you say no, it takes you back to the registry screen(which is good), but the name and age are mixed up. How can I fix that?
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You are mixing the >> operator and the getline method with getline using the default newline terminating char, so when you go back to the top of the program by calling main() (bad choice, if even legal, to recursively call main() by the way), the terminating newline char from the call to cin >> is still in the input stream, thereby causing the initial call to getline()--to get name-- to terminate before data entered into name, but the initial call does remove the implicated newline char from the stream so the data that should have been put into name ends up in age. You need to clear the stream of the terminating char by calling ignore() if kyou call getline after calling >> and you use the default terminating newline char for getline(). The exact syntax of ignore() you use is up to you.
    You're only born perfect.

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    OK. I got it. I put a cin.ignore(); after the cin >> response; and I deleted int answer; I dont know why I put that there. Thanks.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Calling main like that is not legal. Move almost all your code into another function, then call that function in a while loop in main. A simple way to get it to repeat is to return true if you want to continue and false if you want to exit. Or you can return an int (like the response from the user) and break out of the loop based on the int.

  5. #5
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    I understand what you are saying. But I don't know how to do it. Specifically, how would I move my code into a function other than main. I did it once before, but I can't remember. Now everytime I do it, I come up with a bunch of errors.An example would be nice.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Step 1: Change the word main to whatever you want to call your function. I'll call it getInfo. So where it says int main(), change it to int getInfo(). Then, where you call main from inside the switch statement, change the main() to getInfo().

    Step 2: Add a return value to your function. Because main is special you didn't need a return value at the end of it, but other functions require a return value if the return type is not void, so add a return 0; to the last line of the function (before the last closing brace).

    Now you have a function that does all the work.

    Step 3: Create a new main function. I would put it at the bottom of the file (so the compiler recognizes the getInfo function when you use it). Just type in an empty main:
    Code:
    int main()
    {
    }
    You can compile this and it should succeed.

    Step 4: Call your function in main. This is easy. Just add getInfo(); inside the main function.

    Now you have the same program, only it doesn't illegally call main recursively. The next step is to use the while loop in main to call the function, and change the return value of your getInfo function to have some meaning (like whether the loop should continue or not). Play around with this to see if you can get it to work based on how you want the program to run.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
     
    bool foo();
     
    int main()
    	{
    	bool more = true;
    	while(more)
    	 more = foo();
    	cout << "thank you. bye" << endl;
    	}
     
    bool foo()
    {
    	bool result = true;
    	char name[50];
    	char age[4];
    	int answer;
    	int response;
    	cout << "----------------------Welcome to Biotech: Visitor Registry----------------------" << endl << endl;
    	cout << "Secretary: We need your fullname and age to let you in" << endl << endl;
    	cout << "Secretarty: Go to that computer over there" << endl << endl;
    	cout << "Name:";
    	cin.getline(name,50);
    	cout << "Age:";
    	cin.getline(age,4);
    	system("PAUSE");
    	system("CLS");
    	cout << "Name:" << name << endl; 
    	cout << "Age:" << age << endl;;
    	cout << "Is this information correct?" << endl << endl;
    	cout << "1.Yes" << endl << endl;
    	cout << "2.No" << endl; 
    	cin >> response;
    	switch(response){
    					 case 1:
    						 system("CLS");
    						 cout << "Visitor ID:34528" << endl;
    						 system("PAUSE");
    						 break;
    					 case 2:
    						 system("CLS");
    						 cout << "You have requested that your information is incorrect" << endl;
    						 system("PAUSE");
    						 system("CLS");						
    					 default:
    							 system("CLS");
    							 cout << "ERROR:INCORRECT INPUT" << endl << endl;
    							 system("PAUSE");
    							 result = false;							 break;
    							 }
    return result;
    }
    You're only born perfect.

  8. #8
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Thanks Daved & elad. I am more comfortable with int, so I am going to use Daved's idea. The bool thing worked, but I am not to comfortable with it.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Don't forget to go back and learn bool when you have a chance. It is simple and clearer to use when you just need a true or false variable.

  10. #10
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    I keep getting linker errors. It says undefined reference to SDL main. I am not sure where to put main(), I put it before return 0; but after the last switch statement. Is this right?
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Your program, and all programs in C/C++, can have only a single main(). Look at how I set up the program I posted. There is only one main() function and only a single call to that function. You should never try to call main() from within itself or from within another function. If you feel the need to do so, think of loops, or, in the extreme where nothing else will do, (very rare), you could use goto. But for us non-gurus, loops, loops, loops......
    You're only born perfect.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    #include stuff
    
    int getInfo()
    {
      // code
      return 0;
    }
    
    int main()
    {
      getInfo();
    }
    Or do it like elad showed. When you get it working try to understand why your original way didn't work.

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Daved's pseudocode calls the getInfo() just once and only once. He also ignores the return value of getInfo(). And the return value of getInfo() is hardcoded--there is no chance it will be anything other than 0. All of that is fine and dandy if that's what you want to do. However, he could have written the pseudocode such that any int could be returned. Then he could have looked at the return value back in main() and done something with it. For example, if the return value was 0 he could exit a loop in main(), and if the return value was 1 he could have continued with a loop in main(). This would have been equivalent to my program where 0 equated to false and 1 to true, which, by the way, is what bool variables are; false equals zero and true equals one. On the other hand if the return value had been a -1, then he could have had you play a game of Tic-Tac-Toe before getting you back into your original loop, or if the return value was 3315 you could have been asked to enter the number of words in the text of the Gettysburg Address before proceeding to the next iteration of the loop in main, etc.


    Bottom line, loops and functions in combination are very powerful programming tools. You will be using them in just about every program you write and will find them in just about every program you look at.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM