I'm trying to write a program that repeats a block of code as long as the user indicates they want to. I've been trying to make one using this code but I keep getting "[Error] ISO C++ forbids comparison between pointer and integer".
Error is the "stop" in if condition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main() {
char name;
char stillEntering = true;
cout << "Enter name or stop: ";
cin >> name;
while (stillEntering) {
cout << name;
if (name == "stop")
stillEntering = false;
}
return 0;
}
A char is a single character (byte). A string is a... string... of characters (like an array with some bells and whistles).
Third issue: You never ask for user input within your loop, so your loop will either only run once, or be infinite.
Fourth issue: Line 12 should be indented by one more level.
Thanks, those fixed the error. I need help with one more thing tho.
Now when I enter a name it goes into an infinite loop. How do I make it that whenever I enter a name it shows the name then asks me for input again till I input stop then it stops?
name is a single char and "stop" is a string. You cannot compare them (like this). You probably want this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main() {
char name[100]; // Reasonable large c-string
char stillEntering = true;
cout << "Enter name or stop: ";
cin >> name;
while (stillEntering) {
cout << name;
if (0 == strcmp(name, "stop")) // Note: do not compare c-string directly. This would be a pointer comparison not the content.
stillEntering = false;
}
return 0;
}
. How do I make it that whenever I enter a name it shows the name then asks me for input again till I input stop then it stops?
You never ask the user for more input. Use cin >> name within your loop (and use cout to let the user know they need to enter something).
Consider a do-while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
do
{
cout << "Enter name: ";
string name;
cin >> name;
if (name == "stop")
{
stillEntering = false;
}
else
{
cout << "Hello, " << name << '\n';
}
} while (stillEntering);
If you want to use name outside of the loop, then you should probably use two different variables (one for the input, and one to store the name itself).
Enter name or stop: Fred
Fred
Enter name or stop: Manny
Manny
Enter name or stop: Billy Joe Bob
Billy
Enter name or stop: Joe
Enter name or stop: Bob
Enter name or stop: Stop
Stop
Enter name or stop: STOP
STOP
Enter name or stop: stop
#include <iostream>
#include <string>
int main()
{
std::string name;
while (std::cout << "Enter name or stop: " &&
std::cin >> name &&
name != "stop")
{
std::cout << name << '\n';
}
}
Enter name or stop: Fred
Fred
Enter name or stop: Manny
Manny
Enter name or stop: Billy Joe Bob
Billy
Enter name or stop: Joe
Enter name or stop: Bob
Enter name or stop: Stop
Stop
Enter name or stop: STOP
STOP
Enter name or stop: stop
#include <iostream>
#include <string>
int main()
{
std::string name;
while (true)
{
std::cout << "Enter name or stop: ";
// retrieve ALL the input, including spaces
// https://www.cplusplus.com/reference/string/string/getline/
std::getline(std::cin, name);
if (name == "stop")
break; // exit the loop
std::cout << name << '\n';
}
}
#include <iostream>
#include <string>
int main() {
for (std::string name; (std::cout << "Enter name or stop to terminate: ") && std::getline(std::cin, name) && name != "stop"; std::cout << name << '\n');
}
For purposes of being semi-readable I might add some judicious whitespace:
1 2 3 4 5 6 7 8 9
#include <iostream>
#include <string>
int main()
{
for (std::string name;
(std::cout << "Enter name or stop to terminate: ") && std::getline(std::cin, name) && name != "stop";
std::cout << name << '\n');
}