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!

/*	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'); 
}

Recommended Answers

All 5 Replies

common mistake :

while (qAnswer = 'y');

should be

while (qAnswer == 'y');

You understand the difference between '=' and '==' right?

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?

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.

okay thanks, that makes sense, now how exactly do i fix that? cause i can't change the char in either situation right?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.