Checking for user input

hello hey or hi..

so im checking for user input as the title says the user is asked to input the annual income for a company and that of course should only be in numbers.

now the problem is that i want the user to only enter a number not a character if by any chance the user dose enter a character the program messes up well i cant explain it well in words but here is the output of the program.

1st one is when the user enters a number and the program runs fine.

2nd output is when the user enters a character and the program just well gets messed up.

1st Output:
Income for year 2000: 1000
Income for year 2001: 2000
Income for year 2002: 3000
Income for year 2003: 4000
Income for year 2004: 5000
Income for year 2005: 6000
Income for year 2006: 7000
Income for year 2007: 8000
Income for year 2008: 9000
Income for year 2009: 10000


2nd Output:
Income for year 2000: a
Invalid Entry: Income for year 2001:  Invalid Entry: Income for year 2002: 3000:
Invalid Entry: Income for year 2003:  Invalid Entry: Income for year 2004: 5000:
Invalid Entry: Income for year 2005:  Invalid Entry: Income for year 2006: 7000:
Invalid Entry: Income for year 2007:  Invalid Entry: Income for year 2008: 9000:
Invalid Entry: Income for year 2009:  Invalid Entry:


Here is the code of the program:
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 <ctype.h>
#include <cstdlib>

using namespace std;

// Declaring Functions.
void getAnnualIncome(int[]);

main(){
    cout << "\t\t\t***** ABC Company Income Sheet *****\n\n\n";

    // Declaring the variables that will be used in the program.
    int annualIncome[10];

    // Calling the function to get the annual income for 10 years from the user.
    getAnnualIncome(annualIncome);
}

// Function to get annual income for 10 years.
void getAnnualIncome(int annualIncome[]){
    int year = 2000;

    // Prompting the user to enter the company's annual income for 10 years one by one.
    cout << "Please enter the income for past 10 years <In Million Only>: " << "\n\n";

    for(int i = 0; i < 10; i++){
        cout << "Income for year " << year << ": \t";
        cin >> annualIncome[i];
        if(annualIncome[i] != isdigit(annualIncome[i])){
            cout << "Invalid Entry: ";
            cin >> annualIncome[i];
        }
        year++;
    }
}

I don't understand exactly what you want.
A way is to use "do... while".
So the program will repeat cin until annualIncome[i] == isdigit(annualIncome[i]).
well what i want to do is that if the user enters a character the program should output Invalid entry and ask the user to input again. but instead of that happening the program breaks down when a character is entered.
So do what I said.

1
2
3
4
5
6
7
do {
   cout << "Income for year " << year << ": \t";
   cin >> annualIncome[i];
   if (annualIncome[i] != isdigit(annualIncome[i])) {
            cout << "Invalid Entry. Try again." << endl;
   }
   } while (annualIncome[i] != isdigit(annualIncome[i]));


This should work.
That dose not seem to work if i enter in a character it runs in an infinite loop.
Topic archived. No new replies allowed.