Friday, July 31, 2009

Programming issues with C++...please help?

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 %26gt;= min) %26amp;%26amp; (val %26lt;= max))


{


cout %26lt;%26lt; "Please enter exam score " %26lt;%26lt; nscores %26lt;%26lt; " : ";


cin %26gt;%26gt; val;


if ((val %26lt; min) || (val %26gt; max))


{


int y = 1;


int n = 0;


cout %26lt;%26lt; endl %26lt;%26lt; "Score out of range!" %26lt;%26lt; endl %26lt;%26lt; endl;


cout %26lt;%26lt; "Do you wish to re-enter score number " %26lt;%26lt; nscores %26lt;%26lt; " (y | n) : ";


cin %26gt;%26gt; 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/as...





The program is #12.

Programming issues with C++...please help?
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 %26gt;= min) %26amp;%26amp; (val %26lt;= 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)


}


}


}


}
Reply: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 %26gt;%26gt; 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.
Reply:Please clarify your issues, compiling error, runtime error or designing error?
Reply: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.

garden state

No comments:

Post a Comment