Help with getting functions to work

So for my homework i have to create 3 files, a main, a functions, and a header file.
The program is supposed to calculate the charges for a internet company based on package inputted in hours inputted. We have to declare all the functions in the function file then use them in the main file. So far i have all the functions written but have no idea how to get them to work in the main.cpp

Any suggestions would be appreciated!
Functions (updated)
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
 #include <cstdlib>
#include <iostream>
#include <string>
#include "header.h"
using namespace std;

    
    char pck;
    int hours;
    double hoursover;
    double planhours;  
    double plancost;
    double overage;
    double total;
    
char getpck()
{
    cout<<"Enter Package ID:  ";
    cin>>pck;
          
    return pck;
}
        

int validpck()
{
    
    

    if ((pck == 'a')||(pck == 'A')){
        int calculateA();
    }else if ((pck == 'b')||(pck == 'B')){
        int calculateB();
    }else if ((pck == 'c')||(pck == 'C')){
        int calculateC();
    }else {//user did not input correct package id
        cout<< "invalid input.";
       
        return 0;
    }
    
    return 0;
}

int gethr()
{
    cout<<"Enter Hours:  ";
    cin>>hours;
    
   return hours;
}


int calculateA()
{
        planhours = 50;
        plancost = 15;        
        
        if (hours > 50){
        hoursover = (hours-50);
        overage = (hoursover * 2);
        total = overage + plancost;
        
        }
        else {
            hoursover = 0;
            overage = 0;
            total = plancost;     
        }
        return total;
}
int calculateB()
{
        planhours = 100;
        plancost = 20;        
        
        if (hours > 100){
        hoursover = (hours-100);
        overage = (hoursover * 1.50);
        total = overage + plancost;
        
        }
        else {
            hoursover = 0;
            overage = 0;
            total = plancost;
                   
        }
        return total;
}

int calculateC()
{
        planhours = 150;
        plancost = 25;        
        
        if (hours > 150){
        hoursover = (hours-150);
        overage = (hoursover * 1);
        total = overage + plancost;
        
        }
        else {
            hoursover = 0;
            overage = 0;
            total = plancost;
                   
        }
        return total;
}

int showbill()
{
    cout<<"Amount due: "<<total;
    return 0;
}


header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


#ifndef HEADER_H
#define	HEADER_H


char getpck ();
int gethr ();
int validpck ();
calculateA();
calculateB();
calculateC();
int showbill ();




#endif	/* HEADER_H */ 


Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdlib>
#include "header.h"
#include <string>
#include <iostream>
using namespace std;
int main ()
{
    
     getpck();
     gethr();
     validpck();
     
     showbill();
     
    
    return 0;
}
Last edited on
That isn't how you call a function.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdlib>
#include "header.h"
int main ()
{
    
     getpck();
     gethr();
     
     showbill();
     
    
    return 0;
}
i have fixed that

this is my main now. Currently i am getting "forbids comparison between pointer and integer" error..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdlib>
#include "header.h"
#include <string>
#include <iostream>
using namespace std;
int main ()
{
    
     getpck(char pck);
     gethr(int hours);
     validpck(char pck);
     
     showbill();
     
    
    return 0;
}
Out of curiosity, what happens when you try to compile this?

<edit> look closer at what Austin J posted.
Last edited on
next time it would be helpful if you post the whole error, with the line so we can look faster in your errors.

 
getpck(char pck);

your way of calling of the function is wrong because getpck() doesn't accept parameters and more over,
pck is declared as string, in the global scope and yet you declared it again as char,, you should remove char pck since it's useless
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/functions1
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/functions.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/functions.o.d -o build/Debug/GNU-MacOSX/functions.o functions.cpp
functions.cpp: In function 'int validpck()':
functions.cpp:28: error: ISO C++ forbids comparison between pointer and integer
functions.cpp:28: error: ISO C++ forbids comparison between pointer and integer
functions.cpp:30: error: ISO C++ forbids comparison between pointer and integer
functions.cpp:30: error: ISO C++ forbids comparison between pointer and integer
functions.cpp:32: error: ISO C++ forbids comparison between pointer and integer
functions.cpp:32: error: ISO C++ forbids comparison between pointer and integer
make[2]: *** [build/Debug/GNU-MacOSX/functions.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2


i have made the following changes to the header and functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20


#ifndef HEADER_H
#define	HEADER_H


char getpck ();
int gethr ();
int validpck ();
int calculateA ();
int calculateB ();
int calculateC ();
int showbill ();




#endif	/* HEADER_H */



functions
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
    
    char pck;
    int hours;
    double hoursover;
    double planhours;  
    double plancost;
    double overage;
    double total;
    
char getpck ()
{
    cout<<"Enter Package ID:  ";
    cin>>pck;
          
    return pck;
}
        

int validpck ()
{
    
    if ((pck == "a")||(pck == "A")){
        int calculateA ();
    }else if ((pck == "b")||(pck == "B")){
        int calculateB ();
    }else if ((pck == "c")||(pck == "C")){
        int calculateC ();
    }else {//user did not input correct package id
        cout<< "invalid input.";
        return (0);
    }
    
    return 0;
}

int gethr ()
{
    cout<<"Enter Hours:  ";
    cin>>hours;
    
   return hours;
}


Everything else is unchanged. My reason for changing the string to 'char' was because i was getting an error about not being able to convert a string to int. changing it to a 'char' got rid of the error but brought up this new one.

Last edited on
when comparing single char, we only use single quotes -> 'a' not double quotes
Alright that did fix the error. I am now getting the prompt for inputting the hours and package. But it doesnt seem to be using the inputted data in the validpck function to run the calculations.

EI

Enter Package ID: a
Enter Hours: 60
Amount due: 0
RUN FINISHED; exit value 0; real time: 2s; user: 0ms; system: 0ms

that should have come out with a total of 35.

Am i missing something to do with parameters?
remember when calling functions, we don't include the return type:
this int calculateA ();, should be this calculateA();

the simple reason behind your error is possibly you call the function calculateA() but that function doesn't have return statement so automatically whenever you call the function 0 is returned instead
Do you mean in the functions code? or the header? I tried changing both of them to what you said and i got an error saying that it had to be defined.


Wait i got it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int validpck()
{
    
    

    if ((pck == 'a')||(pck == 'A')){
        calculateA();
    }else if ((pck == 'b')||(pck == 'B')){
         calculateB();
    }else if ((pck == 'c')||(pck == 'C')){
         calculateC();
    }else {//user did not input correct package id
        cout<< "invalid input.";
       
        return 0;
    }
    
    return 0;
}


works fine now. I just didnt understand where you were saying i needed to change it.

Thank you all for helping me! I was planning for a long night of research and frustration. I know i am on here basically every week with a new problem but i am learning. Nice to know you guys are always willing to help out!

Thanks again!
Last edited on
This will work also
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double validpck()
{
    
    

    if ((pck == 'a')||(pck == 'A')){
        calculateA();
    }else if ((pck == 'b')||(pck == 'B')){
         calculateB();
    }else if ((pck == 'c')||(pck == 'C')){
         calculateC();
    }else {//user did not input correct package id
        cout<< "invalid input.";
       
        return 43.1;
    }
    
    return 4.6;
}

But that doesn't make it right.
Topic archived. No new replies allowed.