First of all, I would like to thank to all who helped me first two previous threads.... You guys helped a lot. Sorry for not posting in my own last thread as if I didn't bother about your posts, but I did read them, I was too busy and just decided to give up on downloading Dev C++ and whatever but instead planing to buy a proper Microsof Visual C++.
For now, I mannaged to test out my programme, but I have a problem with the else
Here's the programme:
Code:
#include <iostream>
#include <string>
main ()
{
int quantity, total;
std::string s;
std::cout << "Fruits acailable: Apple, Banana and Orange.\n";
std::cout << "Enter type of fruits: ";
std::cin >> s;
if (s == "Apple")
{
std::cout << "Unit per price is 10cents.\n";
std::cout << "Enter quantity: ";
std::cin >> quantity;
total = quantity * 10;
std::cout << "Your total amount is " << total << "cents\n";
}
if (s == "Banana")
{
std::cout << "Unit per price is 8cents.\n";
std::cout << "Enter quantity: ";
std::cin >> quantity;
total = quantity * 8;
std::cout << "Your total amount is " << total << "cents\n";
}
if (s == "Orange")
{
std::cout << "Unit per price is 15cents.\n";
std::cout << "Enter quantity: ";
std::cin >> quantity;
total = quantity * 15;
std::cout << "Your total amount is " << total << "cents\n";
}
else
{
std::cout << "Fruit entered not available" << std::endl;
}
return 0;
}
I did use cents here, but I'm planning to use dollars instead to avoid decimal places.
The problem is the else.
If I choose Orange it works perfectly fine. But if I use Apple and Banana, the "Fruit entered not available" still appears. Even after giving the total amount.
I even tried
Code:
else if (s != "Apple", s != "Banana", s != "Orange")
{
std::cout << "Fruit entered not available" << std::endl;
}
return 0;
}
The same thing happens
I was also suggested to use
Code:
transform(answer.begin(), answer.end(), answer.begin(), ::tolower);
so that upper and lower case doesn't differ to the programme, but I'm not sure how to use it. Do I type int the exact code just before the return 0;
like this?
Code:
....
...
..
}
transform(answer.begin(), answer.end(), answer.begin(), ::tolower);
return 0;
}
is this correct?...
Also I was thinking of dealing with decimals, but... I don't think the lecturer expects that in our assignment because he hasn't teach us yet... But if it is simple to learn, I might consider using it.
But my main question here is how do I get the else to work?
Thank you,
[Z-D]