1

How can I properly use a do, while, or for loop of some kind to prevent the user from entering anything else other than the answer "1" or "2"?

If they don't, the program should tell them they cannot do that and then return them to the previous question.

#include <iostream>
#include "Options.h"
#include <string>

using namespace std;

int main(int argc, char *argv[]) {
    cout << "\tWelcome to my text adventure game\n" << endl;
    cout << "Please enter your characters Name: ";
    string name;
    cin >> name;
    cout  << "\nWelcome " << name << endl;
    cout << "\nPlease choose the side you would like to play in - ";
    Options OptionsObject;
    OptionsObject.optionsSide();

    string answer;
    while(answer != "quit") {
        cin >> answer;
        cout << answer << endl;
    }

    if ( answer == "1" ) {
        cout << "You chose the good side. Let the game begin\n "  << endl;
        cout << "You are located in a city named after the warrior who saved it from the evil\nmany years ago. The city of Redshore. " << endl;
        cout << "You are no ordinary man in the City of Redshore. You are the king who rules it\nYou are seen as King of Justice, a good king.\nOne who only want what is best for his people" << endl;
        cout << "but also a troubled man. You experienced something traumatizing when you\nwere a just a little boy, but no one knows about it,\nno one but yourself that is  " << endl;
    } else if( answer == "2" ) {
        cout << "hey there" << endl;
    }
}
2

2 Answers 2

0

Seen that this problem would be in all the choose the user will do during the game I will make an external function, to let the user choose. Maybe a static class working in this way.

int ans = Answer.getUserAnswer(2,"[1/2]>");

and this should work in this way:

public static int getUserAnswer(int max =1,string message="[1/1]>")
{
    int ans = 0;
    while(ans==0){
        cout<<message<<" ";
        cin >> answer;
        if(answer>0 and answer<=max) return ans;
        cout<<"\tnot a valid choose\n";
        ans=0;
    }
}

and you will have in your ans one of the value you expect, and it will print "not a valid choose" if the answer is not between the one you expect, and you can do this even with 10 answers, calling it by default expect you to give him just the number one.

I used it in a console game I made ;) Hope is useful

EDIT

int getUserAnswer(int max =1,string message="[1/1]>")
{
    int ans = 0;
    while(ans==0){
        cout<<message<<" ";
        cin >> answer;
        if(answer>0 and answer<=max) return ans;
        cout<<"\tnot a valid choose\n";
        ans=0;
    }
}

and to request this code in your code:

cout  << "\nWelcome " << name << endl;
    cout << "\nPlease choose the side you would like to play in - ";
    Options OptionsObject;
    OptionsObject.optionsSide();

    int answer =getUserAnswer(2,"[1/2]>");

    if (answer == 1){

cout << "You chose the good side. Let the game begin\n "  << endl; cout << "You are located in a city named after the warrior who saved it from the evil\nmany years ago. The city of Redshore. " << endl; cout << "You are no ordinary man in the City of Redshore. You are the king who rules it\nYou are seen as King of Justice, a good king.\nOne who only want what is best for his people" << endl; cout << "but also a troubled man. You experienced something traumatizing when you\nwere a just a little boy, but no one knows about it,\nno one but yourself that is  " << endl;
    }

    else if(answer == 2){

        cout << "hey there" << endl;

    }
4
  • 1
    I have completely no idea whats going on in this code, I still havent come far enough to know what a public static means, and Im not very sure on how to "get" something from other classes
    – Tarik Neaj
    Sep 16, 2014 at 16:15
  • Well so use it as a function in your main, is not the correct way...but it works. just took away public static and use the getUserAnswer function as it is. in the code after the question you should use it just like int ans = getUserAnswer(2,"[1/2]>"); where the first parameter is the maximum answer the user can give you, and the second is the prompt shown every iteration. try it
    – vikkio
    Sep 16, 2014 at 16:18
  • 1
    Why is the final line of your loop a naked comparison? ans==0; Sep 16, 2014 at 16:22
  • because I made a mistake :D that have to be ans = 0;
    – vikkio
    Sep 16, 2014 at 16:24
0

You should put the line where you read the input in a do loop that loops while the answer given by the user is invalid.

So, your do loop should be:

string answer;

cin >> answer;

// loop while answer is neither "1" nor "2"
while( answer != "1" && answer != "2" ) {
    cout << "Please enter a 1 or a 2. You entered " << answer << endl;
    cin >> answer;
}
3
  • Or use do...while which seems more logical here (one read is always desired). Sep 16, 2014 at 16:05
  • I tried implementing it and Im pretty sure I didint do it correclty. Now when I type anything else other than 1 or 2 the program doesnt shut down like before, but nothing happens at all I can just type numbers forever.
    – Tarik Neaj
    Sep 16, 2014 at 16:16
  • After the user enters "1" or "2", you are sort of insinuating that they have entered something else. Sep 16, 2014 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.