1

I'm a beginner and learing C++. When I try to run the exe file of this code it closes as soon as I press 'enter' after entering the second number. How do I pause my code to see the result? I also tried system("pause"), it worked but I read that it only works on windows and using it is considered BAD PRACTICE, so I want to do it in a proper way. Please help

Print.h (header file I used in the main code)

#pragma once
#include <iostream>
template<typename T> void print(T arg, int newline=1)
{
    if (newline)
        std::cout << arg << std::endl;
    else
        std::cout << arg;
}
#include <iostream>
#include <string>
#include "../Print.h"


void calculator()
{
    print("Enter the first number: ", 0);
    int num1;
    std::cin >> num1;
    print("Choose Operation (+ - x /): ", 0);
    std::string  operation = "";
    std::cin >> operation;
    print("Enter the second number: ", 0);
    int num2;
    std::cin >> num2;
    if (operation == "+") {
        print("Output ==> ", 0);
        print(num1 + num2);
    }
    else if (operation == "-") {
        print("Output ==> ", 0);
        print(num1 - num2);
    }
    else if (operation == "x") {
        print("Output ==> ", 0);
        print(num1 * num2);
    }
    else if (operation == "/") {
        print("Output ==> ", 0);
        print(num1 / num2);
    }
    else {
        print("Error: Invalid Operation!\nPlease try again.");
        calculator();
    }

}

int main()
{
    print("Welcome!\nLets start!!");
    calculator();
    std::cin.get();
}
Meghraj
  • 13
  • 5
  • `cin.get()` reads the newline left in the buffer after the number is converted. – Retired Ninja Apr 20 '23 at 13:35
  • `get` reads a character, if there is a character waiting to be read it will not stop your program. Therefore in your case there **is** a character waiting to be read. What character do you think that is? – john Apr 20 '23 at 13:36
  • Your call to `cin.get()` is running into the same problem that [this issue](https://stackoverflow.com/questions/21567291/) suffers from when calling `std::getline()`. – Remy Lebeau Apr 20 '23 at 21:59
  • @john it's the first number I enter in the calculator. Am I right? – Meghraj Apr 21 '23 at 02:36
  • @Meghraj No, its the enter key that you type after the number. When you read a number it reads the digits that you type, but it does not read anything after the digits, so if you typed enter after the number, then that character is still unread. That is the character that your code reads when you call `get`. – john Apr 21 '23 at 07:00

1 Answers1

1

Here's a possible way to do what you are asking for

std::cin.ignore(INT_MAX, '\n'); // ignore any pending input
std::cin.get();                 // now wait for some new input

What beginners forget is that input can be left behind after you have read something. That left behind input is still there the next time that you read something, it doesn't go away.

You can read about the ignore method here

john
  • 85,011
  • 4
  • 57
  • 81
  • 3
    `INT_MAX` -> `std::numeric_limits::max()` – NathanOliver Apr 20 '23 at 13:40
  • 1
    @NathanOliver I know but, `INT_MAX` is big enough for any practical purposes, and the code is hard enough to explain as it is. – john Apr 20 '23 at 13:42
  • the `std::cin.get()` doesn't wait before it reads the entered carriage return. You can call it twice. But with Visual Studio, it's a simple configuration option, in option / debug / uncheck the close console at end – dalfaB Apr 20 '23 at 13:46
  • @dalfaB I think that should be a separate answer rather than a comment on my answer. – john Apr 20 '23 at 13:49
  • Chuckling, so true, command line input of more than 2 Billion characters (especially for a calculator) is a rare thing indeed (and quite impossible due to the limits imposed on command line length by the OS...) – David C. Rankin Apr 20 '23 at 17:04
  • 1
    `std::numeric_limits::max()` is more than just a large value. It's a special case that **disables** counting of ignored characters. – Blastfurnace Apr 20 '23 at 18:09