Infinite loop in console

I'm new at programming and I got to do this for homework at school. So my problem is when I go into the console, I want my program to loop if I enter "oui" as x. The problem is that if I enter "oui", the program keeps looping alone for eternity and it doesn't let me enter new value.

Sorry for my bad english.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream> 
using namespace std; 

int main()

{

	double F,/*Fahrenheit*/ C; /*Celsius*/
	bool x;
	int oui(0);
	int Oui(0);

	do {
		cout << "Entrer la température en Fahrenheit:" << endl;
		cin >> F;

		C = ((F - 32) * 5) / 9; /*Formule pour calculer en Celsius*/

		cout << "La température en celsius est: " 
			<< endl << C << " degrés Celsius" << endl;
		cout << endl;

		cout << "Voulez-vous continuer? oui / non" << endl;
		cin >> x;
		
       }

   while (x = oui || x != Oui);
	
	
	system("pause");
	return 0;

}
x is bool. It accepts only 1 and 0 (or true and false if boolalpha is on)
You can: change locale and set boolalpha so it would accept oui / non as valid boolean values, or just make x a string and compare strings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<string>
#include<cctype>

int main()
{
    std::string x;
    do {
        std::cout << "Entrer la température en Fahrenheit:\n";
        double F;
        std::cin >> F;
        double C = ((F - 32) * 5) / 9; /*Formule pour calculer en Celsius*/
        std::cout << "La température en celsius est:\n" << C << " degrés Celsius\n\n"
                     "Voulez-vous continuer? oui / non\n";
        std::cin >> x;
        for(auto& c: x)
            c = std::tolower(c);
    } while (x == "oui");
}

Edit: example of using locales to make input accept alternative spellings of true and false:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <locale>

struct french_bool : std::numpunct<char> {
    string_type do_truename() const { return "oui"; }
    string_type do_falsename() const { return "non"; }
};

int main()
{
    bool b;
    std::cin.imbue(std::locale(std::cin.getloc(), new french_bool));
    std::cin >> std::boolalpha >> b; //Accepts french boolean names
    std::cout << std::boolalpha << b; //Not imbued with french locale, outputs endlish names
}
Last edited on
Topic archived. No new replies allowed.