Problem with strings and while loops

This is a discussion on Problem with strings and while loops within the C++ Programming forums, part of the General Programming Boards category; I'm fairly new to C++, and as a learning exercise I'm making a simple 'quiz game' in which the user ...

  1. #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

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    On the right half of the OR, you need to compare prima to "One".
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #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.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    prima != "Help" || prima != "One" is always true.

  5. #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

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,591
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #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.

  8. #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.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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