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 elseif 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:
#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.
usingnamespace 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;
}
elseif (fruit=="oranges") //If oranges are selected
{
cout << "Valencia, Blood or Navel?" << endl;
string varietyb;
cin >> varietyb;
cout << "You have selected " << varietyb << " Oranges" << endl;
}
elseif (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;
}
}
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