943,816 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 483
  • C++ RSS
Jul 30th, 2009
0

Functions with two parameters

Expand Post »
here is what i have so far, it compiles and runs great except for the fact that when i enter 'n' it doesn't stop the loop, any help is greatly appreciated!



C++ Syntax (Toggle Plain Text)
  1. /* Write a program that prompts the user for two
  2. numbers – the dividend and the divisor – and
  3. then displays the division of the dividend by
  4. the divisor. Your function must be called
  5. displayDivision and will check that the divisor
  6. is not zero before attempting the division. If
  7. the divisor is zero, your function will display
  8. the error message shown below. Your program will
  9. then ask the user if s/he wants to continue. If
  10. the user enters a 'y' (either upper or lower case),
  11. continue to ask the user for the next dividend and
  12. divisor. Use the screen shot below as a guide.
  13. */
  14.  
  15.  
  16. #include <iostream>
  17. #include <string>
  18. #include <cmath>
  19.  
  20. using namespace std;
  21.  
  22. double displayDivision(double dividend, double divisor)
  23. {
  24. if(divisor != 0)
  25. {
  26. double divisionAnswer;
  27. divisionAnswer = dividend/divisor;
  28. cout << divisionAnswer << endl;
  29. return divisionAnswer; }
  30. else { cout << "Error: Attempt to divide by zero!" << endl;
  31. return 0; }
  32. }
  33. int main()
  34. {
  35. char qAnswer;
  36. do
  37. { double dividend, divisor; cout << "Enter the dividend: ";
  38. cin >> dividend;
  39. cout << "Enter the divisor: ";
  40. cin >> divisor;
  41. displayDivision(dividend, divisor);
  42. char qAnswer;
  43. cout << "Do you want to continue (y/n)? ";
  44. cin >> qAnswer;
  45. }
  46. while (qAnswer = 'y');
  47. }
Similar Threads
Reputation Points: 21
Solved Threads: 0
Newbie Poster
qkslvr1621 is offline Offline
9 posts
since Jul 2009
Jul 30th, 2009
0

Re: Functions with two parameters

common mistake :
C++ Syntax (Toggle Plain Text)
  1. while (qAnswer = 'y');
should be
C++ Syntax (Toggle Plain Text)
  1. while (qAnswer == 'y');

You understand the difference between '=' and '==' right?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Jul 30th, 2009
0

Re: Functions with two parameters

yes i do, silly mistake.

but it compiles fine once again but when i run it, and put 'y' or 'n', either one gives me this error

Run-Time Check Failure #3 - The variable 'qAnswer' is being used without being initialized.

any advice?
Reputation Points: 21
Solved Threads: 0
Newbie Poster
qkslvr1621 is offline Offline
9 posts
since Jul 2009
Jul 30th, 2009
0

Re: Functions with two parameters

You're having a problem with scope. You declared it in the main function, as well as within the do statement. Therefore when you set it in the do statement it will set the variable in the do statement, but will not set the variable in the main function. In the while statement, it will attempt to read the main function variable.
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Jul 30th, 2009
0

Re: Functions with two parameters

okay thanks, that makes sense, now how exactly do i fix that? cause i can't change the char in either situation right?
Reputation Points: 21
Solved Threads: 0
Newbie Poster
qkslvr1621 is offline Offline
9 posts
since Jul 2009
Jul 30th, 2009
0

Re: Functions with two parameters

Your problem is the variable qAnswer. you declare it before the
do while loop and inside it as well.

This code : while(qAnswer == 'y') uses the qAnswer before the
do while loop and not the one inside the loop. Thats because
the qAnswer variable thats inside the loop goes out of scope
before the condition, while(qAnswer == 'y') , is evaluated.

To fix you problem first initialize the qAnswer variable :

like so : char qAnswer = 'y';

Then delete the qAnswer inside the do while loop because thats
not needed. Your loop will use the one before the do while loop.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: First Time With Traversals
Next Thread in C++ Forum Timeline: several problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC