error calling function

hi, im still new to programming so im sorry about the code if its not formated well.
im having trouble calling 2 functions inside of a switch statement.



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

#include <iostream>
#pragma hdrstop
using namespace std;

void displayOdd ( int num1, int num2);    // function prototype
void displayEven ( int num1, int num2);   // function prototype

//---------------------------------------------------------------------------

#pragma argsused
int main()
{
	int choice;
	int num1,num2;

	cout << "enter numbers to display the odd or even between input numbers\n"
		  << "(quit to exit)";
		cin >> num1;  //between this number
		cin >> num2;  //and this number


		cout << "Enter your selection ( 1 or 2 ): ";

		cin >> choice;
		switch(choice)
		{
			case 1:
				displayOdd ( int num1, num2);   //calls function
				cout<<"testing";
				break;

			case 2:
				displayEven ( int num1, num2);   //calls second function
				break;

			default:
				cout<< "Your selection must be between 1 and 2!\n";
		}



	cout<<endl<<endl<<endl<< "end of program";
	system("PAUSE");
	return 0;
}
//---------------------------------------------------------------------------
void displayOdd(int num1, int num2, int number)   //function defition
{
	cout << "Odd numbers are: ";
	 for(number = num1;number <= num2; number++)
	 {
		if(number % 2 !=0)
		cout << number<< " ";
			cout<<"Test65";
	 }
}
void displayEven( int num1,int num2,int number)    //function defition
{
	cout<< ("\nEven numbers are: ");
	 for(number = num1;number <= num2; number++)
		cout<<"test66";
		if(number % 2 ==0)

			cout << number << " ";
			cout << "test2";


}
Please, describe the trouble in detail. Compiler errors, perhaps?

To learn to interpret the compiler messages is useful. If you post the errors with the code, then we can help you to "see yourself".
only errors im getting, thanks for fast relpy

[bcc32 Error] File1.cpp(36): E2188 Expression syntax
Full parser context
File1.cpp(15): parsing: int main()

[bcc32 Error] File1.cpp(36): E2188 Expression syntax
Full parser context
File1.cpp(15): parsing: int main()
1. remove int from your calls on lines 29 and 34.
2. You are passing 2 params into your method calls, but your method definitions take two parameters, but only two parameters when you come to define your methods. This is wrong. they need to be in agreement with each other.
3. You are missing braces around the for loop in displayEven().
4. why the #pragma's everywhere?

read this:
http://www.cplusplus.com/doc/tutorial/functions/

a couple of times.
Last edited on
Here is the finished program, i have added comments so if other beginners need help, hopefully they can understand it :)

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

//---------------------------------------------------------------------------

#include <iostream>
#pragma hdrstop

using namespace std;

void displayFirstWay(int, int, char );
void displaySecondWay(int, int, char );    //problem was here
//function prototype has to have same parameters where the call and defition are.
//---------------------------------------------------------------------------

#pragma argsused
int main()
{
	int num1;
	int num2;
	char choice;
	int temp;

	cout << "enter first number to display: " ;
	cin >> num1;                          //between this number
	cout << "enter second number to display: " ;
	cin >> num2;                          //and this number

	if (num1 > num2 )
	{
		temp = num1;                      //if first number is bigger than second
		num1 = num2;                      // they are swapped
		num2 = temp;
	}

	cout << "Enter your selection ( 'o' or 'e' ): ";
	cin >> choice ;            //o for odd numbers, e for even numbers

		if (choice == 'o')
		{
			displayFirstWay( num1, num2, choice );
			cout << endl;
		}

			if(choice == 'e')
			{
				displaySecondWay( num1, num2, choice );
			}

	system("PAUSE");
	return 0;
}
//---------------------------------------------------------------------------
				  //first function defition
void displayFirstWay( int num1, int num2, char choice )
{
int number;
	cout << "Odd numbers are: ";
	for(number = num1;number <= num2; number++)
		{
			if(number % 2 !=0)
			cout << number<< " ";
		}
}

//---------------------------------------------------------------------------
					 //second function defition
void displaySecondWay( int num1, int num2, char choice )
{
int number;
	cout << ("\nEven numbers are: ");
	for(number = num1;number <= num2; number++)
		{
			if(number % 2 ==0)
			cout << number << " ";
		}

}
//--------------------------------------------------------------------------- 
why are you passing choice to your functions? You never use it in there. In other words, your prototypes should look like this:

1
2
void displayFirstWay( int num1, int num2);
void displaySecondWay( int num1, int num2);

You could even give them better names (DisplayEven, DisplayOdd)

And again, why the pragma nonsense?
also, it's good to initialise your variables:
1
2
3
4
5
        int num1;
	int num2;
	char choice;
	int temp;
Last edited on
Topic archived. No new replies allowed.