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 = {20, 20, 20, 20, 20};
double prices = {.75, .75, .75, .80, .80};

//protoypes
int sodaChoice();

int main()
{
char input;
double earnedAmount = 0.0;

do
{

cin.getline(input, 800);
return 0;
}

{

//make a list of the drinks
cout << "\n\nWelcome to the imaginary drink machine!\n";
cout << "---------------------------------------" << endl;
cout << "1. Cola        \$"<< setprecision(2) << fixed << prices << endl;
cout << "2. Root Beer   \$"<< setprecision(2) << fixed << prices << endl;
cout << "3. Lemon-Lime  \$"<< setprecision(2) << fixed << prices << endl;
cout << "4. Grape Soda  \$"<< setprecision(2) << fixed << prices << endl;
cout << "5. Cream Soda  \$"<< setprecision(2) << fixed << prices << endl;
cout << "6. Nothing thank you\n";
cout << "-->";

}

int sodaChoice()
{
char input;

//recive the choice
cin.getline(input,80);

}
{
//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;

{
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;
cout << "Here is your change: " << setprecision(2) << fixed << change << endl;
drinks = drinks - 1;
earned = earned + prices;
}
else
cout << "Now how did you manage to enter that?\n" << "Please enter a POSITIVE amount of money!\n";

}
{
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;
cout << "Here is your change: " << setprecision(2) << fixed << change << endl;
drinks = drinks - 1;
earned = earned + prices;
}
else
cout << "Now how did you manage to enter that?\n" << "Please enter a POSITIVE amount of money!\n";

}
{
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;
cout << "Here is your change: " << setprecision(2) << fixed << change << endl;
drinks = drinks - 1;
earned = earned + prices;
}
else
cout << "Now how did you manage to enter that?\n" << "Please enter a POSITIVE amount of money!\n";

}
{
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;
cout << "Here is your change: " << setprecision(2) << fixed << change << endl;
drinks = drinks - 1;
earned = earned + prices;
}
else
cout << "Now how did you manage to enter that?\n" << "Please enter a POSITIVE amount of money!\n";

}
{
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;
cout << "Here is your change: " << setprecision(2) << fixed << change << endl;
drinks = drinks - 1;
earned = earned + prices;

}
else
cout << "Now how did you manage to enter that?\n" << "Please enter a POSITIVE amount of money!\n";

}

{
//exit option
cout << "\nOkay then! Thank you for your patronage!!\n\n";

}

{
cout << "Silly, there's no choice with that number!\n";
}

cin.getline(input, 80);
return earned;

}
{

//if the choice is 6 exit and display amount earned

{
cout << "\n";
cout << "     --Machine Stock--   "<< endl;
cout << "# of cola left:       " << drinks << endl;
cout << "# of root beer left:  " << drinks << endl;
cout << "# of lemon-lime left: " << drinks << endl;
cout << "# of grape soda left: " << drinks << endl;
cout << "# of cream soda left: " << drinks << 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.

i'm trying to execute this program and everytime it tells me the same thing..

Welcome to the imaginary drink machine!
---------------------------------------
1. Cola \$0.75
2. Root Beer \$0.75
3. Lemon-Lime \$0.75
4. Grape Soda \$0.80
5. Cream Soda \$0.80
6. Nothing thank you
-->1

You chose Cola! That will be .75 cents!
Please insert money (in 0.00 format):0.75
Now how did you manage to enter that?
Please enter a POSITIVE amount of money!

isn't 0.75 positive? moreover y should it ask me to enter the amount when it's already shows how much it costs? System can itself take that amount.

You are initialising 'earnedAmount' to '0' in every call of 'sodaDisplay' method. It should be initialised only once. Make it static and try.

``static double earned = 0;``

The program is supposed to somewhat mimic a soda machine, so even though it tells you the cost, in the end you would still need to insert coins.
but thank you for the help, so I would just replace with my amountEarned with the static earned, I'll attempt to fix the exact amount problem as well.
thank you again

Dont replace earnedAmount, just change this line in 'sodaDisplay' method

``double earned = 0;``

to

``static double earned = 0;``

I kept on thinking of using a static too, but then I would pass on it thinking I could get it to work that way, thank you for helping with that static bit and finding that exact change bug.

ur most welcome ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.