Problem calling int function

I have an int function as follows:

int getHowManyCourses ( int nbrCourses )
{
cout <<"Enter how many courses - up to three(3) : \n\t";
cin >> nbrCourses;

if ( nbrCourses <= 3 )
{
cin >> nbrCourses;
}
else
{
cout << "Invalid number! Try Again!\n\n";
}
}

the prototype for said int function is:

int getHowManyCourses ( int & nbrCourses );


When I try and call it from the int main () as follows:

int main ()
{
int courses;
courses = getHowManyCourses ( int nbrCourses );
}

I recieve this error message: "In function 'int main ()': expected primary-expression before "int"..."

What 'primary-expression' am I missing?

Any help is much appreciated, its holding me back from continuing a large project due tomorrow.

-J



You cannot declare int nbrcourses when calling it.

Try:
1
2
3
4
5
6
7
8
9
10
int getNumber(int start) {
 return (start+1);
}

int main();
 int courses = 2;
 int answer = getNumber(courses);

 return 0;
}
Ok, I'm not really following...

In lines 1 and 2, is that a revision of the original function (getHowManyClasses)? and if so, why do I rename my declared int's different from the original prototype? Why would I be calling 'courses' instead of 'start' in the int main() ?

Sorry, this is my first programming class and I'm struggling,

I've attached the project and code so far so you've got the whole picture.

Valencia Community College
C++ Programming

Learning Outcomes:
1) Selection statements
2) repetition statements
3) Functions

At Valence community college, a student can’t take more than 3 courses under the constraint of having no more than 7 credit hours. The purpose of this assignment is to construct a fee invoice for a student. This requires the input of Student’s id as integer and the course numbers.
It costs 120.25 dollars per credit hour in addition to $35.00 charged for health and id services.

Here is the list of all courses Valence Community College offers:

CRN Course Prefix Credit Hours
4587 MAT 236 4
4599 COP 220 3
8997 GOL 124 1
9696 COP 100 3

After inputting all the necessary data (see sample run), a fee invoice as shown below should be printed to the screen.

VALENCE COMMUNITY COLLEGE
ORLANDO FL 10101
---------------------

Fee Invoice Prepared for Student V5656

1 Credit Hour = $120.25

CRN CR_PREFIX CR_HOURS
4587 MAT 236 4 $ 481.00
4599 COP 220 3 $ 360.75

Health & id fees $ 35.00

--------------------------------------
Total Payments $ 876.75


Use the following function declarations:
void getId ( int & id);
This function asks for id.

int getHowManyCourses ( int & ndrCourses);
This function asks for number of courses. The number of courses has to be less or equal to 3, otherwise, the user is asked to enter it again.

int getHours ( int crn );
This function returns how many credit hours that go with crn

double getTuitionCost ( int crn);
This function returns how much it cost for crn.

void printInvoice ( int crn1, int crn2, int crn3);
Prints the fee invoice.

Add more functions if you want to!
_______________________________________________________
Sample Run (The user’s entry is in bold)

Enter the Students Id
5656

Enter how many courses-up to 3
6
Invalid number! Try again

Enter how many courses-up to 3
3

Enter the 3 course number(s)
4587 4599 9696

Sorry, you can’t cumulate more than 7 credit hours

Do you want to try again (Y/N)
y

Enter how many courses-up to 3
2

Enter the 2 course number(s)
4587 4599

Thank you!
Press Any Key to Continue


VALENCE COMMUNITY COLLEGE
ORLANDO FL 10101
---------------------

Fee Invoice Prepared for Student V5656

1 Credit Hour = $120.25

CRN CR_PREFIX CR_HOURS
4588 MAT 236 4 $ 481.00
4599 COP 220 3 $ 360.75

Health & id fees $ 35.00

--------------------------------------
Total Payments $ 876.75




Would you like to process another student? (Yes/No): No
Goodbye!


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


int getHours ( int crn );
void getPrefix ( string & prefix );
int getHowManyCourses ( int & start );
int getId ();
void printInvoice ( int crn1, int crn2, int crn3 );

//----------------------------
void printInvoice ( int crn1, int crn2, int crn3 )
{
     cout <<"\n\nThank you!\n";
    
     cout <<"HERE IS THE FEE INVOICE...\n\n";
     cout <<"\n\n";
     cout <<"\t\t\tVALENCE COMMUNITY COLLEGE\n";
     cout <<"\t\t\tORLANDO FL 10101\n";
     cout <<"\t\t\t*************************\n\n";
     cout <<"\t\t\tFee Invoice Prepared for Student V5656\n\n";
     cout <<"\t\t\t1 Credit Hour = $120.25\n\n";
     cout <<"\t\t\tCRN\tCredit Hours\n";
}

//----------------------------
int getId ()
{
    int i;
    cout << "Enter the Students id : \n\t";
    cin >> i;
    return i;
    
}
//----------------------------
int getHowManyCourses ( int start )
{
    cout <<"Enter how many courses - up to three(3) : \n\t";
    cin >> start;
    
    if ( start <= 3 )
         {               
           cin >> start;
         }
    else 
         { 
           cout << "Invalid number! Try Again!\n\n";
         }
    return (start+1);
}
//----------------------------
void getPrefix ( int crn , string &prefix )
{
    switch ( crn )
    {
       case 9696 : prefix = "COP 100"; break;
       case 4599 : prefix = "COP 220"; break;
       case 4587 : prefix = "MAT 236"; break;
       case 8997 : prefix = "GOL 124"; break;
    }//end of switch
     
}
//-----------------------------
int getHours ( int crn )
{
    switch ( crn )
    {
       case 9696 : 
       case 4599 : return 3;
       case 4587 : return 4;
       case 8997 : return 1;
    }//end of switch
}
//------------------------------

int main ()
{   
    int id;
    id = getId ();
    
    int courses = 2;
    int answers = getHowManyCourses (courses);
    return 0;
 
    int crn , hours;
    string prefix = "UNKNOWN";
    
    cout <<"Enter a course number : \n\t";
    cin >> crn;
    
    hours = getHours ( crn );
    getPrefix ( crn , prefix ); 
    
    cout << prefix <<" has :"<< hours <<" Credit Hours!\n\n";
    system ("pause");
    
}
ok.. I'm not gonna go through the entire code.. but i am gonna answer the original question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int getHowManyCourses ( int start )
{
    cout <<"Enter how many courses - up to three(3) : \n\t";
    cin >> start;
    
    if ( start <= 3 )
         {               
           cin >> start;
         }
    else 
         { 
           cout << "Invalid number! Try Again!\n\n";
         }
    return (start+1);
}


here you are declaring start as a parameter to the function
You then call cin >> start, erasing whatever you originally had passed. (which in this case you don' t need to pass anything)

Then you check what they entered and if it's less than or equal to 3, which is what you wanted, you prompt them to enter it again.

so to start off with. in this case you don't need to pass anything so
int getHowManyCourses ()
then we declare the int inside the function
int start;
we prompt the user to enter the code and then get thier input
1
2
cout << "Enter how many courses - up to three(3) : \n\t";
cin >> start;

Then check if it's correct. and don't input again.

Entire code should be
1
2
3
4
5
6
7
8
9
10
11
int getHowManyCourses()
{
   int start; // could also name this num or whatever
   cout << "Enter how many courses - up to three(3) : \n\t";
   cin >> start; // get input

   if (start <= 3) // if it's a valid number
      return start + 1; // return the number (you've got start +1, so I do too)

   cout << "Invalid Number!"; // give error
   return -1; // return a number to represent an error 


Then in your main
1
2
3
4
5
int answers = getHowManyCourses(); // get the result
if (answers == -1) // if error
{
   ... // do something here to recover from error
}
having read the description a little more, i see that while this is working code, it doesn't fit the description of the course.. The project asks you to pass a refference to something.. which is probably an array or object that contains the courses or something. It also asks for a while loop to keep getting input if the input isn't <= 3..

so look into adding a while loop like the following
1
2
3
4
5
6
7
cin start; // get original input
while (start > 3) // while start is invalid
{
   cout << "Invalid Number\n";
   cout << "Enter a number - up to three(3)\n\t";
   cin start; // enter again
}


again I can't do all the work, but this will get ya on the right track
My code was merely a representation of how to declare a variable BEFORE you pass it into a function.

e.g
1
2
3
4
int something;
changeValue(something); // Good

changeValue(int somethingelse); // BAD 
ok.. now i see what they want you to do..
by passing the refference to the int, they want you to declare the int outside the function then edit it inside the function.. so yea..

when passing the refference you use

changeValue(int &somethingelse);

so.. back to the original problems.. first you do need to change the code inside the function.. but for calling it

First declare it as
void getHowManyCourses(int &nbrCourses)

// notice we're not returning anything this time and use the & for refference

then in the main function
int courses; // or int courses = 2 if you want
getHowManyCourses(courses); // notice, not (int courses)
Last edited on
Thanks everyone so much !!! I've got tons to work with now!
Topic archived. No new replies allowed.