Why does my output values is always 1?

Thanks for kind people who answered my last question so I can figure out why is the result wont show after 'status'. After I fixed my codes, I got another problem. When I'm trying to call procedure 'input' and 'cetak', it seems like the code doesnt work well. Why does it happen?

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
using namespace std;

char nama[20], gol[10], status[20];
float prosen,potong,gapok,tunja,x,gaji;


int input()
{	char nama[20], a[10], b[20];
	cout<<"===PROGRAM PENGHITUNG GAJI KARYAWAN==="<<endl;
	cout<<"Nama Karyawan  \t	:"; cin>>nama;
	cout<<"Golongan (A/B) ?\t:"; cin>>a;
	cout<<"\nStatus (Nikah/Belum)\t:"; cin>>b;
}
int cetak()
{ 
char a[10],b[20];
cout<<"Anda merupakan golongan"<<a<<"dan status anda adalah"<<b<<endl;
}
void gaji_pokok(char gol[10])
{
	
	if (stricmp("A",gol)==0)
		{
		gapok=2000000;
			
			}
	else
		{
		gapok=3000000;	
	
		}
}

void tunjangan(char gol[10], char status[20])
{

	if ((stricmp("a",gol)==0) && (stricmp("nikah",status)==0))
	
		
		{
		tunja=100000;
		}
	else if ((stricmp("a",gol)==0) && (stricmp("belum",status)==0))
	
		{
		tunja=50000;
		}
	
	else if ((stricmp("b",gol)==0) && (stricmp("nikah",status)==0))
	
		{
		tunja=150000;
	}
	else if ((stricmp("b",gol)==0) && (stricmp("belum",status)==0))
	

		{
		tunja=110000;;
		}
	else 
	{
	cout<<"error";
	}}
	
void prosentase(char gol[10],char status[20])
{
		if (stricmp("A",gol)==0)
		gapok=2000000;
	else
		gapok=3000000;
		//jarak 
		if ((stricmp("a",gol)==0) && (stricmp("nikah",status)==0))
	
		tunja=100000;
	
	else if ((stricmp("a",gol)==0) && (stricmp("belum",status)==0))
	
		tunja=50000;
	
	else if ((stricmp("b",gol)==0) && (stricmp("nikah",status)==0))
	
		tunja=150000;
	
	else if ((stricmp("b",gol)==0) && (stricmp("belum",status)==0))
	
		tunja=110000;
	else cout<<"error";
	//jarak
	x=gapok+tunja;
	if (x<=2500000)
	{
		prosen=0.05;
	}
	else if(x>3000000)
	{
		prosen=0.1;
	}}

void potongan_iuran(char gol[10],char status[20])
{
		if (stricmp("A",gol)==0)
		{gapok=2000000;}
	else
		gapok=3000000;
		//jarak 
		if ((stricmp("a",gol)==0) && (stricmp("nikah",status)==0))
	
		{tunja=100000;}
	
	else if ((stricmp("a",gol)==0) && (stricmp("belum",status)==0))
	
		{tunja=50000;}
	
	else if ((stricmp("b",gol)==0) && (stricmp("nikah",status)==0))
	
		{tunja=150000;}
	
	else if ((stricmp("b",gol)==0) && (stricmp("belum",status)==0))
	
		{tunja=110000;}
	else {cout<<"error";}
	//jarak
	x=gapok+tunja;
	if (x<=2500000)
	{
		potong=x*0.05;
	}
	else if(x>3000000)
	{
		potong=x*0.1;
	}
	

}
void gaji_bersih(char gol[10],char status[20])
{
		if (stricmp("A",gol)==0)
		{gapok=2000000;}
	else
		{gapok=3000000;}
		//jarak 
		if ((stricmp("a",gol)==0) && (stricmp("nikah",status)==0))
	
		{tunja=100000;}
	
	else if ((stricmp("a",gol)==0) && (stricmp("belum",status)==0))
	
		{tunja=50000;}
	
	else if ((stricmp("b",gol)==0) && (stricmp("nikah",status)==0))
	
		{tunja=150000;}
	
	else if ((stricmp("b",gol)==0) && (stricmp("belum",status)==0))
	
		{tunja=110000;}
	else {cout<<"error";}
	//jarak
	x=gapok+tunja;
	if (x<=2500000)
	{
		gaji=x-(x*0.05);
	}
	else if(x>3000000)
	{
		gaji=x-(x*0.1);

}
}


int main()
{	
	char nama[20], a[10], b[20];		
	cout<<"===PROGRAM PENGHITUNG GAJI KARYAWAN==="<<endl;
	cout<<input;
	cout<<cetak;
	cout<<"Gaji Pokok		:Rp "<<gapok<<",00";
	cout<<"Tunjangan		:Rp "<<tunja<<",00";
	prosentase (a,b);
	cout<<"\nPersen pemotongan	:"<<prosen;
	potongan_iuran(a,b);
	cout<<"\nPotongan Iuran		:Rp "<<potong<<",00";
	gaji_bersih(a,b);
	cout<<"\nGaji Bersih		:Rp "<<gaji<<",00";
	return 0;
}




Thank you for your help
Last edited on
Hi,

First compile the program with warnings on, I used cpp.sh (the gear icon top right of the code here) with c++14 and all 3 warnings on

In function 'int input()':
19:1: warning: no return statement in function returning non-void [-Wreturn-type]
In function 'int cetak()':
24:1: warning: no return statement in function returning non-void [-Wreturn-type]
In function 'void gaji_pokok(char*)':
28:21: error: 'stricmp' was not declared in this scope
In function 'void tunjangan(char*, char*)':
43:22: error: 'stricmp' was not declared in this scope In function 'void prosentase(char*, char*)':
73:22: error: 'stricmp' was not declared in this scope
78:23: error: 'stricmp' was not declared in this scope
In function 'void potongan_iuran(char*, char*)':
107:22: error: 'stricmp' was not declared in this scope
112:23: error: 'stricmp' was not declared in this scope
In function 'void gaji_bersih(char*, char*)':
143:22: error: 'stricmp' was not declared in this scope
148:23: error: 'stricmp' was not declared in this scope

In function int main()':
182:8: warning: the address of 'int input()' will always evaluate as 'true' [-Waddress]
183:8: warning: the address of 'int cetak()' will always evaluate as 'true' [-Waddress]
180:7: warning: unused variable 'nama' [-Wunused-variable]


For the first one, it looks like the function input should be void, not returning a value. When you call the function on line 182 it needs parentheses, and shouldn't be part of the cout.

stricmp is spelt incorrectly.

Good Luck !!

Edit: stricmp is a deprecated non standard function, it looks like it was a windows thing, here is the preferred :

https://msdn.microsoft.com/en-us/library/k59z8dwe.aspx

I compiled with a standard compliant compiler, you may not have those errors if compiled with a correctly set up Visual Studio
Last edited on
Hello heyitsme1998,

TheIdeasMan makes some very good points, but I will try to explain this in a different way.

In main you define char nama[20], a[10], b[20];. These are local variables and in this case "nama" here takes presidents over the global variable you defined on line 9. And yes line 182 input();, the () make it a function call, should not be part of the "cout" because you need to input information before you can use it. The same is true for line 183.

To the "input" function:

Line 14 defines char nama[20], a[10], b[20]; again which makes these local variables. Again you are using the local "nama" not the global "nama" that you should be using. It looks like "b" should be the global variable "status" and I am not sure what "a" is for. Your other problem is that when the "input" function ends so do the variables you defined on line 14, so after all that work you have nothing to use. Lastly "input" returns an int, but inside the function you are missing the return statement and there is no "int" to return. As TheIdeasMan said this should be a "void" function.

In the function "cetak()" you define char arrays for "a" and "b", but never give them any values before you reach the "cout" statement.

On line 9 you create global variables that you never use because your functions that could use these global variables create their own local variables of the same name which over shadows the global variables making them useless.

I think what you should do is move these global variables on lines 9 and 10 to main and pass them to your functions. You may need to pass some of these variables by reference so any changes are reflected back in main.

For the function "int cetak()" it says it returns an "int", but inside the function there is no return statement or even an "int" to return. Again it should be a "void" return type on the function.

Your other functions have a "void" return type and are OK for that part.

I will mention some minor things:

1
2
3
4
5
6
#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h> 

"stdio.h" is a C header file and not needed in a C++ program.
"stdlib.h" is included through "iostream" and you really do not have to include it a second time.
"conio.h" is a C header file and I am not sure right now is you actually need it.
Lastly "windows.h" tends to limit the program to Windows computers.

You include the header file "string", but use C style char arrays. If you make them "std::string"s you can do away with "stricmp()" which is outdated.

Lines 9 and 10 define global variables. Do not count on the compiler initializing these variables because they are global. You should always initial your variables when they are defined with some value even zero.

Hope that helps,

Andy
Topic archived. No new replies allowed.