Working on a project and stumbled here for help. I am at about 90% of what I need but I am having one small problem. With the exception of one of my "if" sections, my "else" output is triggered and diplayed when I don't want it to be. Just asking what I need to do so it only shows up if the user enters a wrong input. Basically, the "Next time.." line shows up even if I input the correct "fruit"
Thanks and tell me if I am posting this wrong too.

#include <iostream>
#include <string>
using namespace std;
int main()
{
string fruit, ftype, Apples, Oranges, Pears;

cout<<"Apples, Oranges or Pears?"<<endl;
cin>>fruit;
cin.ignore();

if (fruit=="Apples")
{
	cout<<"Golden Delicious, Granny Smith, or McIntosh?"<<endl;
	cin>>ftype;
	cin.ignore();
}

if (fruit=="Oranges")
{
	cout<<"Valencia, Blood or Navel?"<<endl;
	cin>>ftype;
	cin.ignore();
}

if (fruit=="Pears")
{
	cout<<"Bartlett, Bosc or Comice?"<<endl;
	cin>>ftype;
	cin.ignore();
}

else 
cout<<"Next time please enter one of the fruits requested!"<<endl;
	cout<<"You have selected "<<ftype<<" "<<fruit<<endl;
}

It's a habit of mine to always put my else statement in brackets, not sure if that is the problem though.

>>It's a habit of mine to always put my else statement in brackets,
That is an excellent habit to get into. :) In the code you posted those brackets are necessary.


The else only belongs to the last if statement. IMO it would be better to use else after each if. This prevents unnecessary comparisons.

if( fruit == "Apples")
{
   // blabla
}
else if( fruit == "Oranges" )
{
   // blabla
}
else if( fruit == "Pears" )
{
   // blabla
}
else
{
   // blabla
}
void func(const char *);
...
if(option1)
  func("option1");
else if(option2)
  func("option2");
else
  cout << "Not an option.";

Nvm, AD answered it.

Here you go :-D. hope it works as you want it. I just started C++ and I had to flip through my book like a zillion times to figure this out.. they said online i should try and debug other peoples script "Get it, Read it, Tinker With it, Debug it, And you will become a better programmer." :-)
#include <iostream>
#include <string>
using namespace std;

int main()
{
string fruit, Apples, Oranges, Pears;
char ftype[20];
cout<<"Apples, Oranges or Pears?"<<endl;
cin>>fruit;
if (fruit=="Apples" || fruit=="Oranges" || fruit=="Pears")
{
                    if (fruit=="Apples")
                    {
                                        cout<<"Golden Delicious, Granny Smith, or McIntosh?"<<endl;
                                        cin.ignore();
                                        cin.getline (ftype, 20);
                                        goto Answer;
                    }
                    if (fruit=="Oranges")
                    {
                                         cout<<"Valencia, Blood or Navel?"<<endl;
                                         cin.ignore();
                                         cin.getline (ftype, 20);
                                         goto Answer;
                    }
                    if (fruit=="Pears")
                    {
                                       cout<<"Bartlett, Bosc or Comice?"<<endl;
                                       cin.ignore();
                                       cin.getline (ftype, 20);
                                       goto Answer;
                    }
}
else
cout<<"Next time please enter one of the fruits requested!"<<endl;
Answer:
      cout<<"You have selected "<<ftype<<" "<<fruit<<endl;
      cin.ignore();
      cin.get();
return 0;
}

[B]goto[/B] = bad; Not only that, but your else fallsthrough to label Answer, defeating its purpose.

else
  cout<<"Next time please enter one of the fruits requested!"<<endl;
Answer:

edit:
There is a couple rare cases where the low level goto can be used. This is defiantly not one.(to answer a PM)

Remember we're not in assembly.

Thanks for the help, I am so new to this i didn't realize how the "else if" statement worked. If I understand it right, it functions as an or kind of. Like if not a, else if try b, else if try c, then else sort of wraps up any input no included in prior if or if else statements. Thanks this is so helpful to my understanding

This article has been dead for over six months. Start a new discussion instead.