function help

I'm trying to write a function to print out a subtotal but i can't seem to get it working please any advice would help. This is basically a calculator program that takes in the amount the user enters and keeps a running total until the user wants to stop.





#include <cstdlib>
#include <iostream>
using namespace std;
struct calc {
char description[20];
float amount;

};


void printheading();
void subtotal(calc inventory[], int count);

int main ()
{
printheading();

calc inventory[100];
int count=0;
char rerun;
float total=0;



do
{
cout<<"Please enter the DESCRIPTION: ";
cin>>inventory[count].description;


cout<<"Please enter the Amount: ";
cin>>inventory[count].amount;


subtotal(calc inventory[], count);

cout<<"Would you like to add more transactions?(N or Y) ";
cin>>rerun;
count++;
}
while (rerun == 'Y' || rerun == 'y');






return 0;

}

void subtotal(calc inventory[], int count)
{
float subtotal=0;
subtotal += inventory[count].amount;
cout<<"The subtotal is: "<<subtotal<<endl;
return;
}


void printheading()
{
cout << "********************CALCULATOR 1.0************************"
<< endl;
cout << "Welcome! " << endl;
cout << "This program computes the total for all your items."
<< endl;
cout << "You supply the information: Description, Amount."
<< endl;
cout << "The program will keep a running total for you until you want to stop."
<< endl;
cout << "**********************************************************"
<< endl;
cout <<"Let's get started ..." << endl;

return;
}




Last edited on
You need to tell the compiler when to stop looping based on the number of items.

consider adding this line:

1
2
if(count <= 100)
break;


to you do while loop.. it will check if you have overstepped your array count and stop you from producing a segmentation fault.
Last edited on
But my function subtotal doesn't even work. Am i passing the arg correctly into the function?
Topic archived. No new replies allowed.