-1

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.

Here is photo of problem.

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;
}

Marek
  • 95
  • 1
  • 13
  • Why are you mostly using plain c code with c++?? – πάντα ῥεῖ Dec 04 '22 at 12:55
  • Because I don't know what am I doing :D I am trying to copy my professor – Marek Dec 04 '22 at 13:02
  • Ok, I will check and recreate this to "correct c++". But may I ask, what can be causing this problem I am facing? It is because I am mixing two languages? – Marek Dec 04 '22 at 13:09
  • Extracting values from an `istream` like with `std::cin` leaves everything in the stream buffer besides the characters needed to build the value. So `'\n'` will be left. Basically you have a duplicate of [this Q&A](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction?r=Saves_AllUserSaves) here. – πάντα ῥεῖ Dec 04 '22 at 13:15

1 Answers1

0

Thanks to @πάντα ῥεῖ, I was able to fix my problem.

Here is new, working code:

#include <iostream>

using namespace std;

int temp = 20;
int oldTemp = 20;
int min_temp = 10;
int max_temp = 35;
int temp_input;
char input;
string relay = "vypnute";

int main(int argc, const char * argv[]) {
    cout << "Regulator teploty Turbo 3000. \nAktualna teplota je nastavena na " << temp << " stupnov celzia, rele je " << relay << ".\nStlacte tlacidlo 'i' pre viac informacii.\nZadajte poziadavku: ";
    while(input != 'q'){
        cin >> input;
        cin.ignore();
        switch(input){
            case 'i':
                if(temp != oldTemp){
                    relay = "zapnute";
                }
                cout << "Aktualne je nastavena teplota na " << temp << " stupnov celzia, rele je " << relay << ".\n'i' -> Info \n't' -> Zadanie teploty \n'+' -> Zvysenie teploty o 1 stupen celzia \n'-' -> Znizenie teploty o jeden stupen celzia \n'q' -> Ukoncenie programu 'Regulator teploty Turbo 3000'\nZadajte poziadavku: ";
                break;
            case 't':
                cout << "Zadajte pozadovanu teplotu v rozmedzi od " << min_temp << " stupnov celzia do " << max_temp << " stupnov celzia.\nTeplota: ";
                cin >> temp_input;
                cin.ignore();
                if(temp_input > max_temp || temp_input < min_temp){
                    cout << "Zadali ste teplotu mimo povolenych hodnot. Zadajte hodnotu v rozmedzi od " << min_temp << " stupnov celzia do " << max_temp << " stupnov celzia.\n Teplota: ";
                }
                temp_input != temp ? relay = "zapnute" : relay = "vypnute";
                cout << "Teplota bude regulovana z povodnych " << temp << " stupnov celzia na pozadovanych " << temp_input << " stupnov celzia, rele je " << relay << ".\nZadajte poziadavku: ";
                temp = temp_input;
                break;
            case '+':
                if(temp + 1 > max_temp){
                    cout << "Teplota nemoze byt vyssia ako " << max_temp << " stupnov celzia.\nZadajte poziadavku: ";
                    break;
                }
                relay = "zapnute";
                cout << "Teplota bude regulovana z povodnych " << temp << " stupnov celzia na pozadovanych " << temp + 1 << " stupnov celzia, rele je " << relay << ".\nZadajte poziadavku: ";
                temp++;
                break;
            case '-':
                if(temp - 1 < min_temp){
                    cout << "Teplota nemoze byt nizsia ako " << min_temp << " stupnov celzia.\nZadajte poziadavku: ";
                    break;
                }
                relay = "zapnute";
                cout << "Teplota bude regulovana z povodnych " << temp << " stupnov celzia na pozadovanych " << temp - 1 << " stupnov celzia, rele je " << relay << ".\nZadajte poziadavku: ";
                temp--;
                break;
            default:
                cout << "Neznama poziadavka. Zadajte 'i' pre viac informacii.\nZadajte poziadavku: ";
        }
    }
    oldTemp == temp ? relay = "vypnute" : relay = "zapnute";
    cout << "Dakujeme za pouzivanie programu Regulator Turbo 3000.\nTeplota je nastavena na " << temp << " stupnov celzia, rele je " << relay << ".\nStlacte 'enter' pre ukoncenie aplikacie.\n";
    cin.get();
    return 0;
}


Marek
  • 95
  • 1
  • 13