-
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?
-
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.
-
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.
-
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.
-
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.
-
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: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.
-
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;
}
-
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.
-
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.
-
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?
-
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......
-
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.
-
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.