-
Defining Functions.
I was working on Lesson 5: Switching Cases of cprogramming.com's C++ tutorials however the code given was incomplete, but the author said with little work I could define the functions that were left undefined in the source and get a running program...So I gave it a try, and bam - it didn't work! I looked over the code I studied from Chapter 4 on Functions and really can't see where I am messing up, so if someone could give me a hand, I'd appreciate it. Here is my code:
Code:
#include <iostream>
using namespace std;
#include <conio.h>
int playgame();
int loadgame();
int playmultiplayer()
int main()
{
int input;
cout<<"1. Play game";
cout<<"2. Load game";
cout<<"3. Play multiplayer";
cout<<"4. Exit";
cin>>input;
switch (input)
{
case 1: playgame();
break;
case 2: loadgame();
break;
case 3: playmultiplayer();
break;
case 4:
return 0;
default:
cout<<"Error, bad input, quitting";
}
int playgame()
{
cout<<"You are playing the game!"
}
int loadgame()
{
cout<<"The game is loading!"
}
int playmultiplayer()
{
cout<<"You are playing multiplayer!"
}
return 0;
}
C:/Documents and Settings/David Mackey/My Documents/cprograms/switchcases.cpp: In
function `int main()':
C:/Documents and Settings/David Mackey/My Documents/cprograms/switchcases.cpp:29: parse
error before `{' token
C:/Documents and Settings/David Mackey/My Documents/cprograms/switchcases.cpp: In
function `int loadgame()':
C:/Documents and Settings/David Mackey/My Documents/cprograms/switchcases.cpp:35: parse
error before `}' token
C:/Documents and Settings/David Mackey/My Documents/cprograms/switchcases.cpp: In
function `int playmultiplayer()':
C:/Documents and Settings/David Mackey/My Documents/cprograms/switchcases.cpp:39: parse
error before `}' token
Execution terminated
Thanks for your help in advance.
Respectfully,
David.
-
-
Code:
int playmultiplayer()
Missing a semicolon.
-
Additionally, if your functions don't return anything, give them a 'void' return type.
-
How do I make a function return a void type? I tried making sure that my functions where outside the main, and correcting all the errors you pointed out, but I'm still having problems. Here is the new code:
Code:
#include <iostream>
using namespace std;
#include <conio.h>
int playgame();
int loadgame();
int playmultiplayer();
int main()
{
int input;
cout<<"1. Play game";
cout<<"2. Load game";
cout<<"3. Play multiplayer";
cout<<"4. Exit";
cin>>input;
switch (input)
{
case 1: playgame();
break;
case 2: loadgame();
break;
case 3: playmultiplayer();
break;
case 4:
return 0;
default:
cout<<"Error, bad input, quitting";
}
}
int playgame()
{
cout<<"You are playing the game!"
}
int loadgame()
{
cout<<"The game is loading!"
}
int playmultiplayer()
{
cout<<"You are playing multiplayer!"
}
Respectfully,
David.
-
A function can not return a void type, because a void type indicates to the compiler that the function does not return anything. So to make one of your functions void do this:
void playmultiplayer()
-
Right now, you're expected to be returning an integer from your functions, because that is what you have specified with the return type:
This implies that you will have a return statement inside your function that returns an integer:
Code:
int playgame()
{
cout<<"You are playing the game!"
return 1; // For example.
}
Now, it is unlikely in this case that you could return a meaningful value from your function, so it is better to return nothing at all. To do this, specify the return type as void, and either omit the return statement, or have an empty return statement.
Code:
void playgame();
...
void playgame()
{
cout<<"You are playing the game!"
// No return statement.
}
...or...
void playgame()
{
cout<<"You are playing the game!"
return; // Empty return statement.
}
Hope this makes things clearer.
Cheers
-
Thanks for helping me clear that up. I also discovered I had one other error. If you look in my code you will notice at several points I was missing my trailing ;.
Respectfully,
David.