C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 24th, 2005, 08:08 PM
Titobandito Titobandito is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 3 Titobandito User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 30 sec
Reputation Power: 0
Question noob question-goto command problem

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();

}

  

Reply With Quote
  #2  
Old May 24th, 2005, 08:39 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
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.



Reply With Quote
  #3  
Old May 25th, 2005, 10:18 AM
ShadowCoder ShadowCoder is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: Ottawa
Posts: 20 ShadowCoder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 12 sec
Reputation Power: 0
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.
Comments on this post
B-Con agrees!

Reply With Quote
  #4  
Old May 27th, 2005, 01:43 AM
Titobandito Titobandito is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 3 Titobandito User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 30 sec
Reputation Power: 0
Quote:
Originally Posted by ShadowCoder
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.


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!

Reply With Quote
  #5  
Old May 27th, 2005, 01:51 PM
Titobandito Titobandito is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 3 Titobandito User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 30 sec
Reputation Power: 0
im starting to understand do-while statements. are those good to use?

Reply With Quote
  #6  
Old May 27th, 2005, 03:46 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
Quote:
Originally Posted by Titobandito
im starting to understand do-while statements. are those good to use?

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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > noob question-goto command problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT