Была новая проблема с функцией while. Как легко звучит, я все еще не могу обвести вокруг себя голову.Хотя функция не работает, как будто я хочу ее
Как и моя последняя программа, этот случай неожиданно закрывается после правильных и неправильных сообщений. Я хочу, чтобы это зациклилось после ввода числа, так что программа не остановится. Спасибо за помощь, если она есть.
#include <iostream>
using namespace std;
int main()
{
int X = 0; //setting the first variable
int num; //setting the second
while (X == 0) //this should happen whenever X is equal to 0
{
cout << "Type a number bigger than 3. "; //output
X++; //This should increase X, so that the next while function can happen
}
while (X == 1) //again, since I increased x by one, (0+1=1 obviously) this should happen
{
cin >> num; //standard input
if (num > 3) //if function: if num is bigger than three, then this should happen
{
cout << "Correct! Try again!" <<endl; //output
X--; //Here I'm decreasing x by one, since it was 1 before, now it becomes 0. This should make the "while (X == 0)" part happen again, so that another number bigger than three can be entered
}
if (num <= 3) //if function: if num is lesser than or equal to 3, this should happen
{
cout << "Wrong! Try again!" <<endl; //output
X--; //This is supposed to work like the "X--;" before, repeating the code from "while (X==0)"
}
}
}
использовать замкнутый цикл? – Chantola
Вы изменяете X независимо от ввода –
и где возвращается 0 ;? – Chantola