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

Dani AI

Generated

— two different things were biting your program. correctly pointed out that an assignment used where a comparison is intended will make the loop condition always true when the assigned char is nonzero. correctly explained the scope problem: declaring another response variable inside the do block hides the one the while condition tests, so that outer variable can be uninitialized when evaluated.

Practical fixes and hardening: declare the response exactly once in the scope that the loop condition uses, and do not redeclare it inside the body. Prefer reading the reply as a string (safer than a single char) and normalize case before testing. Always validate numeric input after cin >> (clear the failbit and discard the rest of the line on error) so invalid input does not leave the stream in a bad state. Also decide whether your division helper should print results or return them; returning status + result (or std::optional in modern C++) makes error handling clearer than returning a sentinel value.

A concise, robust pattern (single response variable, input checks, case normalization, and a clear division contract):

#include <iostream>
#include <string>
#include <limits>
#include <cctype>

bool safeDivide(double a, double b, double &out)
{
    if (b == 0.0) return false;
    out = a / b;
    return true;
}

int main()
{
    std::string answer;
    do {
        double dividend, divisor, result;
        std::cout << "Enter the dividend: ";
        if (!(std::cin >> dividend)) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid input — try again.\n";
            continue;
        }
        std::cout << "Enter the divisor: ";
        if (!(std::cin >> divisor)) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid input — try again.\n";
            continue;
        }

        if (safeDivide(dividend, divisor, result))
            std::cout << "Result: " << result << '\n';
        else
            std::cout << "Cannot divide by zero.\n";

        std::cout << "Continue (y/n): ";
        std::cin >> answer;
        if (!answer.empty()) answer[0] = std::tolower(static_cast<unsigned char>(answer[0]));
    } while (!answer.empty() && answer[0] == 'y');
}

This avoids the shadowing/initialization trap and improves input and error handling.

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.