1

I got a little problem with my code.. My do-while loop doesn't exit when i give 0 to symbol? I don´t know why because i'm new with c++.. I think that loop needs to quit when the symbol=0?

#include "stdafx.h"  
#include <iostream>
using namespace std;

int main(){
char symbol;
double num1, num2;
do {
    cout << "Give the operator (+,-,*,/,0 = quit) : ";
    cin >> symbol;

    cout << "Number 1 : ";
    cin >> num1;

    cout << "Number 2 : ";
    cin >> num2;

    if (symbol == '+')
        cout << "Addition : " << num1 + num2 << endl;

    else if (symbol == '-')
        cout << "Subtraction : " << num1 - num2 << endl;

    else if (symbol == '*')
        cout << "Multiplication: " << num1 * num2 << endl;

    else if (symbol == '/')
        cout << "Division : " << num1 / num2 << endl;

} while (symbol != 0);

cout << "See you";
system("pause");

return 0;}
6
  • 5
    You probably want '0', not 0. Commented Feb 6, 2018 at 14:29
  • 2
    symbol is a character. Now how is the digit 0 represented as a char? Commented Feb 6, 2018 at 14:29
  • Typo. 0 is a number, '0' is a character that you would enter Commented Feb 6, 2018 at 14:29
  • As for your probable next question: The newline after you input num2 will be left in the input buffer, to be read by the next cin >> symbol. You need to ignore everything up to the next newline in your input after the last input in the loop. Commented Feb 6, 2018 at 14:31
  • 1
    Even after fixing 0 with '0' your loop won't exit unless you input number 1 and number 2. I suggest you rethink your solution and add break after you enter symbol like this: std::cin >> symbol; if (symbol == '0') break; Commented Feb 6, 2018 at 14:36

2 Answers 2

6

Try changing to while (symbol != '0');

In the original version, the comparison was to the number 0 rather than to the symbol '0'. Since your input was of type char the condition would never be met.

Sign up to request clarification or add additional context in comments.

2 Comments

You may want to consider explaining why. (Even though it may seem obvious)
Thank you for your feedback. I'm kind of new to this and you are absolutely right that an explanation of why would be a much more useful answer.
1

You are inputting a coded character into symbol, not an integer. When you cin into a char, it will capture a coded character from the user rather than a numerical value ze inputs.

cin >> symbol;

What is a coded character?

According to Wikipedia, "code is a system of rules to convert information—such as a letter, word, sound, image, or gesture—into another form or representation." There are many different ways of representing glyphs used in human language, such as numerals, as numbers inside a computer. These ways are called character sets or encodings. Examples of character sets are SHIFT-JIS, ASCII, Unicode, and EBCDIC. These encodings are able to state that a glyph, say A, is equal to the number 65. It does so with every glyph it supports so that each glyph has a unique number.

How does this relate to my issue?

Well... assuming that your environment uses the ASCII character set, and your user inputs a 0, the number your program is going to receive is NOT going to be 0. Instead, it is going to be 48. This is an issue because your program is checking for the number 0. Please note that different environments can have different character encodings with different values for different glyphs. Most character sets (but not all!) tend to agree with ASCII on the basic Latin alphabet and Arabic numerals, however.

} while (symbol != 0);

How do I fix this?

The solution is very simple. All you have to do is surround the zero you are checking for with single quotes. This will cause your program to check symbol for the coded representation of the zero glyph your user input rather than the numerical value of zero.

} while (symbol != '0');

What else do I need to know?

The char data type actually isn't that special. It's a number just like the rest of the data types. It was intended for storing character values, however. Variables of type char are one byte wide. If a byte is 8 bits, that means that a signed char can store any number from -128 to 127. You can treat char variables exactly the same as any other scalar integer type in C++. So what your char is actually holding is just a number. Nothing more, nothing less.

The definition of code came from https://en.wikipedia.org/w/index.php?title=Code&oldid=823846736.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.