I am totally new to c++ and I am working on small console project for school. I created something like Thermostat. When you run that app, It will ask for user input.
i - informations
t - temperature settings
q - quit
+ - add to temperature by 1
- - remove from temperature by 1
Everything is working for me, I can press 'i', I can press +/- and everything is ok. But when I press 't', enter some temperature and confirm, I will receive corrent response. But now, whatever I will press, it will get into 'default' part of switch. Why is this happening to me? Then I press just 'enter', then again 't' and it is working again.
As you can see, after I put '18', I pressed enter. There is response that Temperature will be set to 18 and relay is on. If I want to close app, I should press 'q' or I can continue in setting new temperature. Whatever I press it will show: "Zly vstup. Stlacte tlacidlo 'i' pre viac informacii" and that is from 'default' part of switch. I can press anything, all the time this will be shown until I press 'enter', then I can enter 't' again and it is working.
Why is this happening?
Here is code:
#include <iostream>
#include <string>
using namespace std;
int input;
int temp = 20;
int oldTemp = 20;
string relay = "vypnute";
int main() {
printf("Regulator teploty Turbo 3000 \nAktualna teplota: %d stupnov celzia, rele je %s. \nStlacte tladiclo 'i' pre informacie: ", temp, relay.c_str());
while(input != 'q'){
input = getchar();
getchar();
switch(input){
case 'i': case 'i ':
if(temp != 20){
relay = "zapnute";
}
printf("Aktualne je nastavena teplota na %d stupnov celzia, rele je %s. \n", temp, relay.c_str());
printf(" i Info \n\ t Zadanie teploty od 10 do 35 stupnov \n\ + Zvysenie teploty o 1 stupen \n\ - Znizenie teploty o 1 stupen \n\ q Koniec programu \n");
break;
case 't': case 't ':
printf("Zadajte pozadovanu teplotu: ");
cin >> input;
if(input > 35 || input < 10){
printf("Zadajte teplotu v rozmedzi od 10 do 35 stupnov. \nStlacte 't' pre zadanie novej teploty. ");
break;
}
if(input == temp){
relay = "vypnute";
}
else{
relay = "zapnute";
}
printf("Teplota bude nastavena z povodnych %d na pozadovanych %d stupnov, rele je %s.",temp, input, relay.c_str());
temp = input;
printf("\nAk chcete ukoncit regulator, stlacte 'q', inak pokracujte v nastavovani teploty. ");
break;
case '+': case '+ ':
if(temp+1 > 35){
printf("Teplota nemoze byt vyssia ako 35 stupnov. ");
break;
}
relay = "zapnute";
printf("Teplota bola zvysena z povodnych %d na pozadovanych %d stupnov, rele je %s. ",temp, temp+1, relay.c_str());
temp++;
printf("\nAk chcete ukoncit regulator, stlacte 'q', inak pokracujte v nastavovani teploty. ");
break;
case '-': case '- ':
if(temp-1 < 10){
printf("Teplota nemoze byt mensia ako 10 stupnov. ");
break;
}
relay = "zapnute";
printf("Teplota bola znizena z povodnych %d na pozadovanych %d stupnov, rele je %s. ",temp, temp-1, relay.c_str());
temp--;
printf("\nAk chcete ukoncit regulator, stlacte 'q', inak pokracujte v nastavovani teploty. ");
break;
case 'q': case 'q ':
break;
default:
printf("Zly vstup. Stlacte tlacidlo 'i' pre viac informacii. ");
break;
}
}
if(temp == oldTemp){
relay = "vypnute";
}
else{
relay = "zapnute";
}
printf("Dakujeme za pouzivanie Regulatora Turbo 3000. \nTeplota je po novom nastavena na %d stupnov celzia a rele je %s. ", temp, relay.c_str());
printf("Stlacte 'enter' pre ukoncenie aplikacie.");
getchar();
return 0;
}