Funtion


What is wrong with this?



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

void neg_msg(char a);
char a = "Negative";

void zero_msg (char b);
char b = "Zero";

void pos_msg (char c);
char c = "Positive";
int main()

{
	int a;
	cout << "Enter an integer value for variable a." 
	<<endl;
	cin >> a;
	
	if (a < 0)
	{cout << "The integer you have entered is "<< a
	<< " ." << endl;}

	if ( a == 0 )
	{ cout << " The integer you have have enter is " << b
	<< " ." << endl;}

	if ( a >0 )
	{ cout << " The integer ypu have entered is " << c 
	<< " ." << endl;
	return 0;
}

It is the compiler that says what is wrong with your code.
Char can only be 1 single letter of the Alphabet. For example 'A' or 'B' or 'c' it can not be full words. If you want to get full words try using a string. To use a string you will need to include the header, " #include <string>" this will give you want you want. For your functions you did not pass the value of int a. Remember you can use multiple names of something as long as they are not the same type it doesn't matter. So calling something char number and then using an int to say the same thing int number is two different things. Since one is a char(character) and int is a (integer or number). If you want to pass the value of a variable ( ex: int, char, float) to a function and have it actually effect the variable that you had pass the value from. You would need a & in the parameter of a function in order to pass the value by reference instead of just passing a copy. Lastly You had made your function but you did not say anymore in int main that you want to use the actual function. Remember that your whole program runs from int main. Next the functions themselves where not written correctly, you did not use {} to say this will happen for whatever function you made. So what
happen is that you made a prototype or a basic sketch of what the function will have but did not say what it will do. Line 30 you are missing the } for the if statement.

Here is a revision to your code. This is only show you one way that you could do this. Always remember there is many ways that this can be done.

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

#include <iostream>
#include <string>

using namespace std;

// This function Tells the user the integer is negative
void neg_msg()
{
    string negative = "negative";
    cout << " The integer you have entered is "<<negative<<endl;

}

// This function tells the user the integer is zero
void zero_msg ()
{
    string zero = "zero";
    cout << " The integer you have entered is "<<zero<<endl;

}

// This function tells the user the integer is zero
void pos_msg ()
{
    string positive = "positive";
    cout << " The integer you have entered is "<<positive<<endl;

}

int main () 
{
     int a;
     cout << " Enter an integer value for variable a."<<endl;
     cin >> a;

     if (a < 0)
     {

          neg_msg();

      }

      if (a==0)
      {

          zero_msg();

      }

      if (a > 0 )
      {

          pos_msg();

      }

        return 0;

}




You should check back to the part about functions in the tutorial if you are using that to learn from. to help reference or to clear any confusion you have about what I just wrote.

Cheers,
Stormhawk
Last edited on
Topic archived. No new replies allowed.