In the Input function I am supposed to update the variables MilesDriven and GalsUsed. It says to do it by using Pass-By-Reference. Then, after completing that the program will go back to the menu display.
The call to Input() on line 33 requires 2 parameters to be passed.
Call as: Input(MilesDriven, GalsUsed);
Also add code to the Input function for input of GalsUsed. It currently only gets MilesDriven from the user.
What's with the orphaned 'do' on line 78? Did you want to make a do-while loop in the function?
I will fix those things. Also, you are correct as the 'do' is going to be a do-while loop in the Input function! I am required to have the program loop back to the menu until the user asks to quit.
Take input for the 2nd value (as mentioned in 1st post) and complete the do-while loop.
Also, cin.clear() won't be enough to reset for a retry at reading values. You must also remove the offending input from the stream. Use also cin.ignore(256, '\n'); after cin.clear();.
There are many syntax errors in your current code for Input(). Try to fix some of them.
eg. Line 76 belongs inside the function.
Line 81 fail is a function, so call as cin.fail()
line 86 missing ; at the end.
do-while missing {} braces and the while at the end.
Please post back something for the Input function that compiles (or at least comes close).
EDIT: I keep seeing more errors! Lose the ; at the end of line 83 or lines 84-88 not associated with the if.
The error is because you've declared CalcAndDisplay returns an int, but there is no return in the function. Change return type to void.
Nice improvement on the Input function but the code may not work right yet.
I wrote some similar code. See if it fits your situation. You can run it in the shell here.
Click the little wheel like symbol at the upper right corner of the code.
#include<iostream>
usingnamespace std;
int main()
{
double a,b;
bool invalid = false;
do
{
invalid = false;
cout << "Enter a and b: ";
cin >> a >> b;
if( cin.fail() )
{
invalid = true;
cin.clear();
cin.ignore(256, '\n');
}
}while( invalid );
cout << "valid a and b has been entered! a = " << a << " b = " << b;
cout << endl;
return 0;
}
Try entering letters for a and b. It should repeat until numbers are entered.
About the CalcAndDisplay function. Why prompt for MilesDriven and GallonsUsed again? Isn't collection of that data Input's job?
Pass the already collected values to CalcAndDisplay and just use them there.
But, you are right. The input function isn't working correctly. I think I understand how to convert my code into the format you've provided above so I'm going to work on that!
Also I think I understand what you mean about CalcAndDisplay. Basically I can eliminate lines 61-66, correct? Because they are already declared in Input? Hopefully I'm understanding that correctly.
EDIT: I just tried deleting those lines in CalcAndDisplay and the function did something funky! It just spit out all 0's and went back to menu!
Yes, you need to do a couple of other things for that change. Also modify the prototype before main and change how the function is called in the switch body. I was hoping you would see the need for those other changes.
For learning purposes, look at the errors and see if you can tell that's what they're telling you is wrong.
Okay I am currently working on it! I see for the Input function example that you did the do-while loop in main. Is that any different from mine? Can I just follow the template you've provided here but put it in the Input function?
That looks better but you're still taking input in the calcAndDisplay function. I'm thinking remove the code lines55-69. The function is being passed those values.
The only thing odd about the program is that the user could select option C the 1st time, which would leave MilesDriven=0 and GalsUsed=0 from their initial values. I'm not sure how to work around this little problem. Perhaps call Input() once before entering the while loop?
Oops! I meant 55-60.
The (E) option is needed if user doesn't select (Q). He would want to enter new numbers.
Slightly tricky problem. Calling Input once before the loop seems like the easiest solution to guaranteeing numbers are entered before (C) can be selected.