I made a C++ user input that detects when no value is being input, but it terminates the program on startup without outputting anything. Why?
#include <iostream>
int main() {
bool entered = false;
while(entered = false) {
std::cout << "Please enter thy name: ";
std::string name;
std::getline(std::cin, name);
std::cout << "Hello, " + name;
int length = name.length();
if(length > 0) {
std::cout << "Hello, " + name;
entered = true;
} else {
std::cout << "Thou did not enter thy name";
}
}
}
I already made a similar program in Java with a similar format and that one works fine.
This loop condition:
while(entered = false)
is wrong. Instead of comparing false
and entered
, you are assigning false
to entered
.
Instead, you need to do:
while(entered == false)
If you turn on warnings, for example with -Wall
, the compiler will tell you that you are likely making a mistake here.