C++ Baby steps

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 3
Reputation: joey z is an unknown quantity at this point 
Solved Threads: 0
joey z joey z is offline Offline
Newbie Poster

C++ Baby steps

 
0
  #1
Oct 12th, 2006
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";
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Baby steps

 
0
  #2
Oct 12th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: Harkua is an unknown quantity at this point 
Solved Threads: 0
Harkua Harkua is offline Offline
Newbie Poster

Re: C++ Baby steps

 
0
  #3
Oct 12th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: joey z is an unknown quantity at this point 
Solved Threads: 0
joey z joey z is offline Offline
Newbie Poster

Re: C++ Baby steps

 
0
  #4
Oct 13th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 229
Reputation: DavidRyan is an unknown quantity at this point 
Solved Threads: 11
DavidRyan's Avatar
DavidRyan DavidRyan is offline Offline
Posting Whiz in Training

Re: C++ Baby steps

 
0
  #5
Oct 13th, 2006
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:
  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. }
Please anyone, correct me if I am wrong. It is the best way for me to learn.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Baby steps

 
0
  #6
Oct 13th, 2006
Originally Posted by joey z View Post
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..
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: joey z is an unknown quantity at this point 
Solved Threads: 0
joey z joey z is offline Offline
Newbie Poster

Re: C++ Baby steps

 
0
  #7
Oct 14th, 2006
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,610
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 465
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: C++ Baby steps

 
0
  #8
Oct 14th, 2006
Something like
  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. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC