So i have to do a program that uses functions in c++, i have written the code and i successfully compiled, but every time i run it, after i input the (idnum,invenstart,numreceived,sold), it just closes the program without doing the calculations and showing the balance output. Im new to c++, and functions seems to be harder to me at this point.
thanks in advanced
code:
/* This progams displays the input information and updated inventory balance of a bookstore.
*/
#include <iostream>
#include <iomanip>
using namespace std;
// FIRST FUNCTION STARTS
int main()
{
// Local declarations
int idnum; //declared as integer
int inventstart; // declared as integer
int numreceived; // declared as integer
int sold; // declared as integer
int balance;
// input
cout << "Enter the ID number: ";
cin >> idnum;
cout << "Enter the Inventory Start: ";
cin >> inventstart;
cout << "Enter number received: ";
cin >> numreceived;
cout << "Enter the ID number: ";
cin >> idnum;
cout << "Enter the Inventory Start: ";
cin >> inventstart;
cout << "Enter number received: ";
cin >> numreceived;
cout << "Enter number sold: ";
cin >> sold;
// return 0; << FIXME THIS IS WHERE THE PROBLEM IS
//FIRST FUNCTION ENDS
// SECOND FUNCTION STARTS
balance = (inventstart + numreceived - sold);
return 0;
Thanks for the help, i did took out that "return 0;" statement, but it looks like I'm still getting the same problem, i believe that this last section is not calling the functions from main:
/* This progams displays the input information and updated inventory balance of a bookstore.
*/
#include <iostream>
#include <iomanip>
usingnamespace std;
void displaybalance(int balance);
// FIRST FUNCTION STARTS
int main()
{
// Local declarations
int idnum; //declared as integer
int inventstart; // declared as integer
int numreceived; // declared as integer
int sold; // declared as integer
int balance;
// input
cout << "Enter the ID number: ";
cin >> idnum;
cout << "Enter the Inventory Start: ";
cin >> inventstart;
cout << "Enter number received: ";
cin >> numreceived;
cout << "Enter number sold: ";
cin >> sold;
//FIRST FUNCTION ENDS
// SECOND FUNCTION STARTS
balance = (inventstart + numreceived - sold);
displaybalance(balance);
// For Windows system("pause");
// For Linux system("sleep 5");
return 0;
}
// SECOND FUNCTION ENDS
// THIRD FUNCTION STARTS
void displaybalance(int balance)
{
int idnum,inventstart,numreceived,sold;
cout << "id number" << idnum << endl << endl;
cout << "inventory start" << inventstart << endl << endl;
cout << "number received" << numreceived << endl << endl;
cout << "number sold" << sold << endl << endl;
return;
}
You never give values to the variables you define here, so you will get undefined behavior. Instead of passing balance to displaybalance (int) you should pass idnum,inventstart,numreceived,sold (from main). This should give values to the variables in the function which will give the expected results.