First off I'd like to say I'm a beginner in C++ (VB was my stronger point I think), so be gentle about how gigantic my code is.
I've been working on a project for a class for a couple days now, it's not due for a couple days, but for the life of me, I can't seem to get my earned return (returned from double sodaDisplay) to work.
Even on exams I've had problems with the return part of the function as well, so I have no clue as to what I'm doing wrong (though, just like my car, if the teacher's looking over my shoulder, it will all go well). All today I've been attempting to debug my code, and I can't see it, so I've learned that if I let another coder look at it they'll find out a lot quicker than the person who's been dealing with it longer.
Anyways, the code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
using namespace std;
int drinks[5] = {20, 20, 20, 20, 20};
double prices[5] = {.75, .75, .75, .80, .80};
//protoypes
void sodaMenu();
int sodaChoice();
double sodaDisplay(int menuChoice);
void endDisplay(int menuChoice, double earned);
int main()
{
char input[800];
int menuChoice;
double earnedAmount = 0.0;
//call the menu and loop the menu until a valid choice is made
do
{
sodaMenu();
menuChoice = sodaChoice();
earnedAmount = sodaDisplay(menuChoice);
}while (menuChoice != 6);
endDisplay(menuChoice, earnedAmount);
cin.getline(input, 800);
return 0;
}
void sodaMenu ()
{
//view menu
//make a list of the drinks
cout << "\n\nWelcome to the imaginary drink machine!\n";
cout << "Please choose your icey cool beverage!!\n";
cout << "---------------------------------------" << endl;
cout << "1. Cola $"<< setprecision(2) << fixed << prices[0] << endl;
cout << "2. Root Beer $"<< setprecision(2) << fixed << prices[1] << endl;
cout << "3. Lemon-Lime $"<< setprecision(2) << fixed << prices[2] << endl;
cout << "4. Grape Soda $"<< setprecision(2) << fixed << prices[3] << endl;
cout << "5. Cream Soda $"<< setprecision(2) << fixed << prices[4] << endl;
cout << "6. Nothing thank you\n";
cout << "-->";
}
int sodaChoice()
{
char input[80];
int menuChoice;
//recive the choice
cin.getline(input,80);
menuChoice = atoi(input);
return menuChoice;
}
double sodaDisplay(int menuChoice)
{
//if the user picks a number not on the list, show an error
//and determine the soda choice
//also calculate change,number left, and tally amount earned
double change;
double money;
double earned = 0;
char input[800];
if(menuChoice == 1)
{
cout << "\nYou chose Cola! That will be .75 cents!\n";
cout << "Please insert money (in 0.00 format):";
cin.getline(input,80);
money = atoi(input);
if(money > 0)
{
change = money - prices[0];
cout << "Here is your change: " << setprecision(2) << fixed << change << endl;
drinks[0] = drinks[0] - 1;
earned = earned + prices[0];
}
else
cout << "Now how did you manage to enter that?\n" << "Please enter a POSITIVE amount of money!\n";
}
if(menuChoice == 2)
{
cout << "\nYou chose Root Beer! That will be .75 cents!\n";
cout << "Please insert money (in 0.00 format):";
cin.getline(input,80);
money = atoi(input);
if(money > 0)
{
change = money - prices[1];
cout << "Here is your change: " << setprecision(2) << fixed << change << endl;
drinks[1] = drinks[1] - 1;
earned = earned + prices[1];
}
else
cout << "Now how did you manage to enter that?\n" << "Please enter a POSITIVE amount of money!\n";
}
if(menuChoice == 3)
{
cout << "\nYou chose Lemon-Lime! That will be .75 cents!\n";
cout << "Please insert money (in 0.00 format):";
cin.getline(input,80);
money = atoi(input);
if(money > 0)
{
change = money - prices[2];
cout << "Here is your change: " << setprecision(2) << fixed << change << endl;
drinks[2] = drinks[2] - 1;
earned = earned + prices[2];
}
else
cout << "Now how did you manage to enter that?\n" << "Please enter a POSITIVE amount of money!\n";
}
if(menuChoice == 4)
{
cout << "\nYou chose Grape Soda! That will be .80 cents!\n";
cout << "Please insert money (in 0.00 format):";
cin.getline(input,80);
money = atoi(input);
if(money > 0)
{
change = money - prices[3];
cout << "Here is your change: " << setprecision(2) << fixed << change << endl;
drinks[3] = drinks[3] - 1;
earned = earned + prices[3];
}
else
cout << "Now how did you manage to enter that?\n" << "Please enter a POSITIVE amount of money!\n";
}
if(menuChoice == 5)
{
cout << "\nYou chose Cream Soda! That will be .80 cents!\n";
cout << "Please insert money (in 0.00 format):";
cin.getline(input,80);
money = atoi(input);
if(money > 0)
{
change = money - prices[4];
cout << "Here is your change: " << setprecision(2) << fixed << change << endl;
drinks[4] = drinks[4] - 1;
earned = earned + prices[4];
}
else
cout << "Now how did you manage to enter that?\n" << "Please enter a POSITIVE amount of money!\n";
}
if(menuChoice == 6)
{
//exit option
cout << "\nOkay then! Thank you for your patronage!!\n\n";
}
if (menuChoice > 6)
{
//option not on the menu
cout << "Silly, there's no choice with that number!\n";
}
cin.getline(input, 80);
return earned;
}
void endDisplay(int menuChoice, double earnedAmount)
{
//if the choice is 6 exit and display amount earned
if(menuChoice == 6)
{
cout << "\n";
cout << " --Machine Stock-- "<< endl;
cout << "# of cola left: " << drinks[0] << endl;
cout << "# of root beer left: " << drinks[1] << endl;
cout << "# of lemon-lime left: " << drinks[2] << endl;
cout << "# of grape soda left: " << drinks[3] << endl;
cout << "# of cream soda left: " << drinks[4] << endl;
cout << "***This machine has earned " << setprecision(2) << fixed << earnedAmount << "***";
}
}
I'm Very Very Very sorry that it's so longwinded, but... I'm really no good in C++
The program's meant to be a refresher of things from the beginning of our text so that we can be refreshed for the cumulative exam at the end.
but anyways, my return always gives me a strange number like -90000000, or it won't return at all.
thank you for your help.