I need some help. its an easy problem but...

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 4
Reputation: xfactornos is an unknown quantity at this point 
Solved Threads: 0
xfactornos xfactornos is offline Offline
Newbie Poster

I need some help. its an easy problem but...

 
0
  #1
Jan 13th, 2008
I have been writing my program and everything works like it should unless the user inserts a character then the whole thing blows up... how can I fix this.

Is there something I can add to block characters from being added? any help would be appreciated!

here is my code:
#include <iostream>
using namespace std;

int main()
{
	// Declaring Variables
	int numscores = 0;
	double average = 0.0;
	double total = 0.0;
errorcheck1:

	// Ask for user input: How many tests to average
	cout << "Please enter how many test scores you would like to average: ";
	cin >> numscores;

	// Check to make sure number of scores is greater then 0 but less then 101
	if (numscores <= 0 || numscores > 100)
	{
		cout << "You must enter a number greater then 0 but less then 101!" << endl;
		cout << endl;
		goto errorcheck1;
	}
	// Getting the values for the number of tests
	// Start of loop
	double scores[100] = {0};
	for (int x = 1; x <= numscores; x++)
	{
		cout << "Enter test score #" << x << ": ";
		cin >> scores[x];
		if (scores[x] < 0 || scores[x] > 100)
		{
			cout << "==============================================" << endl;
			cout << "Error: Please enter a value between 0 and 100. " << endl;
			cout << "==============================================" << endl;
			x = x - 1;
		}
	} // End of loop
	// This is the output section
	cout << "=======================================" <<endl;
	cout << "Total number of test scores: " << numscores << endl;
	// Add all of the scores and call it total
	for (int y = 0; y <= numscores; y++)
	{
		total = total + scores[y];
	}
	// End loop
	// Below I will average the scores then display them
	average = total / numscores;
	cout << "Average of all test scores: " << average << endl;
	//End
	// Needed for some compilers like mine
	return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: I need some help. its an easy problem but...

 
0
  #2
Jan 13th, 2008
Ah, young grasshopper, you have advanced to the next level: you have recognized the need for better input validation.

It might be worth it to make yourself a little function that reads and returns a validated number.
Last edited by Duoas; Jan 13th, 2008 at 12:33 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: xfactornos is an unknown quantity at this point 
Solved Threads: 0
xfactornos xfactornos is offline Offline
Newbie Poster

Re: I need some help. its an easy problem but...

 
0
  #3
Jan 13th, 2008
Could you explain a little more on what you mean? I read that site you sent me and its confusing me because my setup isn't like his.

Plus im new to programming and require a little more help.

Thanks so much.
Matthew
Last edited by xfactornos; Jan 13th, 2008 at 12:42 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: I need some help. its an easy problem but...

 
0
  #4
Jan 13th, 2008
He's using ISO-standard C++, so everything he does you can do. However, the code he provides isn't in the same order you would put it in your program, just in the order he tells you about it.

The basic strategy is that you should read all user input as a string.
  1. #include <iostream>
  2.  
  3. int main() {
  4. std::string user_input;
  5.  
  6. std::cout << "How many years old are you? ";
  7. std::getline( std::cin, user_input );
  8.  
  9. return EXIT_SUCCESS;
  10. }

At this point, the user could have entered anything --including nothing by just pressing the ENTER key. However, since we can expect users to do dumb stuff like that, we have obtained input in a safe way: the program is still working properly.

Now, we need to verify that the user gave us a string that is correctly formatted. Since we are asking for a number, let's create some way to verify that the user did, in fact, enter a number using the digits 0..9. I'll use the same kind of technique referenced in the article.
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cctype>
  4.  
  5. // This is just a fancy way of making a function that returns
  6. // true if c is not a digit in '0'..'9'. Except, instead of a function,
  7. // it is an object.
  8. class nondigit {
  9. public:
  10. bool operator () ( char c ) { return !isdigit( c ); }
  11. };
  12.  
  13. int main() {
  14. std::string user_input;
  15.  
  16. std::cout << "How many years old are you? ";
  17. std::getline( std::cin, user_input );
  18.  
  19. // If the user entered anything except digits, find where
  20. // in the string. Notice how we create a temporary 'nondigit'
  21. // object to use as the test predicate.
  22. std::string::iterator error_iter =
  23. find_if( user_input.begin(), user_input.end(), nondigit() );
  24.  
  25. // Now complain if he did, by showing him exactly where he
  26. // made his error.
  27. if (error_iter != user_input.end()) {
  28. std::cout << user_input << '\n'
  29. << std::string( error_iter -user_input.begin(), ' ' )
  30. << "^this is not a digit!\n";
  31. return 0;
  32. }
  33.  
  34. return EXIT_SUCCESS;
  35. }

Once we have verified that the input is correct, we can convert it to an actual number. Here it is again. (This time we use a function for the test predicate instead of an object, just so you can see the difference.)
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <cctype>
  5.  
  6. // This is our function that returns true if c is not a digit
  7. // in '0'..'9'.
  8. bool nondigit( char c ) { return !isdigit( c ); }
  9.  
  10. int main() {
  11. std::string user_input;
  12.  
  13. std::cout << "How many years old are you? ";
  14. std::getline( std::cin, user_input );
  15.  
  16. // If the user entered anything except digits, find where
  17. // in the string.
  18. std::string::iterator error_iter =
  19. find_if( user_input.begin(), user_input.end(), nondigit );
  20.  
  21. // Now complain if he did, by showing him exactly where he
  22. // made his error.
  23. if (error_iter != user_input.end()) {
  24. std::cout << user_input << '\n'
  25. << std::string( error_iter -user_input.begin(), ' ' )
  26. << "^this is not a digit!\n";
  27. return 0;
  28. }
  29.  
  30. // Else convert it to an actual number
  31. int users_age;
  32. std::stringstream ss( user_input );
  33. ss >> users_age;
  34.  
  35. std::cout << "So you are " << users_age << " years old.\n";
  36.  
  37. return EXIT_SUCCESS;
  38. }

Ah, almost forgot. All these programs you can compile and test yourself. Try giving it both valid and invalid input. Like
12
twelve
30years
2600 moons

I hope this helps.
Last edited by Duoas; Jan 13th, 2008 at 1:32 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: xfactornos is an unknown quantity at this point 
Solved Threads: 0
xfactornos xfactornos is offline Offline
Newbie Poster

Re: I need some help. its an easy problem but...

 
0
  #5
Jan 13th, 2008
Thanks for your reply but I tried to use the code with mine and I had a lot of errors. One was something about a redefinition.
Im still working on it and I appreciate the responses.

I might just turn it in as is because there is nothing in our book that says what you guys are trying to teach me.

once again thanks guys


Edit:
I tried your examples and they work fine but i cant seem to integrate it into my code.

Matthew
Last edited by xfactornos; Jan 13th, 2008 at 2:01 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: T.N.Sharma is an unknown quantity at this point 
Solved Threads: 1
T.N.Sharma T.N.Sharma is offline Offline
Newbie Poster

Re: I need some help. its an easy problem but...

 
0
  #6
Jan 13th, 2008
i think this function can be of your help. You could read scores as characters and pass to this function to check for any characters in it. the function will return you with a integer value of score.

#include<iostream>
using namespace std;

int checkchar(char scores[4])
{

        int y = 0;
        int i=0;
        cin>>scores;
        while(scores[i]!='\0')  {
                if (((int) scores[i]<48) || ((int) scores[i]>57))       {
                        cout<<"The score is not a proper number \n";
                        return 1;
                }
                else
                        y = (int) scores[i] - 48 + y*10;              //to convert to integer
                i++;
        }


        return y;
}
[/QUOTE]
Last edited by T.N.Sharma; Jan 13th, 2008 at 5:21 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3
Reputation: taivox is an unknown quantity at this point 
Solved Threads: 1
taivox taivox is offline Offline
Newbie Poster

Re: I need some help. its an easy problem but...

 
0
  #7
Jan 13th, 2008
If nothing here helps then i should say Just Google It!
Dear all,
Please click here, to help me
Please register and start surfing ads daily.
Thanks to all who will do that for me!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: I need some help. its an easy problem but...

 
0
  #8
Jan 13th, 2008
Ah, yes, it is the beginning of the year... so you should be at the beginning of your programming course and not at the end...

Sorry I went over your head.

The basic principles are:
  1. always read user input as a string, using something like getline( cin, s );
  2. test the string to make sure the input is valid
  3. convert the string to the proper type of thing (integer, float, enum, etc.)
This is not lighthearted stuff. But all robust code that interfaces with the user must have it in some form. In academia people get away without it because the focus is often on other things.

There are as many ways to check input as you can think of. You could, for example, just create a function that tells you if it is good or bad.
  1. getline( cin, s );
  2. if (!isanumber( s )) { cout << "fooey!\n"; return; }
  3. n = converttonumber( s )
Somewhere else you'd have defined the handy little functions isanumber() and converttonumber() to help you. The abstraction and organization of your code is up to you.

T.N.Sharma Be careful:
  • Get rid of that cin>>scores;! It's pure evil, and getting rid of it is the whole point of this thread.
  • The argument should be char scores[].
  • Don't hardcode ASCII values. Use '0' and '9'.
  • Your function tries to do two things but succeeds with only one.
    1. if your function determines that the input is invalid, it writes a message, but still returns a value to be used. The caller will have no idea that something is wrong and will continue on its merry way with invalid data.
    2. also, in general, utility functions should not communicate with the user, or abort the program, etc.
    The best ways to keep validation and conversion combined like you have is one of:
    1. Throw an exception if the input is invalid
    2. Return a functor type (like Haskell's 'maybe')
    3. Use reference parameters to return the number, validity, or both.
    4. Restrict the output (for example, say negative numbers are invalid and return a negative number if the input is invalid)
    It is laudable to realize that validation and conversion are closely related. The idea of making a single function to validate and convert is perfectly valid. Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: xfactornos is an unknown quantity at this point 
Solved Threads: 0
xfactornos xfactornos is offline Offline
Newbie Poster

Re: I need some help. its an easy problem but...

 
0
  #9
Jan 14th, 2008
Well guys I contacted my instructor and he said not to worry with it yet. This is only our first program. He states we will learn strings later.
Thanks for all your help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC