![]() |
| | #1 |
| Registered User Join Date: Sep 2009
Posts: 12
| Here's the code so far: Code: #include <cstdlib>
#include <iostream>
using namespace std;
int main()
{string prima, secunda, tertia;
cout << "How many were going to St. Ives?\n";
cin >> prima;
while (prima !="Help" || prima!="One") {
cout <<"Sorry, that's not the right answer. Would you like some help?\n";
cin >> prima;}
while (prima=="Help") {
cout << "Believe me when I say you don't need to do the math.\n";
cin >> prima;}
if (prima=="One") {
cout <<"Nice one! Now lets move on to question two, shall we?\n";}
system("PAUSE");
return EXIT_SUCCESS;
}
1. There seems to be something wrong with Code: while ((prima !="Help") || (prima!= "One")) {
2. Is there any way to re-initialize a while loop after it's been executed? Right now, if a user inputs one (or many) incorrect responses, then inputs "Help", and after reading the hint, enters another wrong answer, the program ends. I'd like to make it so that the "Sorry, that's the right answer..." loop occurs whenever a user supplies an incorrect response, even if the loop has occurred and ended before. Thanks so much in advance for your help; I really appreciate this, and hopefully it'll be a step on the way to becoming a fully-fledged programmer one day. Last edited by Angie; 09-12-2009 at 01:27 PM. Reason: Minor Syntax Errors |
| Angie is offline | |
| | #2 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| On the right half of the OR, you need to compare prima to "One".
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #3 |
| Registered User Join Date: Sep 2009
Posts: 12
| I've edited it slightly (reflected in the post), referencing prima to both responses , but when I run it, the program won't respond to either input. It just prints the "Sorry..." message regardless of what I enter. Am I using incorrect syntax? Sorry if I'm missing something obvious. |
| Angie is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| prima != "Help" || prima != "One" is always true. |
| tabstop is offline | |
| | #5 |
| Registered User Join Date: Sep 2009
Posts: 12
| Wait, that's a logic error, isn't it? Because what I'm saying is that even if the string value input into the prima variable is one of the accepted inputs, the while loop will still initiate because it's not the other one, right? "Help" will be rejected because it isn't "One" and vice versa. *facepalm* I'm guessing that means I need to use a different operator. I just substitued '&&' for '||' and the program's working fine, now. Thank you both a hell of a lot. [edit]Does anyone have any ideas in regards to making while loops re-occur (part 2)? I'm going to experiment for a few days, but if I can't devise something within the next week or so, is it acceptable to make another topic focusing solely on this issue? If not, would it be okay to bump this thread and edit my initial post?[/edit] Last edited by Angie; 09-12-2009 at 04:36 PM. Reason: Query about re-asking questions |
| Angie is offline | |
| | #6 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| To your help, you also have the keywords break (breaks a loop) and continue (jumps to the beginning of a loop). You will not be able to edit your first post around tomorrow or so, so a new topic or just a new reply should do. I would prefer a new thread, though, since it's easier to organize and for people to search and find it (archiving purposes).
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #7 | |
| Registered User Join Date: Sep 2009
Posts: 63
| Quote:
Code: while (prima != "One")
{
tellUserHesWrong();
askUserIfWantHelp();
if (user wants help)
{
giveHelp();
}
askForInput();
}
It might also be a good thing to check for alternative inputs. I could write "one," and your program would tell me I'm wrong. I could also write "1" and still be wrong. | |
| Zach_the_Lizard is offline | |
| | #8 |
| Registered User Join Date: Sep 2009
Posts: 12
| (Apologies for the slow reply, hope I didn't offend anyone) Thanks to your suggestions, I've made a lot of progress with this program, and I'm now on to things like editing the text, adding alternative inputs, and doing as many different things as possible in order to spot any dead-ends. |
| Angie is offline | |
![]() |
| Tags |
| operators, strings, while loops |
| Thread Tools | |
| Display Modes | |
|