-
Help Please!
Im learning c++ and im having problems with functions!
The line marked with // THIS LINE IS PROBLEM! has the following errors (please help me fix) :
Code:
1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(18) : error C2144: syntax error : 'int' should be preceded by ')'
1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(18) : error C2660: 'favnumber' : function does not take 0 arguments
1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(18) : error C2059: syntax error : ')'
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int favnumber(int number);
int main()
{
int input;
cout<<"Welcome to Ant1jr's Test Software!\n";
cout<<"1. Favorite Number \n";
cout<<"Input: ";
cin>> input;
switch ( input ) {
case 1:
favnumber( int number ); // THIS LINE IS PROBLEM!
break;
}
}
int favnumber(int number)
{
//The computer asks your fav number
cout<<"Hello, what's your favorite number? \n";
//You input your fav number
cin>> number;
cin.ignore();
//The computer responds with "Your favorite number is <number>!"
cout<<"Your favorite number is "<< number <<"! \n";
cin.get();
// If your favorite number is 2 you are identified as grandma.
if ( number == 2 ) {
cout<<"Hello Grandma! \n";
// Press enter and you are told "Bye Grandma!"
cin.get();
cout<<"Bye Grandma!";
}
else if ( number == 1337 ) {
cout<<"Hello Dad! \n";
// Press enter and you are told "Bye Dad!"
cin.get();
cout<<"Bye Dad!";
}
else if ( number == 42 ) {
cout<<"Hello Milo! \n";
// Press enter and you are told "Bye Milo!"
cin.get();
cout<<"Bye Milo!";
}
// If you input 666 it will contantly print "UNHOLY DEMON!" until you close the application.
else if ( number == 666 ) {
for ( int x = 0; x >= 0; x++ ) {
cout<<"UNHOLY DEMON! ";
}
}
// If you dont input 2, 1337, 42, or 666 you are told your number is low if <50 or high if >50"
else {
if ( number <= 50 ) {
cout<<"You have a low favorite number! \n";
// Press enter and you are told "Bye!"
cin.get();
cout<<"Bye!";
}
else {
cout<<"You have a high favorite number! \n";
cin.get();
// Press enter and you are told "Bye!"
cout<<"Bye!";
}
}
//Press enter to close.
cin.get();
}
-
Take a closer look at the functions tutorial. When you call a function, you don't specify the datatypes (because the variables you are using already do).
The functions tutorial
Here's an example.
Code:
void hello(int x) { //Do something }
int main()
{
int bob = 10;
hello(bob); //CORRECT
hello(int bob); /*INCORRECT -- The compiler already knows that "bob" is an int, and when
you do that, it thinks you are trying to create a new variable called bob
(which is invalid inside of a function call like that).*/
}
Hope that all helps.
-
I tried removing int from the problem line but it still doesnt work! I don't understand...
-
Also, you need to tell it what variable you are passing.
Notice how you have
Code:
int input;
cin >> input;
fav_number (number); //Incorrect, although the parameter for fav_number is "number," that is irrelevent.
// You need to pass it a local variable. You probably want "fav_number(input);"
-
When I change it to input it says
'favnumber' : must return a value
-
If you don't want the function to return a value then maybe declare it as void.
-