| | |
Functions with two parameters
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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: 83
Reputation:
Solved Threads: 18
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
- Simple Functions , Parameters and Return Question (Python)
- Converting Functions (Assembly)
- C++ Functions (C++)
- Functions Parameters (C++)
- Tic-Tac-Toe (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
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates text text-file tree url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






