i"am learning c and decided to make a simple calculator and when i compiled the project and ran it all it showed is a black screen. Btw im running MS VSCode 2022. Thanks in advance for help! :) `
#include <iostream>
using namespace std;
int main() {
int konec = 0;
double cislox = 0;
double cisloy = 0;
int operace = 0;
double vysledek1 = 0;
double vysledek2 = 0;
double vysledek3 = 0;
double vysledek4 = 0;
while (konec == 0); {
cout << "Vítejte v kalkulačce Ver. 0.1" << endl;
cout << "stiskněte 1 pro sčítání, 2 pro odčítání, 3 pro násobení, 4 pro dělení" << endl;
cin >> operace;
switch (operace) {
case 1:
cout << "Zadejte číslo X" << endl;
cin >> cislox;
cout << "Zadejte číslo Y" << endl;
cin >> cisloy;
vysledek1 = cislox cisloy;
cout << cislox << " " << cisloy << "=" << vysledek1;
break;
case 2:
cout << "Zadejte číslo X" << endl;
cin >> cislox;
cout << "Zadejte číslo Y" << endl;
cin >> cisloy;
vysledek2 = cislox - cisloy;
cout << cislox << "-" << cisloy << "=" << vysledek2;
break;
case 3:
cout << "Zadejte číslo X" << endl;
cin >> cislox;
cout << "Zadejte číslo Y" << endl;
cin >> cisloy;
vysledek3 = cislox * cisloy;
cout << cislox << "*" << cisloy << "=" << vysledek3;
break;
case 4:
cout << "Zadejte číslo X" << endl;
cin >> cislox;
cout << "Zadejte číslo Y" << endl;
cin >> cisloy;
vysledek2 = cislox / cisloy;
cout << cislox << "/" << cisloy << "=" << vysledek4;
break;
}
cout << "chcete ukoncit program?" << "1 = ANO; 0 = NE" << endl;
cin >> konec;
}
return 0;
}
`
Well, i tried cheking all the code, but there should be no mistakes. Maybe im compiling it wrong? (im a MS VSCode user only for a while)
CodePudding user response:
you put a ;
after the while(konec)
. Thats why the loop is executed but it does nothing.
As mentioned in the comment a while(konec == 0); { //do stuff }
is equal to a empty while loop while(konec == 0) {}
CodePudding user response:
There is a typo in the while loop:
while (konec == 0); {
The ;
on there makes it an infinite while loop that does not execute any code, since it's empty. The correct way should be:
while (konec == 0) {