Learning the Basics but I am running into some problems

I'm teaching myself C++ at the moment, and I am trying to write a program that will integrate most of what I have learned so far (which isn't very much).

In my code everything works fine. However, just not the way I want it too. I tried write a simple program to just check the user input and output accordingly. I also tried to write in an error check to make sure that the user can't just input a character or an integer that it has to be a either "Yes" or "No". However the, do...while and in previous tries, the while loop, does not seem to work correctly as they just continue running regardless if the user inputs the correct string.

Where did I go wrong in the loop? As I am still learning and just barely breaking the surface, any guidance of articles and other resources will be much appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  #include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main(){
    string ans;
    // string g;
    cout << "Would you like to do simple math?(Yes or No)" << endl;
    cin >> ans;
    if(ans != "Yes" || ans != "No"){
        do{
            cout << "Please enter Yes or No" << endl;
            cin >> ans;
            cout << ans << endl;
        }while(ans != "Yes" || ans != "No");
    }
    else if (ans == "Yes" || ans == "No"){
        if(ans == "Yes"){
            cout << "Lets do simple math!" << endl;
            }
        else if(ans == "No"){
            cout << "You have failed me!" << endl;
            int x = 0;
            int y = 10;
            while(x<10){
                y--;
                cout <<"\n"<< y;
                if (y == 0){
                    cout << "\n";
                    cout << "Boom!" << endl;
        }
        x++;
    }
    }

    }
}
if(ans != "Yes" || ans != "No")
this doesnt make any sense, if you Input "No" the first condition is true, if you Input "Yes" the first condition is true,

TRUE || NO = TRUE
NO || TRUE = TRUE
Ah, I understand. That explains why my loop kept running regardless of the input being Yes or No, because it was always true. But then the question is how would I change the condition so that it would allow the loop to run correctly and still be able to stop the user from writing anything other than the strings that I have set? I think thats the problem I'm running into, because I just don't understand how I can set the condition so that it will only accept strings and reject anything else and make it run the while loop until the user types either Yes or No.

Edit: Is it possible for me to just generalize the condition and say if ( ans != string ){ blah blah }? What I mean by string is just getting the variable type, so that if the user enters, an integer, or a character that it'll kick them to the loop until they enter the string?
Last edited on
It's much cleaner to put all of this in a do...while loop.
1
2
3
4
do
{
    //question.
}while(ans != "No" && ans != yes); //You want an 'and', not an 'or' condition. ;)  


Also, later on, notice you are incrementing x outside of it's while loop!!!

1
2
3
4
5
6
7
8
while(x<10){
                y--;
                cout <<"\n"<< y;
                if (y == 0){
                    cout << "\n";
                    cout << "Boom!" << endl;
        }
        x++;


Personally, I tend to prefer for loops as they tend to prevent such scenarios.

1
2
3
4
for(x = 0; x < 10; ++x} //<-- Initial value, test value and increment all together on one line
{
 //Do stuff
}


*You might want to edit it to add in "yes" and "no" too (unless you want to be a really grammar-nazi about capitalization!).

Is it possible for me to just generalize the condition and say if ( ans != string ){ blah blah }? What I mean by string is just getting the variable type, so that if the user enters, an integer, or a character that it'll kick them to the loop until they enter the string?


No. An integer or a single character is also a valid string. So in this case, you can't check what kind of data the user enters. You decided that already by reserving memory for a string!

string ans; //You already decided what would be entered is a string.
Last edited on
Ah, I see. Thats awesome! Thanks for the help!
About the incrementing x I think its inside the loop its just probably where I placed it that it made it look like it was outside.
I think I will re-code this using the do while loop after i get this code to work correctly as it'll allow me to understand the functions better.


As I am still learning C++, could you recommend me some resources I can take a look at to give me a better understanding? At the moment I am using an APP on my phone that gives video tutorials, however the way the person explains it makes it a little bit difficult to understand fully that the function is capable of. Anything will be of help!

Edit: Would it be possible for you to message me with the resources? Since the problem is solved, I don't think i would need to keep it open :D. Thank you very much again @Mats and @Lorence30!
Last edited on
Well since you have PMs disabled...

I really find the best way to improve is to do projects. I started out by doing simple things... e.g. Making a text calculator that does basic arithmetic, making a blackjack game and then just kept on doing more and more advanced stuff, looking up what I thought was needed and asking for help on here if I got really stuck at any time.

Taking a look at problems posted on here (to start with in the beginner's section) also will help. Think and see if you can solve the problem yourself. If you couldn't, read up what the solution was and learn from it.

Reading books is also good, but you a quick look in the search box here will yield lots of results for good beginner books.

And yes you're right about the while loops. Be consistant with those indents! When programs get large and complicated, you will need it!
Topic archived. No new replies allowed.