Noob here. Need help with If statements

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 <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    string mystr;
    
    cout << " What is your name? ";
    getline (cin, mystr);
    
    cout << " Nice to see you " << mystr << ", it is my pleasure to meet you.\n";
    system("PAUSE");
    cout << " May I ask, would you like to keep going this conversation or just shut me off?\n (input yes or no, if not the computer will want to smite you)\n ";
    getline (cin, mystr);
     //if anything is inputed it will print out the following
    //if(mystr == (cin, mystr))  
    
    if(mystr == "yes") {
     cout << " Ok good let's keep going\n";
}
    else{
     cout << " Too bad. We're going anyways.\n ";    
}    
    cout << "So... since we are still here, how has your day been?" <<endl;
    getline (cin, mystr);
    
    if (mystr != mystr ) {
     cout << "That's great. I suppose. I am a computer, I can't feel how my day has been" <<endl;         
}  
    else{
     cout << "Oh look at you, trying to ignore me eh? Well, until you answer you can't get rid of me :-)" <<endl;   
}    
system("PAUSE");
    return 0;
}



Hello everyone. I need some simple help getting through my practice with If-else statements. I just started coding not that long ago so this may be quite the simple thing. Basically, line 28, what can I do so that the program knows that if an any string or letters of the alphabet are put, it will output line 31. If not, or that if numbers are put or just empty space, it will respond with the else. How can I make that happen? Once again, complete beginner here and I just started.
One way is to search the string one character at a time to determine if it meets your requirements.

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
#include <iostream>
#include <string>
using std::cin;
using std::cout; using std::endl;
using std::string;

int main()

{
    string test;

    getline(cin, test);

    for (int x = 0; x <= test.size(); ++x) // Searching through the string, one character at a time.
    {
        if (isdigit(test[x])) //If at any time the character is a digit, continue with the following statement.
        {
            cout << "There is a number, go away!" << endl;
            break;
        }
        if (isspace(test[0])) //If at the beginning of the sentence an empty space is detected, continue with statement.
        {
            cout << "Didn't you know that sentences don't start " <<
                    "with a space? Probably an empty sentence." << endl;
            break;
        }
        if (x == test.size()) //Searched through the entire string, no issues.
        {
            cout << "Good, this is actually a string." << endl;
            break;
        }
    }

    return 0;
}


Let me know if there's something you don't understand.

Edit: If you answer "no" when the program ask if you want to continue the conversation, it will still run to the final if else statement. I suppose you'll want to find out how to end a program prematurely to solve that.
Last edited on
How would I implement this into my code? It seems a little advanced. I can basically see what the code is doing which is exactly what I want but how do I implement it?
Topic archived. No new replies allowed.