5 Replies - 234 Views - Last Post: 14 October 2012 - 06:35 PM Rate Topic: -----

#1 MegaTerminatorz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-October 12

Trouble with While loop.

Posted 12 October 2012 - 02:16 PM

#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string i;
    string Hotdog;
    string Milkshake;
    string Hamburger;
    while (i == ( "Hotdog" || "Milkshake" || "Hamburger" ) )
    {
    cout << "Enter 'Hotdog,' 'Milkshake,' or 'Hamburger.'";
    cin >> i;
    }
}


I'm sorry. I accidently posted, while I was trying to type.

I'm trying to make a code so that the user must input "Hotdog," "Milkshake," or "Hamburger" to continue, but the compiler says:

error: not match for 'operator==' in 'i == true'

What can I do to fix this?

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with While loop.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Trouble with While loop.

Posted 12 October 2012 - 02:26 PM

First in future, if you get compiler errors, post the complete error messages exactly as they appear in your development environment. These messages have important information embedded within them to aid in locating and fixing these errors.

Now one problem I see is this line:
while (i == ( "Hotdog" || "Milkshake" || "Hamburger" ) )

In C/C++ you can't chain conditionals like this. You need both sides of the equation for each statement. For example:
while( i == "Hotdog" || i = "Milkshake")


Jim
Was This Post Helpful? 1
  • +
  • -

#3 MegaTerminatorz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-October 12

Re: Trouble with While loop.

Posted 12 October 2012 - 02:33 PM

Thank you so much, Jim. I'll be sure to do that for next time, and I have another question.

I fixed the code, and ran it, but it comes up as empty. I think it just skips the loop.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Trouble with While loop.

Posted 12 October 2012 - 02:42 PM

Most likely. Where did you set i to be equal to "Hotdog", or "Hamburger" , or "Milkshake"?

Also note creating a variable with the name of Hotdog will not make it equal to "Hotdog".

You should review the following links: Variables and Constants.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Trouble with While loop.

Posted 12 October 2012 - 05:59 PM

Quote

I fixed the code, and ran it, but it comes up as empty. I think it just skips the loop.

You loop checks the condition "FIRST" then it asks for input. You might want to ask for input "FIRST" and the check the condition.

You can accomplish this by prompting for input before entering your while loop, or you could use a do-while loop.


std::string i;
do {
   std::cin>>i; //get input
} while(i == "yes"); //if input was yes, then loop again, otherwise break


Was This Post Helpful? 1
  • +
  • -

#6 MegaTerminatorz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-October 12

Re: Trouble with While loop.

Posted 14 October 2012 - 06:35 PM

Thank you "jjl" and "jimblumberg." I corrected the code with your advice, and it works perfectly now, and I understand it all.

Thank you once again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1