Reguarding 04 tutorial ( prototypes )
Code:
#include <iostream>
using namespace std;
int mult ( int x, int y );
int main()
{
int x;
int y;
cout<<"Please input two numbers to be multiplied: ";
cin>> x >> y;
cin.ignore();
cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
cin.get();
}
int mult ( int x, int y )
{
return x * y;
}
The above snippet is from http://www.cprogramming.com/tutorial/lesson4.html ...
Why are prototypes used? wouldn't it be cleaner too write the functions before the required one? I'm confused on why they are needed.
If someone could explain :) I didn't see it mentioned in the tutorial of why so I ask here.
Another Prototype Problem:
Here i'm trying to finish the 05 lessons by writing up the functions for the voids in their code. But it complains on me, this is what I got so far:
Code:
#include <iostream>
using namespace std;
int playgame();
int loadgame();
int playmultiplayer();
int main()
{
int input;
cout<<"1. Play game\n";
cout<<"2. Load game\n";
cout<<"3. Play multiplayer\n";
cout<<"4. Exit\n";
cout<<"Selection: ";
cin>> input;
switch ( input ) {
case 1: // Note the colon, not a semicolon
playgame();
break;
case 2: // Note the colon, not a semicolon
loadgame();
break;
case 3: // Note the colon, not a semicolon
playmultiplayer();
break;
case 4: // Note the colon, not a semicolon
cout<<"Thank you for playing!\n";
break;
default: // Note the colon, not a semicolon
cout<<"Error, bad input, quitting\n";
break;
}
cin.get();
}
int playgame() {
cout<<"Yeaha Baby we're playing!\n";
}
int loadgame() {
cout<<"Bummer.. you cheating bastard, loading a game are you?\n";
}
int playmultiplayer() {
cout<<"Let the multiplayer mayhem begin...\n";
}
I changed void prototypes too int
defined the functions below main
It works too compile, and run but the compiler complains and was wondering if i'm doing something bad....
the compile error:
Quote:
g++ -c -Wall lesson2.cpp -o lesson2.o
lesson2.cpp: In function ‘int playmultiplayer()’:
lesson2.cpp:47: warning: control reaches end of non-void function
lesson2.cpp: In function ‘int loadgame()’:
lesson2.cpp:44: warning: control reaches end of non-void function
lesson2.cpp: In function ‘int playgame()’:
lesson2.cpp:41: warning: control reaches end of non-void function
g++ lesson2.o -o hello
Originally it looked like this:
Code:
#include <iostream>
using namespace std;
void playgame();
void loadgame();
void playmultiplayer();
int main()
{
int input;
cout<<"1. Play game\n";
cout<<"2. Load game\n";
cout<<"3. Play multiplayer\n";
cout<<"4. Exit\n";
cout<<"Selection: ";
cin>> input;
switch ( input ) {
case 1: // Note the colon, not a semicolon
playgame();
break;
case 2: // Note the colon, not a semicolon
loadgame();
break;
case 3: // Note the colon, not a semicolon
playmultiplayer();
break;
case 4: // Note the colon, not a semicolon
cout<<"Thank you for playing!\n";
break;
default: // Note the colon, not a semicolon
cout<<"Error, bad input, quitting\n";
break;
}
cin.get();
}