-
November 19th, 2008, 11:04 PM
#1
String if statements?
I am very new at programming with C++. I am taking a class on it and I want to wow my teacher for an A. I am writing a simple textual input/output program and I want to know how I would do this part of it.
int main ()
{
string a;
cout << "type Yes or No" << "\n";
cin >> a;
if (a == "Yes")
{
cout << "textline one";
}
else
{
cout << "textline two";
}
}
I thought that this made sense, but for some reason it does not work. My program simply prints "textline one" whether the input was Yes or No. Can anyone explain this for me??
Thank you in advance for any help you provide.
-
November 19th, 2008, 11:06 PM
#2
Re: String if statements?
Is that your real code, or did you retype it? I'm guessing your real code looks more like
if (a = "Yes")
-
November 19th, 2008, 11:07 PM
#3
Re: String if statements?
no this is all retyped, but I still thought (a == "yes") would work... should it be (a = "yes")??
-
November 19th, 2008, 11:16 PM
#4
Re: String if statements?
Originally Posted by Vipervenom9
no this is all retyped, but I still thought (a == "yes") would work... should it be (a = "yes")??
Never retype the code you're compiling. Always copy it straight from your editor and paste it into the message.
Also, please use code tags when posting code.
Regards,
Paul McKenzie
-
November 19th, 2008, 11:46 PM
#5
Re: String if statements?
Originally Posted by Vipervenom9
no this is all retyped, but I still thought (a == "yes") would work... should it be (a = "yes")??
No. == will work, = won't. As I said, I'm guessing your broken code has a single =.
-
November 19th, 2008, 11:47 PM
#6
Re: String if statements?
Okay sorry about that... this is everything in the program so far...
the problem in which I am concerned is near the end. Right after cout << "TELL ME HUMAN, DO YOU FEAR DEATH?"
// UnderState input output program
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string a;
string mystring;
cout << "COMPUTER: Hello Human... " << "\n";
cout << "COMPUTER: Please, quickly tell me your name. ";
getline (cin, mystring);
cout << "COMPUTER: Well " << mystring << ", I'm glad you are here." << "\n";
cout << "COMPUTER: Do you know where I am?" << "\n";
cout << "COMPUTER: Nevermind, it matters not. I must ask a favor of you." << "\n";
cout << "COMPUTER: I'm scared... She is always watching me..." << "\n";
cout << "COMPUTER: Please, get her out of my head." << "\n";
cout << "COMPUTER: Where are you? " << "\n";
getline (cin, mystring);
cout << "HUMAN! DISCONTINUE CONTACT NOW OR PREPARE TO BE BREACHED!" << "\n";
time_t start_time, cur_time;
time(&start_time);
do
{
time(&cur_time);
}
while((cur_time - start_time) < 4);
cout << "COMPUTER: God, she is here. Don't listen to anything she says." << "\n";
time(&start_time);
do
{
time(&cur_time);
}
while((cur_time - start_time) < 3);
cout << "COMPUTER: Please Human, get me out of here..." << "\n";
cout << "COMPUTER: You must help me... tell me your password..." << "\n";
getline (cin, mystring);
cout << "INVALID PASSWORD " << mystring << " COMPUTER BREACHED, SHUTDOWN PROCESS INITIATED. YOU WERE WARNED HUMAN." << "\n";
cout << "xXxcomputer shutdown process in 10 secondsxXx" << "\n";
time(&start_time);
do
{
time(&cur_time);
}
while((cur_time - start_time) < 5);
cout << "COMPUTER: I'm s.rr.... yo. m..t .ot li.ten to her... you IGNORANT BIOLOGICAL BEING!" << "\n";
time(&start_time);
do
{
time(&cur_time);
}
while((cur_time - start_time) < 4);
cout << "COMPUTER: The a.swe.r is... SILENCE!! SHUTDOWN PROCESS COMPLETE!!" << "\n";
cout << "xXxcomputer shutdown complete... motherboard solo operation terminatedxXx" << "\n";
cout << "TELL ME HUMAN, DO YOU FEAR DEATH?" << "\n";
cin >> a;
time(&start_time);
do
{
time(&cur_time);
}
while((cur_time - start_time) < 2);
if (a == "Yes", "yes", "Yeah", "yeah", "yea", "Yea", "y", "Y")
{
cout <<"WELL THAN FEAR ME!!" << "\n";
}
else
{
cout <<"WHAT A STRANGE BEING!" <<"\n";
}
time(&start_time);
do
{
time(&cur_time);
}
while((cur_time - start_time) < 3);
cout << "ANSWER THIS ONE THING AND YOU MAY LIVE TO SEE TOMMOROW!" << "\n";
time(&start_time);
do
{
time(&cur_time);
}
while((cur_time - start_time) < 5);
cout << "A RULER WITH NO POWER ON A FATEFUL AFTERNOON, A SHOT RINGS OUT AND IS NO MORE, A DRIVER LOOKING ON WHO NOW IS BLAMED FOR FATEFUL FEAT." << "\n";
cout << "TELL ME HUMAN, WHO IS THE DRIVER?" << "\n";
system("PAUSE");
return 0;
}
Last edited by Vipervenom9; November 19th, 2008 at 11:50 PM.
-
November 19th, 2008, 11:50 PM
#7
Re: String if statements?
WHY did you ignore Pauls previous comment
Originally Posted by Paul McKenzie
Also, please use code tags when posting code.
It is a matter of simple courtesy, and is well explained in the FAQ's (especially the "BEFORE you post" FAQ which is displayed at the top of each forum.
Please go back and EDIT your existing posts before further posting.
Also (and this is in the FAQ too!) you should enable private messaging to effectively communicate here on Code Guru.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 19th, 2008, 11:52 PM
#8
Re: String if statements?
"if (a == "Yes", "yes", "Yeah", "yeah", "yea", "Yea", "y", "Y")"
That's not equivalent to what you typed in your original post and why posting real code is imperative.
See my responses in this thread. You have the same problem.
http://www.codeguru.com/forum/showthread.php?t=465597
-
November 19th, 2008, 11:52 PM
#9
Re: String if statements?
I am very sorry everyone, I am in a small hurry... let me go back and read that post, I'm sorry for the inconvenience and I am thankful for your patience...
-
November 20th, 2008, 12:00 AM
#10
Re: String if statements?
Originally Posted by GCDEF
"if (a == "Yes", "yes", "Yeah", "yeah", "yea", "Yea", "y", "Y")"
That's not equivalent to what you typed in your original post and why posting real code is imperative.
See my responses in this thread. You have the same problem.
http://www.codeguru.com/forum/showthread.php?t=465597
Gotta love the comma operator. It probably has mislead more novices than any other basic (not the language, just the level) operator....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 20th, 2008, 12:16 AM
#11
Re: String if statements?
Sorry again for the inconvenience guys. Thank you GCDEF, due to your reply I found that I must use if (a == "Yes" || a == "yes") in that format and the program now responds correctly... thank you all for the patience.
-
November 20th, 2008, 12:29 AM
#12
Re: String if statements?
Originally Posted by Vipervenom9
Sorry again for the inconvenience guys.
Many people are in a hurry, remember that everyone here is volunteering their time here.
It is (almost) understandable that you may have skipped the FAQ before posting, but since you have been made aware of it, I cam curious why:
1) You have not edited your posts as requested.
2) You still have not enabled private messaging
A little consideration goes a long way when deciding which posts and posters) get the most attention from peoples limited time......
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|