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:
#include <iostream>
#include <ctype.h>
#include <cstdlib>
usingnamespace 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.