Keeping Correct User Input

I need the user to input a 7 digit number that will later be output back. If the user inputs a number shorter or longer, they will be prompted to re-input a 7 digit number. When it comes time to output their number, the initial number input will be output, not the last correct 7 digit one. Can anyone help?

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
51
int main()
{
    string name, idNumber,gradeLetter;
    int grade;
    
    cout<< "••••••••••••••••••••••••••••••••••••••••••"<<endl
    <<"\tWelcome to Grade Estimation Program\n"
    << "••••••••••••••••••••••••••••••••••••••••••"<<endl<<endl
    <<"Please Enter your Name (LastName,FirstName): ";
    getline(cin, name);
    
    cout<<"\nPlease Enter you 7 Digit ID: ";
    cin>>idNumber;
    if (idNumber.length() ==7) {
        cin>>idNumber;
        
    }
    else  {
    for (string idNumber;idNumber.length()<7||idNumber.length()>7;){
    cout<< "\nPlease Enter your 7 Digit ID: ";
    cin>>idNumber;
        }
    }
    for (int grade; grade <= 0 || grade >100;){
        if (grade <= 0 || grade > 100) {
            cout<<"\nPlease Enter a Score Between 0 and 100: ";
            cin>>grade;
        }
        if (grade >=90){
              gradeLetter= "A+";
          }
          else if (grade >=85 && grade < 90){
              gradeLetter= "A";
          }
          else if (grade >=80 && grade < 85){
              gradeLetter= "A-";
          }
          else if (grade >=70 && grade < 80){
              gradeLetter= "B";
          }
          else if (grade >=60 && grade < 70){
              gradeLetter= "C";
          }
          else if (grade <60){
              gradeLetter= "F";
          }

          if (grade>0 && grade<101) {
            cout<<"\nBased on the "<< grade <<" "<<name<<"("<< idNumber <<")" <<" will likely get "<<gradeLetter<<endl;
        }
    }

Last edited on
You have two variables with the same name.
> string name, idNumber,gradeLetter;
...
> for (string idNumber;idNumber.length()<7||idNumber.length()>7;){

Your else part needs to use the original variable, not declare another one.

Also, this -> http://www.cplusplus.com/articles/jEywvCM9/
This is how it runs and what gets output. I thought my else part did use the original idNumber variable?

••••••••••••••••••••••••••••••••••••••••••
Welcome to Grade Estimation Program
••••••••••••••••••••••••••••••••••••••••••

Please Enter your Name (LastName,FirstName): Smith,John

Please Enter you 7 Digit ID: 123

Please Enter your 7 Digit ID: 1234567

Please Enter a Score Between 0 and 100: 101

Please Enter a Score Between 0 and 100: -9

Please Enter a Score Between 0 and 100: 85

Based on the 85 Smith,John(123) will likely get A
Program ended with exit code: 0
When validating input, I find it easiest to break out of the middle of the loop. Something like this:
1
2
3
4
5
6
7
8
while (true) {
    // Prompt for input
    // Get the input
    // If the input is valid then break
    // else tell the user what's wrong with the input
}

// When you get here, the input is valid. 
Here is what i have. I just don't understand why only the initial input is saved into 'idNumber'. How can the newest input override the previous one(s)?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    while (true) {
        cout<<"\nPlease Enter you 7 Digit ID: "; //prompting for id
        cin>>idNumber;
        if (idNumber.length() ==7) {
        cin>>idNumber;
        break;
        }
    
    else {
    for (string idNumber;idNumber.length()<7||idNumber.length()>7;){
    cout<< "\nPlease Enter your 7 Digit ID: ";
        cin>> idNumber;
        }
    }
    
> I just don't understand why only the initial input is saved into 'idNumber'.
What part of "you have TWO variables with the same name" is confusing you?

> for (string idNumber;idNumber.length()<7||idNumber.length()>7;)
This is a BRAND NEW VARIABLE, that has nothing to do with the other one with the same name.
If you write to this one, then whatever you store in it is LOST when the for loop exits.

1
2
3
4
do {
    cout<< "\nPlease Enter your 7 Digit ID: ";
    cin>> idNumber;
} while ( idNumber.length() != 7 );
ohh my bad, i thought i was just writing which variable was going to be used.....Thanks :')
Topic archived. No new replies allowed.