I am writing a program using mathematical operators for a book. There was a question (which may have already been asked): how correctly in the if condition, should the b symbol be replaced with ENTER? Tell me please.

#include "stdafx.h" #include <iostream> #include <conio.h> #include <cstdlib> #include <ctime> using namespace std; #define PRINT(STR, VAR) \ cout << STR " = " << VAR << endl; int main() { int i, j, k; float u, v, w; char c = 0; while(true){ cout << "enter an integer: "; cin >> j; cout << "enter another integer: "; cin >> k; PRINT("j", j); PRINT("k", k); i = j + k; PRINT("j + k", i) i = j - k; PRINT("j - k", i) i = k / j; PRINT("k / j", i) i = k * j; PRINT("k * j", i) i = k % j; PRINT("k % j", i) j %= k; PRINT("j %= k", j); _getch(); system("cls"); cout << "Enter a floating-point number: "; cin >> v; cout << "Enter another floating-point number: "; cin >> w; PRINT("v", v); PRINT("w", w); u = v + w; PRINT("v + w", u); u = v - w; PRINT("v - w", u); u = v * w; PRINT("v * w", u); u = v / w; PRINT("v / w", u); u += v; PRINT("u += v", u); u -= v; PRINT("u -= v", u); u *= v; PRINT("u *= v", u); u /= v; PRINT("u /= v", u); _getch(); cout << endl; cout << "Would you like to try again? " << endl; cout << "if YES than press 'b'" << endl; cout << "if NO than press 'q' " << endl; cin >> c; if (c == 'q'){ break; } else if(c == 'b'){ system("cls"); continue; } } _getch(); return 0; } 
  • 2
    "in the condition if, replace the character b with ENTER" - what does this mean? Just put '\n' ? Or are you not about that? - AnT
  • Do the normal formatting and correct the balance of braces. Where do you have a while ? - AnT
  • @AnT, I need to press the symbol (using the ENTER key) to continue the program. In this program, I use the 'b' symbol and the program works fine, but when I try to use the ENTER key, nothing happens, only the transition to the next occurs. string if replaced by '\ n'. I would like, by pressing ENTER, to continue the cycle and run the program from the beginning. About while thanks! - invzbl3
  • See my answer about "nothing happens." - AnT

6 answers 6

Your

 cin >> c; 

refuses to read the Enter key because by default std::cin works in the skip mode of "whitespace" (newline, space, tab, etc.). Turn on noskipws mode and Enter will be perfectly readable.

 std::cin >> std::noskipws >> c; if (c == '\n'){ ... 

This method will not help you to read the space or tabulation, but you can read them just the newline.

  • thank you for clarifying! - invzbl3

If I understand you correctly, the test should be

  if (c == 'q') { break; } 

................

in general, it would be better to write like this

 while(с!='q'){ ...... cin >> c; } 
  • Yes, this is it, only when writing this code, nothing happens to me when I press the "ENTER" key, just a simple transfer to the next lines, but how are you? - invzbl3
  • @invzbl3 stop, you can generally remove else - Lolidze
  • I'm sorry, I didn’t see it that way, apparently, the essence of my question was that during the keystroke request, I’ll ask in my program to press the 'b' symbol to continue, but I would like to replace this symbol with the usual enter key. but when using it, the program ignores enter and simply transfers to the next line. - invzbl3

I would use the non-standard getch() function getch() , because cin>>c will ignore all Entera.

  • Yes? did not know, thank you. - invzbl3
  • Well, ask him not to ignore - and he will stop ignoring. Why go to getch() ? - AnT
 c = getch(); if (c == 13){...} 

key codes

  • 13 Enter
  • 27 escape
  • 8 backspace
  • 32 space
  • Yes, it turned out now, thanks a lot! - invzbl3
  • Not at all, success in learning. - Isin Min
  • Ugliness is incredible. Non-standard function plus some other system-dependent "key codes" ... - AnT
  • one
    AnT, why disgrace? But what about the ASCII table using code 13 for enter-a? I found a question of similar content on the forum- cplusplus.com/forum/beginner/9503 , but cin.get is advised to use the SetConsoleMode () function. - invzbl3
  • @ invzbl3: First, the C and C ++ languages ​​do not know anything about any "ASCII table". Secondly, there can be no key codes in the ASCII table, so there is no "enter code". Thirdly, the code of the newline character in ASCII was always 10, not 13. 13 is the code for the carriage return character. - AnT

It turned out as a result. Thanks to everyone who unsubscribed!

  int main() { int i, j, k; float u, v, w; char c = 0; while(true){ cout << "enter an integer: "; cin >> j; cout << "enter another integer: "; cin >> k; PRINT("j", j); PRINT("k", k); i = j + k; PRINT("j + k", i) i = j - k; PRINT("j - k", i) i = k / j; PRINT("k / j", i) i = k * j; PRINT("k * j", i) i = k % j; PRINT("k % j", i) j %= k; PRINT("j %= k", j); _getch(); system("cls"); cout << "Enter a floating-point number: "; cin >> v; cout << "Enter another floating-point number: "; cin >> w; PRINT("v", v); PRINT("w", w); u = v + w; PRINT("v + w", u); u = v - w; PRINT("v - w", u); u = v * w; PRINT("v * w", u); u = v / w; PRINT("v / w", u); u += v; PRINT("u += v", u); u -= v; PRINT("u -= v", u); u *= v; PRINT("u *= v", u); u /= v; PRINT("u /= v", u); _getch(); cout << endl; cout << "Would you like to try again? " << endl; cout << "if YES than press 'ENTER'" << endl; cout << "if NO than press 'q' " << endl; c = _getch(); if(c == 'q'){ break; } else if (c == 13){ system("cls"); continue; } } _getch(); return 0; } 
  • No way. No need to mindlessly mix C ++ stream I / O, C I / O, and, all the more, non-standard I / O in one program. - AnT
  • This is a book example. I just added if else and the bottom 3 lines of cout << "endl; ... The top cout / cin and macro were used in the example itself. - invzbl3
  • Do not quite understand. If this is a “book example”, then why was there not such a jumble in your original version? - AnT
  • except for the added 3 cout-a and a piece with if else, the rest was given. - invzbl3

Enter code - 13 Just enter 13 in the right place. KappaPride

  • In this case, I did. - invzbl3