| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello everyone. I am new to C++ but I'm very interested in learning. I decided the best way to go is to start small with easy beginners' tutorials and work my way up.
I understand all of the basic commands, and I'm starting to understand the concept of arrays, but I'm stuck with character arrays. Anyway, this is my problem. I'm trying to get the user to be able to input a first name and last name, and then have them verify if it's correct. If it's correct, then it should go on to ask the user their last name. If it's incorrect, the code should start over and ask them their first name again. For some reason...no matter what the user enters, the code automatically moves on to ask them their last name, but it doesn't even do that. It seems like it's just skipping over all the code after my if/else/goto command. It shows all of my "cout" commands, but it does not allow the user to input anything. I'm pretty sure it's a problem with my 'if' statement. This is probably a very easy question, but I'm just a beginner, and I've already tried everything I know, which isn't much, but hey. Any help is appriciated.... Thanks. Code:
#include <iostream>
usingnamespace std;
void main(void)
{
char name[50];
char lastname[50];
char fullname[100];
char usernameyn = 'n';
Start:
cout<<"Please enter your first name: ";
cin.getline ( name, 50 );
cout << "You entered " << name << "." << endl;
cout << "Is this correct? (Y/N) ";
cin >> usernameyn;
{
if (usernameyn = 'Y')
goto Lastname;
else
{
if (usernameyn = 'N')
goto Start;
}
}
Lastname:
cout<<"Enter your last name: " << endl;
cin.getline ( lastname, 50 );
fullname[0] = '\0';
strcat ( fullname, name );
strcat ( fullname, " " );
strcat ( fullname, lastname );
cout<<"Your full name is "<< fullname <<"\n";
cin.get();
}
|
|
#2
|
||||
|
||||
|
Don't use goto's, it's a bad habit and makes life hell for anyone, including yourself, to read your code....
The proper usage of if() and while() statements should give you all the control of your program that you need. Simply rework your program's logic till you exclude the goto's, then try it.....
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#3
|
|||
|
|||
|
Like B-Con said you should never use the goto statement. However, looking at your code I can see that you are making a mistake within your 'if' statement. The correct syntax would be
Code:
if (usernameyn == 'y') This is a pretty common noob mistake. In your code you are actually setting the value of usernameyn to 'Y' instead of checking it. |
|
#4
|
|||
|
|||
|
Quote:
thank you very much. i'm not yet familiar enough with C++ to use if statements and while statements together. i do understand my problem though. i kind of have this phrase in my head so i dont get confused. "== means equals" and "= means is" so like "usernameyn='y' " that would mean that its telling it usernameyn IS y...instead of "if it equals y"...if that makes any sense. idk i understand though, so thank you soo much! |
|
#5
|
|||
|
|||
|
im starting to understand do-while statements. are those good to use?
|
|
#6
|
||||
|
||||
|
Quote:
They're not as common as normal while() loops, but they are perfectly valid and can be very helpful when you need to ensure that a given loop will execute before a condition is checked.... usually this can be accomplished with a while() loop by simply setting the condition to be something else before entering the loop, but it depends on your style preference.... My, I perfer while(), do-while is perfectly fine, however ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > noob question-goto command problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|