GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Feb-2009, 20:55
Azuran Azuran is offline
New Member
 
Join Date: Feb 2009
Posts: 2
Azuran is on a distinguished road

Input validation: can only enter a number ranging from 0-300


Okay, I'm a first-year student in C++ and I'm writing a program where a bowling team consisting of five players bowls three games. The program ask the user to enter the scores for the 5 individual players. When someone finished entering all the scores, the program takes the total score for each player and outputs the average. I got that part down.

What I'm having trouble with is the input validation. Someone can only enter a number ranging from 0-300. If someone enters a number higher or lower than that, the program ask the user the input the number again until is valid. I try doing it with an in statement but that fail because the program still counts the wrong number as part of the average. Also, when I enter two invalid numbers in a row, the program ignores the input range and keeps going.

So, cna anyone help me, give me some pointer or different suggestion or point me in the right direction?

I'll appreciate it.

Here's my current crappy code:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{

  const int BOWLERS = 5;
  const int SCORES = 3;
  double dblScores, dblAverage, dblTotal;
  int i, j;

  for (i = 1; i <= BOWLERS; i++) 
  {
    dblTotal = 0;                 
    for (j = 1; j <= SCORES; j++)
    {
      cout << "Enter score number " << j << " for bowler " << i << ": ";
      cin  >> dblScores;
      dblTotal = dblTotal + dblScores;
    
               if ((dblScores < 0) || (dblScores > 300))
               {           
                 cout << "Invalid input! Please re-enter the score again. \n";
                 cout << "Enter score number " << j << " for bowler " << i << ": ";
                 cin  >> dblScores;
               }
    }                                
    dblAverage = dblTotal / SCORES;
    cout << "\nThe average score for bowler " << i << " is " << dblAverage;
    cout << endl;
    cout << endl;
  }
                    
  cout << endl << endl;
  system("PAUSE");
  return 0;
}

... Yeah.
  #2  
Old 13-Feb-2009, 23:38
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Input validation: can only enter a number ranging from 0-300


Quote:
Originally Posted by Azuran
What I'm having trouble with is the input validation. Someone can only enter a number ranging from 0-300. If someone enters a number higher or lower than that, the program ask the user the input the number again until is valid.
Wrap input of each value in a while loop or do-while loop (Your choice...). The exit condition out of the loop is a value within the range you expect. The loop only wraps about the input, so nothing else in the code needs to be modified.
  #3  
Old 14-Feb-2009, 00:17
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 801
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Input validation: can only enter a number ranging from 0-300


Well I don't know, it looks to me like you'd want make sure this does not get done:
CPP / C++ / C Code:
dblTotal = dblTotal + dblScores;
...until you're sure the input is good.
It could be moved to after that input validation loop ocicat spoke of.
Presently it's right in the middle or at least will get hit once....
Speaking of which, if you do the loop right , you should only need one:
cin >> dblScores;
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:27.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.