If/Else Problems

Hey,
I'm working on a program where the user will select one of three fruits and then be directed to select a variety of that fruit. I am having a really difficult time figuring out how to properly use my if/else statements.
Thanks for helping.
-Sarah

#include <iostream>
#include <string>
//Write a program which asks for the user to choose a fruit and then ask for a specific variety depending on which fruit is selected.
using namespace std;
int main ()
{
string fruit, apples, oranges, pears; //Declare variables
cout << "Apples, Oranges or Pears?" << endl; //First selection
cin >> fruit; //Direct to selected fruit varieties
if (fruit==apples) //If apples are selected
{
cout << "Golden Delicious, Granny Smith or McIntosh?" << endl;
string varietya;
cin >> ws;
getline(cin,varietya);
cout << "You have selected " << varietya << " Apples" << endl;
}
if (fruit==oranges) //If oranges are selected
{
cout << "Valencia, Blood or Navel?" << endl;
string varietyb;
cin >> varietyb;
cout << "You have selected " << varietyb << " Oranges" << endl;
}
if (fruit==pears) //If pears are selected
{
cout << "Bartlett, Bosc or Comice?" <<endl;
string varietyc;
cin >> varietyc;
cout << "You have selected " << varietyc << " Pears" <<endl;
}
else
{
cout <<cout << "Next time please enter one of the fruits requested!" <<endl;
cout << "You have selected " << cin >> fruit << endl;

}
Here's a better way, featuring the else if statement. Also, you will need the options to be in " " or ' ', otherwise they will not register with the initialized strings. Here is a better version of the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "stdafx.h" //You may need to comment this out
#include <iostream>
#include <string>
//Write a program which asks for the user to choose a fruit and then ask for a specific variety depending on which fruit is selected.
using namespace std;
int main ()
{
	string fruit, apples, oranges, pears; //Declare variables
	cout << "Apples, Oranges or Pears?" << endl; //First selection
	cin >> fruit; //Direct to selected fruit varieties
	if (fruit=="apples") //If apples are selected
	{
		cout << "Golden Delicious, Granny Smith or McIntosh?" << endl;
		string varietya;
		cin >> ws;
		getline(cin,varietya);
		cout << "You have selected " << varietya << " Apples" << endl;
	}
	else if (fruit=="oranges") //If oranges are selected
	{
		cout << "Valencia, Blood or Navel?" << endl;
		string varietyb;
		cin >> varietyb;
		cout << "You have selected " << varietyb << " Oranges" << endl;
	}
	else if (fruit=="pears") //If pears are selected
	{
		cout << "Bartlett, Bosc or Comice?" <<endl;
		string varietyc;
		cin >> varietyc;
		cout << "You have selected " << varietyc << " Pears" <<endl;
	}
	else 
	{
		cout <<cout << "Next time please enter one of the fruits requested!" <<endl;
		cout << "You have selected ";
		cin >> fruit;

	}
}
First please read this and update your post with neatly formatted code and tags.
http://cplusplus.com/articles/firedraco1/

Second, you are comparing inputs to uninitialized string objects.
Try doing this and see what happens. You should see nothing since the string objects will be default initialized with no characters.
1
2
string fruit, apples, oranges, pears;
std::cout << fruit << apples << oranges << pears << std::endl;


Since you are mixing cin and getline you might as well read item 15.6 of this. 15.1 - 15.7 will all be useful for a beginner. These issues always seem to trip people up.
http://www.parashift.com/c++-faq-lite/input-output.html
Topic archived. No new replies allowed.