-2
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <conio.h>

using namespace std;

float x, y;
char wybor;

int main()

{
    for (;;) {
        cout << "Liczba 1: ";
        cin >> x;
        cout << "Liczba 2: ";
        cin >> y;
        cout << endl;

        cout << "    MENU" << endl;
        cout << "-------------" << endl;
        cout << "1. Dodawanie" << endl;
        cout << "2. Odejmowanie" << endl;
        cout << "3. Mnozenie" << endl;
        cout << "4. Dzielenie" << endl;
        cout << "5. Wyjdz" << endl;
        cout << "-------------";
        cout << endl
             << ": ";

        wybor = getchar();
        cout << endl;

        switch (wybor) {
        case '1':
            cout << "Wynik: " << x + y << endl
                 << endl;
            break;

        case '2':
            cout << "Wynik: " << x - y << endl
                 << endl;
            break;

        case '3':
            cout << "Wynik: " << x * y << endl
                 << endl;
            break;

        case '4':
            if (y == 0)
                cout << "Pamietaj cholero nie dziel przez zero -_-" << endl
                     << endl;
            else
                cout << "Wynik: " << x / y << endl
                     << endl;
            break;

        case '5':
            exit(0);

            break;

        default:
            cout << "Nie ma takiej opcji!" << endl
                 << endl;
        }
        getchar();
        getchar();
        system("cls");
    }
    return 0;
}

I don't know why the program is not waiting for me to click any button, and automaticcly uses the default case, why is this happening, what I am doing wrong, just how to fix it :O ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

drescherjm
  • 8,907
  • 5
  • 42
  • 60
  • Because you are attempting to mix C++'s `>>` operator with the C library's `getchar()` function. This is undefined behavior. You fix it by using just one programming language's, C's or C++'s input library functions. In which C++ textbook did you learn about `getchar()`? – Sam Varshavchik May 22 '21 at 20:50
  • Basically the same reason as [this](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). `cin >> y` reads the digits and leaves carriage return `\n` character in the stream. `getchar()` then reads that `\n`. You can verify that with `cout << int(wybor)` - this prints the ASCII code of the character in `wybor`; I expect it to print `10`. – Igor Tandetnik May 22 '21 at 20:55
  • I am learning on YT – DaXari YT May 22 '21 at 21:02
  • C++ is the most complex and hardest to learn general purpose programming language used in the world today. Any clown can upload a video to Youtube, or put up a web site that says anything that its owner wants to say. Just because someone claims to be a C++ uberhacker on Youtube doesn't make it true. The only way you'll be able to learn C++ [is from an edited, and proofread, C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik May 22 '21 at 21:07
  • Ok, I will check this out – DaXari YT May 22 '21 at 21:08

0 Answers0