Thread: Need a little bit of help with my program

1. Need a little bit of help with my program

My code dose something really basic. All it dose is that it ask you how many grades you have to enter. Then you enter them grades and it give you the sum and the average. now i put a if statement to make sure the user can not enter in a number lower or equal to 0. but it doesn't work ? The program runs but i enter a number lower then 0 and i just goes into an infinite loop? can any one led me in the right direction. Thanks

Code:
```#include <iostream>

int main ( ) {

unsigned short int numberOfGrades;
double grades;
double sum = 0;

std::cout << "Enter number of grades you are entering: ";
std::cin >> numberOfGrades;

std::cin.ignore();

if ( numberOfGrades <= 0 ) {

std::cerr << "ERROR \n\n";

return EXIT_FAILURE;

}

for ( int i = 0; i < numberOfGrades; i++ ) {

std::cout << "> ";
std::cin >> grades;
std::cin.ignore();

sum = sum + grades;
}

float Average = sum / numberOfGrades;

std::cout << std::endl << std::endl;
std::cout << "The sum is    : " << sum << std::endl;

std::cout << "The average is: " << Average << std::endl;

std::cin.get();

return ( 0 );

}```

2. Maybe try an if statement that runs everything you want if the entered number is greater than 0, and just ends the program if anything else.

3. Execute the following code:
Code:
```#include <iostream>

int main ( )
{

unsigned short int numberOfGrades;
std::cout << "Please enter a negative value ";
std::cin >> numberOfGrades;

std::cout<<"The value you entered is "<<numberOfGrades;
return 0;
}```
Playing with

unsigned short int numberOfGrades seems to be a trap.
Code:
```//Why to use unsigned short when a normal

int numberOfGrades;

//works equally well```

4. Originally Posted by zalezog
unsigned short int numberOfGrades seems to be a trap.
It is not a trap, and it makes sense since the number of grades must be non-negative. The choice of unsigned short int over unsigned int might be rather unnecessary here, but it should be fine.

The key to handling the invalid input is to check the state of the stream after the formatted input, and also check if numberOfGrades == 0.

5. I change the unsigned short int to a int and the program works... Wired I don't know why but it did. Thanks for the help / feed back.

6. Originally Posted by Sshakey6791
I change the unsigned short int to a int and the program works.
Now, try entering some letters instead of a number.

All that has happened is that you now accept a negative number as input so you can handle a negative number by declaring it invalid input, but you are still not correctly handling invalid input in general.

If you want to handle invalid input that comes after valid input, then you need to read in everything as a string and then parse that string... but this could be overkill for your program.

7. Ya it might be a little overkill for this program but now I can learn how to fix it for this useless program so I can put better use to it later.

Thanks for the help.

Do you have any were I can start reading up on this ? Any good Websites?

Thanks...

Popular pages Recent additions