If/then If/else statments in c++

I am doing a class homework but I am stuck (first computer class I'm taking) and I can't figure out the first part of my "program". I wanted to try it out by myself but the hw is do in 5 hours and I just can't do it by myself so I am asking for help.....

What I need to do is create a program that tells you the price of a fruit when the fruit is chosen.... I think I have the if/then if/else part down but I can't get it to stay open for me to see the price...

dont know what i am doing wrong.

here is what I have so far... any input will be appreciated..

Thank you.


#include <iostream>
#include <string>
using namespace std;
int main()

{

char apple;
char orange;
char peacan;
char pear;
char fruit;

cout << "Which fruit would you like the price for?" << endl;

cout << "Press ENTER to contine..." << endl;
cin.get();
cout << "enter APPLE, ORANGE, PEACAN, OR PEAR." << endl;
cin >> fruit;
if (fruit == apple){
cout << "$.69 per lb." << endl;}
else
if (fruit == orange){
cout << "$.79 per lb." << endl;}
else
if (fruit == peacan){
cout << "$.99 per lb." << endl;}
else
if (fruit == pear){
cout << "$.89 per lb." << endl;}

cin.get();
return 0;

}
Last edited on
You are checking if the character the user entered is equal to the variable, which is currently some undefined and unknown value. Also, chars can only hold one value so there is no way for the user to enter a word as their response and the char to store it all.

Since you are including the string header, you should use std::strings instead of chars.
Use strings as Zhuge suggested and out words like orange in quotes or declare them as string variables and assign a value to them.
Topic archived. No new replies allowed.