Loops

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>
using namespace 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;
}
Change char stillEntering to bool.
That's an issue, but that's not the issue.

Add
#include <string>

and change

char name; to string name;

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.
Last edited on
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>
using namespace 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).
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

int main()
{
   std::string name;

   while (true)
   {
      std::cout << "Enter name or stop: ";
      std::cin >> name;

      if (name == "stop")
         break;  // exit the loop

      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
Thank you @George P, that was what I was trying to make.
Thank you all for the guidance and information too, much appreciated.
An advanced form of a while loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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
As you saw retrieving string input that contains spaces doesn't quite work with std::cin. Use std::getline instead to read the entire line of input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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';
   }
}
Enter name or stop: Fred
Fred
Enter name or stop: Billy Joe Bob
Billy Joe Bob
Enter name or stop: stop

Using std::getline and std::cin together does have other problems you will discover. That's another lesson for later.
1
2
3
4
5
6
#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');
}

Thanks I hate it.
:)
Last edited on
Yes, seeplus, that is possible, but DAYUM! That is using a nuke to swat a fly. My advanced while loop is bad enough.

That for loop is sufficiently weird enough that I filed it away for future coding horror shows. :Þ
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');
}

At least to me it makes it easier to understand the illogic of the loop.

And enclosing the std::cout statement in parentheses is something I should have remembered!

*annoyed grunt!*
Topic archived. No new replies allowed.