How to use void fuctions

I need help/aid on my c++ program/file. I am a beginner and I am using the code that I saw on youtube, but a bit different. The compiler does not detect any errors, however when the program runs, the void calc does not appear. Does anyone knows the program.
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
#include <iostream>
#include <cstdio>
#include <windows.h>
using namespace std;


void calc() {
    int a;
    int b;
    int sum;
    cout <<"Enter a number \n";
    cin >> a;
    cout <<"Enter the second number \n";
    cin >> b;
    sum=a+b;
    cout <<"The sum of "<<a <<"+" <<b;
    cout <<"=" <<sum;
    Sleep (500);
}
int main() {
    cout << "Hello World!" <<endl;
    Sleep (1000);
    cout << "Input your name: ";
    string name;
    cin >> name;
    cout <<"Hello " << name <<" welcome to my C++ program " <<endl;
    Sleep (1000);
    cout <<"Random Number Generator" <<endl;
    int numrand;
    numrand+rand();
    cout <<numrand <<endl;
    cout <<"Enter the amount of hours you worked this week: \n";
    int hours;
    cin >> hours;
    double totalpaycheck=hours * 10.15;
    cout <<"You will recieve: " << totalpaycheck <<endl;
    Sleep (1000);
    int answer;
    int a=5;
    int b=6;
    Sleep (1000);
    answer= a+b;
    cout << "5+6=" <<answer << endl;
    Sleep (500);
}
i to am a beginner so dont take what i say as concrete fact.. but i dont think you called the calc function within the main function O_o
Last edited on
Yea, you never called the function. It might be a good idea to capitalize Function names to make them standout from variable names which are preferred using the camel-casing format.

// function (capitalized)
void CalculatePay( );

// variable (camel-case)
int calculatedPay;


These are just conventions but they do make your code easier to read.
Thanks for the tips, I figure out the problem. As kapo pointed I did not declare the function in the main() function.

I found it weird that when I called/declare the function in last line of int main function but I didn't wrote void Calc();, but instead I wrote Calc(); and it works. However, when I tried to add void Calc();, the void Calc function did not appear. I appreciate your help kapo and IceThatJaw.

My improved version:
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
#include <iostream>
#include <cstdio>
#include <windows.h>
using namespace std;

void Calc() {
    int a;
    int b;
    int sum;
    cout <<"Enter a number \n";
    cin >> a;
    cout <<"Enter the second number" <<endl;
    cin >> b;
    sum=a+b;
    cout <<"The sum of "<<a <<"+" <<b;
    cout <<"=" <<sum;
    Sleep (500);
}

int main() {
    cout << "Hello World!" <<endl;
    Sleep (1000);
    cout << "Input your name: ";
    string name;
    cin >> name;
    cout <<"Hello " << name <<" welcome to my C++ program " <<endl;
    Sleep (1000);
    cout <<"Random Number Generator" <<endl;
    int numrand;
    numrand+rand();
    cout <<numrand <<endl;
    cout <<"Enter the amount of hours you worked this week: \n";
    int hours;
    cin >> hours;
    double totalpaycheck=hours * 10.15;
    cout <<"You will recieve: " << totalpaycheck <<endl;
    Sleep (500);
    Calc();
}


Last edited on
Looks good. Now try adding some loops to it. ; )
Last edited on
Topic archived. No new replies allowed.