943,891 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2065
  • C++ RSS
Oct 12th, 2006
0

C++ Baby steps

Expand Post »
Hi yall,

Absolute C++ newbie here.

I would like to know how to prompt a user to enter yes or no, and then display a different msg depending on whether y or n was pressed. I am trying to set char y=1 or true and char n=0 or false. But then I am stuck at how to compare the input variable to y and n. Right now, this bad code only makes the program display the "yes" message no matter what key is pressed. Can anyone please gimme some tips? Thank you very much.

#include <iostream>
usingnamespace std;
void main ()
{char y,
n,
x;
y=1;
n=0;

cout<<"\nPlease enter either y or n\n";
cin>>x;
if (x=1)
cout <<"you pressed yes\n";
else
cout <<"you pressed no\n";
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joey z is offline Offline
3 posts
since Oct 2006
Oct 12th, 2006
0

Re: C++ Baby steps

Please read this thread about how to post code inside code tags so that spaces and tabs are preserved. There are also other helpful threads for newbes at the top of this board that contain links to other useful information.

Thank you, and welcome to DaniWeb
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Oct 12th, 2006
0

Re: C++ Baby steps

Okay... Well... >.<

Seems like you just started learning a few days... maybe one or two? Unless you're learning by yourself. Heh. I'm no pro (I just started a few weeks ago) but if you're following a class, I reccommend learning by them until you feel comftorable enough to surpass what is being taught... Anyway...

do something like
char choice;
cout << "Please enter either Y or N.";
cin >> choice;
if (choice=='Y')
cout << "\nYou entered Yes.";
if (choice=="N')
cout << "\nYou entered No.";
return 0;
}
But then again, this is very basic, and if you want, you can go google up things like iostream/c++ guide... its best if you have a textbook to follow by, and then search the internet for when you can't find it from reference.
Last edited by Harkua; Oct 12th, 2006 at 10:34 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Harkua is offline Offline
3 posts
since Oct 2006
Oct 13th, 2006
0

Re: C++ Baby steps

I am taking a university C++ class, but this is my very first attempt to write a program on my own.

Thanks again. That makes sense. I didn't realize I could set the IF condition to make the input equal to a char. Thought it had to be an int value.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joey z is offline Offline
3 posts
since Oct 2006
Oct 13th, 2006
0

Re: C++ Baby steps

If conditions can be any valid boolean expression (IE one which evaluates as either TRUE or FALSE).
Fixed up the code a bit, and added a third option:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. int main (){
  4. char y,
  5. n,
  6. x;
  7. y=1;
  8. n=0;
  9.  
  10. char choice;
  11. cout << "Please enter either Y or N.";
  12. cin >> choice;
  13. if (choice=='Y')
  14. cout << "\nYou entered Yes.";
  15. else if (choice=='N')
  16. cout << "\nYou entered No.";
  17. else
  18. cout << "\nYou did not enter a valid character.";
  19. return 0;
  20. }
Reputation Points: 22
Solved Threads: 11
Posting Whiz in Training
DavidRyan is offline Offline
229 posts
since Jul 2006
Oct 13th, 2006
0

Re: C++ Baby steps

Click to Expand / Collapse  Quote originally posted by joey z ...
I am taking a university C++ class, but this is my very first attempt to write a program on my own.

Thanks again. That makes sense. I didn't realize I could set the IF condition to make the input equal to a char. Thought it had to be an int value.
In C and C++ the char data type is an int -- it is a one-byte integer. There really is no such thing as a true character data type, the characters you see on the screen are represented internally as integers, for example the letter 'A' has a numeric value of 65. You can find out the values of all of them in an ascii chart..
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Oct 14th, 2006
0

Re: C++ Baby steps

Another question. Is it possible to put multiple conditional statements in a WHILE loop? For example, if the user enters anything but n, N, y, or Y, I want the program to prompt him to re-type his choice until he enters one of those 4 values, how can I do so using just one loop?

I tried:
C++ Syntax (Toggle Plain Text)
  1. while (choice!='y' || choice!='Y' || choice!='n' || choice!='N')
  2. {
  3. cout << "Try again\n";
  4.  
  5. cin >> choice;
  6. }
This clearly doesn't work.

I know it's a laughable mistake, but how can I fix it?

Thank you again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joey z is offline Offline
3 posts
since Oct 2006
Oct 14th, 2006
0

Re: C++ Baby steps

Something like
C++ Syntax (Toggle Plain Text)
  1. int main( void )
  2. {
  3. char choice[2] = {'\0'} ;
  4. do
  5. {
  6. fgets( choice, 2, stdin ) ;
  7. choice[0] = tolower(choice[0]) ;
  8. }
  9. while ( choice[0] != 'y' && choice[0] != 'n' ) ;
  10.  
  11. return 0 ;
  12. }
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem with execv argument passing
Next Thread in C++ Forum Timeline: serial port programming between two sytems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC