| | |
Functions with two parameters
![]() |
•
•
Join Date: Jul 2009
Posts: 9
Reputation:
Solved Threads: 0
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)
/* Write a program that prompts the user for two numbers – the dividend and the divisor – and then displays the division of the dividend by the divisor. Your function must be called displayDivision and will check that the divisor is not zero before attempting the division. If the divisor is zero, your function will display the error message shown below. Your program will then ask the user if s/he wants to continue. If the user enters a 'y' (either upper or lower case), continue to ask the user for the next dividend and divisor. Use the screen shot below as a guide. */ #include <iostream> #include <string> #include <cmath> using namespace std; double displayDivision(double dividend, double divisor) { if(divisor != 0) { double divisionAnswer; divisionAnswer = dividend/divisor; cout << divisionAnswer << endl; return divisionAnswer; } else { cout << "Error: Attempt to divide by zero!" << endl; return 0; } } int main() { char qAnswer; do { double dividend, divisor; cout << "Enter the dividend: "; cin >> dividend; cout << "Enter the divisor: "; cin >> divisor; displayDivision(dividend, divisor); char qAnswer; cout << "Do you want to continue (y/n)? "; cin >> qAnswer; } while (qAnswer = 'y'); }
common mistake :
should be
You understand the difference between '=' and '==' right?
C++ Syntax (Toggle Plain Text)
while (qAnswer = 'y');
C++ Syntax (Toggle Plain Text)
while (qAnswer == 'y');
You understand the difference between '=' and '==' right?
•
•
Join Date: Aug 2008
Posts: 90
Reputation:
Solved Threads: 20
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.
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.
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.
![]() |
Similar Threads
- Tic-Tac-Toe (C++)
- Simple Functions , Parameters and Return Question (Python)
- Converting Functions (Assembly)
- C++ Functions (C++)
- Functions Parameters (C++)
- Windows Main and Command Line Parameters (C)
- need links wher i can find c++ codes lots of them!!! (C++)
- Getting started in programming (Computer Science)
- Objects (C)
Other Threads in the C++ Forum
- Previous Thread: First Time With Traversals
- Next Thread: several problems
Views: 335 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm array arrays assignment beginner binary c++ c/c++ calculator char class classes client code command compile compiler console constructor conversion convert count delete dll dynamic encryption error file files fstream function functions game givemetehcodez graph gui homework http iamthwee ifstream input int lazy linker list loop loops math matrix member memory multidimensional network newbie number numbers object objects opengl operator output parameter pointer pointers problem program programming project qt random read recursion recursive reference simple sort spoonfeeding string strings struct student studio system template templates text time tree variable vc++ vector video visual visualstudio void win32 window windows winsock xml






