Functions with two parameters

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 9
Reputation: qkslvr1621 is an unknown quantity at this point 
Solved Threads: 0
qkslvr1621 qkslvr1621 is offline Offline
Newbie Poster

Functions with two parameters

 
0
  #1
Jul 30th, 2009
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!



  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,116
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 142
firstPerson's Avatar
firstPerson firstPerson is online now Online
Veteran Poster

Re: Functions with two parameters

 
0
  #2
Jul 30th, 2009
common mistake :
  1. while (qAnswer = 'y');
should be
  1. while (qAnswer == 'y');

You understand the difference between '=' and '==' right?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: qkslvr1621 is an unknown quantity at this point 
Solved Threads: 0
qkslvr1621 qkslvr1621 is offline Offline
Newbie Poster

Re: Functions with two parameters

 
0
  #3
Jul 30th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 83
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: Functions with two parameters

 
0
  #4
Jul 30th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: qkslvr1621 is an unknown quantity at this point 
Solved Threads: 0
qkslvr1621 qkslvr1621 is offline Offline
Newbie Poster

Re: Functions with two parameters

 
0
  #5
Jul 30th, 2009
okay thanks, that makes sense, now how exactly do i fix that? cause i can't change the char in either situation right?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,116
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 142
firstPerson's Avatar
firstPerson firstPerson is online now Online
Veteran Poster

Re: Functions with two parameters

 
0
  #6
Jul 30th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC