Need a little bit of help with my program

This is a discussion on Need a little bit of help with my program within the C++ Programming forums, part of the General Programming Boards category; My code dose something really basic. All it dose is that it ask you how many grades you have to ...

  1. #1
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    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 );
    
    
    }
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    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. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57
    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.
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57
    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...
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21