I need help with a function returning the correct values

So I have an issue getting this function to work in my program for a summer course for C++.

We are to create a program where a user orders food and we return the total to them. I created a simple function in the beginning that basically adds the two choices the user makes in order to give them the total.This isn't the entire program, just the segment that is giving me issues. The program runs without errors but the totals are returning all wrong.

Some assistance would be appreciated because I literally started writing code for the first time a week and a half ago. . .but I must say I really like it! Oh and let me know of any tips on proper syntax and best practices. I really want to get good at this.

#include <iostream>
#include <cmath>
#include <string>

float addition (float a ,float b) {

float c;

c = a + b;

return c;
}


using namespace std;

int main() {

const string FOOD1 = "1. Hamburger ($2.00)";
const string FOOD2 = "2. Cheeseburger ($2.50)";
const string FOOD3 = "3. Double Deluxe Fat Man Burger ($8.00)";
const string DRINK1 = "1. Diet Coke ($2.00)";
const string DRINK2 = "2. Iced Tea ($2.00)";
const string DRINK3 = "3. Texas Size Sweet Tea ($6.00)";
char FoodChoice;
char DrinkChoice;



//Revealinig the menu to the customer
cout << "Here are our Food items. Please input the number of the item you would like\n";
cout << FOOD1 <<endl;
cout << FOOD2 <<endl;
cout << FOOD3 <<endl;
cin >> FoodChoice;

if (FoodChoice == 1) {
float FoodPrice1 = 2.00;
FoodChoice = FoodPrice1;
}
else if (FoodChoice == 2) {
float FoodPrice2 = 2.50;
FoodChoice = FoodPrice2;
}
else if (FoodChoice == 3) {
float FoodPrice3 = 8.00;
FoodChoice = FoodPrice3;
}
cout << "Here are our Drink items. Please input the number of the item you would like,\n";
cout << DRINK1 <<endl;
cout << DRINK2 <<endl;
cout << DRINK3 <<endl;
cin >> DrinkChoice;

if (DrinkChoice == 1) {
float DrinkPrice1 = 2.00;
DrinkChoice = DrinkPrice1;
}
else if (DrinkChoice == 2) {
float DrinkPrice2 = 2.00;
DrinkChoice = DrinkPrice2;
}
else if (DrinkChoice == 3) {
float DrinkPrice3 = 6.00;
DrinkChoice = DrinkPrice3;
}
cout << "Thank you for your selection. Here is your total:" <<endl;
cout << addition (FoodChoice , DrinkChoice);


}
Please write [code] and [/code] around your code so the forum will preserve indentation and syntax highlight it for us when we read it.

The problem, I think, is that your variables for storing the user's choices are characters; this means when you compare the integral 1 to it, you are using the character with ASCII value 1, which is the SOH (start of heading) control character. Not what I think you meant!

The character '1' is represented by ASCII value 49, so you could compare to that, though I would recommend just using the literal '1' - that's a 1 in single quotes - to mean the character 1.

Other notes:

Instead of creating temporary float variables then assigning them to the variable you use to store the choice:
1
2
3
// What you are doing:
float DrinkPrice3 = 6.00;
DrinkChoice = DrinkPrice3;

Just assign to it directly:
1
2
// More succinct:
DrinkChoice = 6.00;


Your function is incredibly simple and so in normal cases I'd recommend just removing it, but I can see it is probably for learning reasons. You could at least simplify it similarly to the above:
1
2
3
float addition(float a, float b) {
    return a + b;
}
Last edited on
THANK YOU SO MUCH I knew it was a super small fix that I was overlooking. I appreciate the help! I would buy you a beer if you were in NY haha!
Topic archived. No new replies allowed.