Please help me with this functions

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 number sold: ";
cin >> sold;

return 0;

//FIRST FUNCTION ENDS

// SECOND FUNCTION STARTS



balance = (inventstart + numreceived - sold);

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;
}
Last edited on
you have a return statement before the calculations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

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:

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

The program it's supposed to output the balance but i can't get this part working in the program. Please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* This progams displays the input information and updated inventory balance of a bookstore.
*/

#include <iostream>
#include <iomanip>
using namespace 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;

}

 
Last edited on
I have tried to add those lines, i I'm still getting runtime errors when i execute the program.
1
2
3
4
5
6
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;


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.
so it should look like this??

void displaybalance(int idnum,int inventstart,int numreceived,int 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;

it makes sense, but i get this error:

1>bookend.obj : error LNK2019: unresolved external symbol "void __cdecl displaybalance(int)" (?displaybalance@@YAXH@Z) referenced in function _main
1>E:\project4\Debug\project4.exe : fatal error LNK1120: 1 unresolved externals
Topic archived. No new replies allowed.