-2

I need help with closing the program by entering 'quit'

for example.

while(true)
{
  cout << "enter a name" << endl;
  std::getline (std::cin,input);
  if(input =='quit')
  {
    break;
  }
}

it is not breaking out or quiting, also how come you can't compare a string to a int?

i.e. : while (input != 'quit') <<-- that won't work also.

bames53
  • 86,085
  • 15
  • 179
  • 244
V Kid
  • 117
  • 4
  • 12
  • 3
    What language is this? – Troubleshoot Oct 09 '13 at 15:36
  • 1
    `can't compare a string to a int`... Why can't you compare apples to oranges? They are different things. You can either attempt to *convert* your string to an int and compare or vice versa. – Eric J. Oct 09 '13 at 15:40
  • but I also need my input as a string because I will be comparing it with other variables later in the code.....is there a way to 'quit' program without converting to string? – V Kid Oct 09 '13 at 15:41
  • 1
    I deleted my answer because you keep editing the question such that it completely changes the original question. Please don't do that. – Eric J. Oct 09 '13 at 15:45

1 Answers1

1

quit needs to be in double quotes to be a string:

#include <iostream>

int main()
{
    std::string input;
    while (true)
    {
        std::cout << "enter a name: ";
        std::getline(std::cin, input);
        if (input == "quit")
        {
            break;
        }
    }
    std::cout << "Broken" << std::endl;
}

See it run.

also how come you can't compare a string to a int.

Because this behaviour isn't defined by the standard. Would "1.0" be equal to 1?

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 3
    Welcome to Version 3 of his question. Hopefully it doesn't change again invalidating your answer :-) – Eric J. Oct 09 '13 at 15:46
  • 1
    @user2855990 Since you've got an answer to this question now, you should delete the older version of your question. Don't forget to accept this answer, too - click the check mark next to the answer to accept. – Sergey Kalinichenko Oct 09 '13 at 16:26