having trouble with if statement and string

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

Join Date: Oct 2008
Posts: 49
Reputation: mybluehair is an unknown quantity at this point 
Solved Threads: 0
mybluehair mybluehair is offline Offline
Light Poster

having trouble with if statement and string

 
0
  #1
Nov 14th, 2008
i have a code that you enter section after section. when your done, you type end, and it stops asking you to enter in stuff. My problem is, I can get it to detect when I type end.
here is my problem:

  1. cout << "\n\n\nTo start, enter each section line per line, by entering a section, hitting enter, then the next section, enter, and so on. when you done, type end.";
  2. cin >> stringa;
  3. end = stringc;
  4. if (end = end)
  5. {
  6. cout<< "ok, you are now finished entering #'s\n\n";
  7. }
  8. else
  9. {
  10. cout << "\nNext:\n";
  11. cin >> stringb;
  12. end = stringc;
  13. if (end = end)
  14. {
  15. cout<< "ok, you are now finished entering #'s\n\n";
  16. }
  17. else
  18. {
  19. cout << "\nNext:\n";
  20. cin >> stringc;
  21. end = stringc;
  22. if (end = end)
  23. {
  24. cout<< "ok, you are now finished entering #'s\n\n";
  25. }
  26. else
  27. {
  28. cout << "\nNext:\n";
  29. cin >> stringd;
  30. }

I see the problem here. it's saying that if the string "end" equals itself, then go into the if statement. but what I want to happen is if someone actually types end, and hits enter, I want it to be able to know. if they type anything else, then just keep going. how do i do that?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 29
Reputation: jbrock31 is an unknown quantity at this point 
Solved Threads: 1
jbrock31 jbrock31 is offline Offline
Light Poster

Re: having trouble with if statement and string

 
0
  #2
Nov 14th, 2008
Your IF statements are not testing for equality. You are assigning using just the single =.

Do this.

IF ( end == end )

blah, blah, blah
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: having trouble with if statement and string

 
0
  #3
Nov 14th, 2008
Is it a joke?
end == end is always true (if you don't overload operator ==, of course).
What's a sense of humor ...
Last edited by ArkM; Nov 14th, 2008 at 12:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 29
Reputation: jbrock31 is an unknown quantity at this point 
Solved Threads: 1
jbrock31 jbrock31 is offline Offline
Light Poster

Re: having trouble with if statement and string

 
0
  #4
Nov 14th, 2008
=P

Just pointing out the incorrect use of = in the IF. But yes you are correct of course.
Last edited by jbrock31; Nov 14th, 2008 at 12:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: having trouble with if statement and string

 
0
  #5
Nov 14th, 2008
  1. if (stringa == "end")
I think that's what you meant.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 49
Reputation: mybluehair is an unknown quantity at this point 
Solved Threads: 0
mybluehair mybluehair is offline Offline
Light Poster

Re: having trouble with if statement and string

 
0
  #6
Nov 14th, 2008
Originally Posted by skatamatic View Post
  1. if (stringa == "end")
I think that's what you meant.
thanks. the "" was what i was missing. I used the ==, but it didnt work, so thats why i didnt place that in the code i showed you guys.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: having trouble with if statement and string

 
0
  #7
Nov 14th, 2008
Originally Posted by mybluehair View Post
thanks. the "" was what i was missing. I used the ==, but it didnt work, so thats why i didnt place that in the code i showed you guys.
So basically, you where properly comparing an arbitrary word against itself?
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC