Checking input type by user

I'm an absolute beginner to C++ and started yesterday but I'm trying to solve this issue that I guess is important maybe.
I've browsed the forum a bit and found several threads on how to check if the user input is an int, a string etc... and tried different methods proposed by people but for some reason I can't manage to make it work on my code, when I run de program it simply asks twice for the first variable name (even if the user enters a number/letter/string) and then continues with the rest of the code.
Can you guys help me out? The code is the following:
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
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;

int main() {
    
    char variableX, variableY;
    string inBetweenVariables;
    int factorX, factorY;
    int factorXResult, factorYResult;
    int inBetweenDoubled;
    int finalresult;
    bool valid = false;
    
    cout << "Enter the name of your first variable (x, a...) : \n";
    cin >> variableX;
    while (!valid)
    {
        valid = true; //Assume the cin will be an integer.
        
        cout << "Enter the name of your first variable (x, a...) : \n";
        cin >> variableX;
        
        if(cin.fail()) //cin.fail() checks to see if the value in the cin
            //stream is the correct type, if not it returns true,
            //false otherwise.
        {
            cin.clear(); //This corrects the stream.
            cin.ignore(); //This skips the left over stream data.
            cout << "Please enter a letter only, not a number or sequence of numbers/letters." << endl;
            valid = false; //The cin was not an integer so try again.
        }
        
    }
    
    cout << "Enter the name of your second variable (y, b...) : \n";
    cin >> variableY;
    cout << "This program will expand (a+b)^2.\nEnter the factor of " << variableX << " : \n";
    cin >> factorX;
    cout << "Enter the factor of : " << variableY << "\n";
    cin >> factorY;
    factorXResult = factorX*factorX;
    factorYResult = factorY*factorY;
    inBetweenDoubled = 2*factorY*factorX;
    cout << factorXResult << variableX << "^2" << " + " << inBetweenDoubled << variableX << variableY << " + " << factorYResult << variableY << "^2\n";
    
    system("PAUSE");
    return 0;
}

> when I run de program it simply asks twice for the first variable name
remove lines 14, 15
I did but then the program simply asks once for the first variable and whatever the input is it accepts it and continues with the rest of the code.
I changed it and tried something else but it still doesn't work and accepts every input :
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
#include <iostream>
using namespace std;

int main() {
    
    string variableX, variableY;
    string inBetweenVariables;
    int factorX, factorY;
    int factorXResult, factorYResult;
    int inBetweenDoubled;
    int finalresult;
    
    cout << "Enter the name of your first variable (x, a...) : \n";
    cin >> variableX;
    while (!cin)
    {
        cout << "Please enter a letter only, not a number or sequence of numbers/letters." << endl;
        cin.clear();
        cin.ignore();
        cout << "Enter the name of your first variable (x, a...) : \n";
        cin >> variableX;
        
    }
    
    cout << "Enter the name of your second variable (y, b...) : \n";
    cin >> variableY;
    cout << "This program will expand (a+b)^2.\nEnter the factor of " << variableX << " : ";
    cin >> factorX;
    cout << "Enter the factor of : " << variableY;
    cin >> factorY;
    factorXResult = factorX*factorX;
    factorYResult = factorY*factorY;
    inBetweenDoubled = 2*factorY*factorX;
    cout << factorXResult << variableX << "^2" << " + " << inBetweenDoubled << variableX << variableY << " + " << factorYResult << variableY << "^2\n";
    
    return 0;
}
Hello FastBanana,

The problem you are having is because you redefined "variableX and variableY" as strings from a char. Now line 14 will take and put into "variableX" whatever you type up to either a space or new line character. And if there is a space that will cause a new problem with the next "cin >>". You would do better changing those variables back to a char.

Suggestions;
1. the while loop starting at line 15 should follow each "cin >>" to verify what was entered.
2. the cin.ignore(); is more often written std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); this will need the header file "limits".

Note the "std::" qualifier is not needed the way you have used "using namespace std;", but it does not make any difference if you leave it.

Hope that helps,

Andy
Topic archived. No new replies allowed.