Hello zxkun,
According to your original code you initialized "index", so the
index++;
in case 1 should not be a problem. At least it worked for me.
Second thing I did was to initialize all the variables of the struct because any unused variable had a very large negative number:
1 2 3 4 5
|
struct Calculation
{
std::string itemName{ "" };
double price{ 0.0 }, tax{ 0.0 }, sum{ 0.0 };
};
|
I changed the function "showPrice" to use a for loop and also changed the output. Unless your idea was to use "showPrice" after each entry. In which case I would have to undo my changes and do some testing.
1 2 3 4
|
case 1:
input(total,index,sum);
//index++; here
break;
|
index++;
should work. I hope the word "here" is commented or tho there or you will have a problem. If this gives you an error message post the message.
Next "sum" is defined in main and passed by value to the function. In the function you only are using a copy of "sum", so any changes in the function will not be reflected in main. Ad when the function ends "sum" is destroyed. If yo really want to leave "sum" in main and want the value to change then pass the variable by reference:
void input(Calculation total[], int index, double & sum)
. Now any change to "sum" in the function will be seen in main, but there is no real need for "sum" to be defined in main or passed to the functions. Unless the idea is to keep a running total of your input.
the sum displays a extremely huge number. |
I do not see how because the variable is initialized to zero and never changes.
When I first added the "index++" to case 1 the input worked, but the showPrice" function did not because "index" would have the wrong value.
If you are still having problems post the whole code for main.
Hope that helps,
Andy