C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-12-2009, 12:52 PM   #1
Registered User
 
Join Date: Sep 2009
Posts: 12
Unhappy Problem with strings and while loops

I'm fairly new to C++, and as a learning exercise I'm making a simple 'quiz game' in which the user needs to input the correct answer in order to proceed to the next question. They can also input 'Help', which prompts the program to print a small hint.

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;

}
I've got two main problems at the moment:

1. There seems to be something wrong with
Code:
while ((prima !="Help") || (prima!= "One")) {
I've tried writing it out in a number of different ways, substituting the '||" for 'or', or putting 'prima !=' before both conditions, but the program only ever acknowledges the first condition (ie. That the 'help' command wasn't entered). If I enter 'One', I get the same response loop as if I'd entered any other string value that isn't 'Help'. Would someone mind correcting me so that the program will respond to both conditions?

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   Reply With Quote
Old 09-12-2009, 12:58 PM   #2
Jack of many languages
 
Dino's Avatar
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 2,070
On the right half of the OR, you need to compare prima to "One".
__________________
Mac and Windows cross platform programmer. Ruby lover.
Dino is offline   Reply With Quote
Old 09-12-2009, 01:32 PM   #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   Reply With Quote
Old 09-12-2009, 01:32 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,805
prima != "Help" || prima != "One" is always true.
tabstop is offline   Reply With Quote
Old 09-12-2009, 01:42 PM   #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   Reply With Quote
Old 09-12-2009, 05:26 PM   #6
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,781
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 09-12-2009, 05:27 PM   #7
Registered User
 
Join Date: Sep 2009
Posts: 63
Quote:
Originally Posted by Angie View Post
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.
What I would do for your situation is change the initial while loop, to something like this (in pseudocode form):

Code:
    while (prima != "One")
    {
        tellUserHesWrong();
        askUserIfWantHelp();
        
        if (user wants help)
        {
            giveHelp();
        }
        
        askForInput();
 
    }
EDIT:

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   Reply With Quote
Old 09-18-2009, 03:56 PM   #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   Reply With Quote
Reply

Tags
operators, strings, while loops

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 04:32 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22