English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

For those who remember, this is the function I'm having problems with:

bool in_range(int val, int min, int max)
{
bool ans;
int nscores;
while ((val >= min) && (val <= max))
{
cout << "Please enter exam score " << nscores << " : ";
cin >> val;
if ((val < min) || (val > max))
{
int y = 1;
int n = 0;
cout << endl << "Score out of range!" << endl << endl;
cout << "Do you wish to re-enter score number " << nscores << " (y | n) : ";
cin >> ans;
if (ans == y)
return 1;
if (ans == n)
{
return 0;
}
}
nscores = nscores + 1;

}
}

This is the part that is giving me issues. I have the right variables set and all. If you need a more in-depth look as to what this is doing, please use this link:

http://www.cs.uwm.edu/~cs201/semester/assignment/06/06/06.html

The program is #12.

2006-10-27 04:45:46 · 5 answers · asked by Charlie 1 in Computers & Internet Programming & Design

5 answers

You are out of spec. The specs call for the loop to keep accepting input then check for range. Only when the input is out of range that you ask if they want to re-enter or quit to the report.

Your program (once it gets past while ((val >= min) && (val <= max)) ) will loop until invalid input is given. As this is coursework, I will only give pseudocode - the rest is up to you.

functionX (min, max)
{
loopagain=true;
while(loopagain)
{
displayinstructions();
// tell them to enter numbers, and
//enter Q to quit or something like that
if(checkifnumeric()==true)
{
if(checkifinrange()==true)
{
addToReport();
loopAgain();
}
else
{
displayInstructionsAndLoopAgainOrExit();
//this is where you tell user that input is invalid
//and ask for valid input (a number in range or
//Q to quit)
}
}
else
{
if(checkifQ()==true)
{
gotoReport();
}
else
{
displayInstructionsAndLoopAgainOrExit();
//this is where you tell user that input is invalid
//and ask for valid input (a number in range or
//Q to quit)
}
}
}
}

2006-10-27 05:24:51 · answer #1 · answered by Timoy 2 · 0 1

Well, after a quick look at your code snippet:

-You shouldn't allocate variables within an IF statement. You are allocating the 'y' and 'n' variables in a IF statement, which is within the WHILE loop. Allocate your variables outside of main() if you want them to be global. If you want to allocate local variables, then allocate them at the start of the procedure.
-Your 'ans' variable would be better off as a char. Cout and cin prints and inputs a variable based on the data type you are passing to it. In your 'cin >> ans' line, you want the user to input a character (y/n), so allocate 'ans' as a char. Your If statement would have to be modified to something like this: if (ans=='y')
-You can do some more idiot-proofing with the code that checks if the user pressed 'y' or 'n'.
For example, you could write something like this:

if (ans=='y')
return 1;
return 0;

If the user pressed 'n' or any other key, then the program will go to the 'return 0;' line.

2006-10-27 05:06:57 · answer #2 · answered by Balk 6 · 0 0

you never initialize nscores, so when you output it the first time, you will get garbage.
the only way to exit the loop is to enter a invalid score and answer "n". if I read it right you need to add a return 0; after nscores = nscores + 1 line.

2006-10-28 02:50:56 · answer #3 · answered by justme 7 · 0 0

i'm no longer a c++ programmer so i do no longer comprehend the syntax o.k. notwithstanding curiously to me which you're lacking curly braces after your if statements. attempt this: if(m=="addition") { cout << "upload this selection: "; cin >> n; cout << "To this selection: "; cin >> o; cout << "the respond is: "; cout << n + o; }

2016-10-16 11:24:06 · answer #4 · answered by Erika 4 · 0 0

Please clarify your issues, compiling error, runtime error or designing error?

2006-10-27 05:01:23 · answer #5 · answered by Anonymous · 0 0

fedest.com, questions and answers