James Allingham
James Allingham

Reputation: 726

Comparing Doubles and Chars in C++

I am trying to write a simple program in C++ that reads in an unspecified number of marks, then once the user inputs the character 'q', the program must calculate and display the average mark. However I am having some trouble. The approach I am taking is to save each value as a double, the I want to compare the double to the character 'q' and if they are the same character, end the loop, calculate and display the average.

However I think that the comparison between the char value 'q' and double value for the mark seem to be incomparable. This worked for me when I did the same using integer values for the mark but not doubles it seems. Any help would be appreciated.

Here is the code:

int main()
{
    cout << "Please enter any number of marks. Enter 'q' to stop." << endl;
    double total = 0;
    int counter = 0;
    bool repeat = true;
    do
    {
        double mark;
        cin >> mark;

        if (mark != 'q')
        {
            total += mark;
            counter++;
        }
        else
        {
            repeat = false;
        }

    }
    while (repeat == true);

    double average = total/counter;
    cout << "Average: " << average << endl;

    return 0;
}

Upvotes: 0

Views: 1325

Answers (4)

Sriram
Sriram

Reputation: 150

you can not cast a double to char. You may need to use additional c++ library functions which convert string (char*) to double. There are different ways to do this. Try this :

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    cout << "Please enter any number of marks. Enter 'q' to stop." << endl;
    double total = 0;
    int counter = 0;
    bool repeat = true;
    do
    {
        char userinput[8];
        cin >> userinput;


    std::stringstream ss;
    double mark;

        if (userinput[0] != 'q')
        {
            ss << userinput;
            ss >> mark;
            total += mark;
            counter++;
        }
        else
        {
            repeat = false;
        }

    }
    while (repeat == true);

    double average = total/counter;
    cout << "total : " << total  << " count : " << counter << endl;
    cout << "Average: " << average << endl;

    return 0;
}

Upvotes: 1

user3356152
user3356152

Reputation:

Typecast double to int and then compare, It must work because it compares ASCII value of character

Here is the code:

 int main()
{
cout << "Please enter any number of marks. Enter 'q' to stop." << endl;
double total = 0;
int counter = 0;
bool repeat = true;
do
{
    double mark;
    cin >> mark;

    if ((int)mark != 'q')
    {
        total += mark;
        counter++;
    }
    else
    {
        repeat = false;
    }

}
while (repeat == true);

double average = total/counter;
cout << "Average: " << average << endl;

return 0;
}

Upvotes: 2

HelloWorld123456789
HelloWorld123456789

Reputation: 5369

You are doing it wrong. If you try to cin a char into a double variable, the input char stays in the input buffer and the double variable remains the same. So this will end in an infinite loop.

If you really want the user to enter a char to end input, you need to take the whole input in a string variable. Check the string for q. If not present, use atof() to convert it to double.

Upvotes: 0

Roger Rabbit
Roger Rabbit

Reputation: 98

you'll need to change the mark variable to string, and then compare it to 'q', else try to parse is as a number.

otherwise this entire code, does not make a lot of sense, because 'q' in ASCII is 113, which I guess is a possible value

Upvotes: 2

Related Questions