Thread: Need a little bit of help with my program

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 03:30 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM