Functions

Can somebody help me organize my program so far, so that it has functions at top, and there definitions at the bottom, that way i can call any part of my program where it is needed. I also need help calling them to make them run like this program does. Just post the rewritten program please.


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

long callPlayerChoice; //completed code
long playGame; //in progress
string playerName; //completed code
long Instructions; //in progress
long Quit; //completed code

int main()
{

cout << "Please enter a number for the task you want to complete." << endl << endl;
cout << "1) Play" << endl;
cout << "2) Instructions" << endl;
cout << "3) Quit" << endl;

cin >> callPlayerChoice;
cin.ignore(80,'\n');
////////////////////////////////////////////////////////////////////////////
if(callPlayerChoice == 1) 
{ 
playGame = 1; 
} 
else if(callPlayerChoice == 2) 
{ 
Instructions = 2; 
} 
else{Quit = 3;
}; 
///////////////////////////////////////////////////////////////////////////// 
if(Quit == 3)
{
system ("CLS");
cout << "Thanks for playing!!! Hope you enjoyed!!!" << endl;
cin.get();
}; 
/////////////////////////////////////////////////////////////////////////////
if(Instructions == 2)
{
system ("CLS");
cout << "There are currently no instructions for this program." << endl;
cin.get();
};
//////////////////////////////////////////////////////////////////////////////
{
if(playGame == 1)
{
system ("CLS");
cout << "Welcome to the game! Since you are starting on your journey," << endl;
cout << "let me know a little bit about you.";

cout << " What is your full name?" << endl << endl;
getline(cin,playerName);
cout << "Nice to meet you " << playerName << "." << endl;
cin.ignore(80,'\n');

}
}
///////////////////////////////////////////////////////////////////////////////
system ("PAUSE");
return 0;
};
Last edited on
Eh... Thats a tad bit difficult to understand. Maybe if you indented everything inside each block by about 4 spaces I'd be able to help some more. What I can see here though is that you seem to have ended your main function with };. Does this compile without errors?
Yes ... agree with Buffer it is a tad difficult as your code does not appear to possess any functions other than get(), ignore() and getline() which do not require this treatment. I'm supposing that you want your function prototypes above "int main ()" and their definitions below "int main ()".
Hey you also ended some,
if()
{

};
make it correct please..
Here, I'll tidy up his code for you:
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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

long callPlayerChoice; //completed code
long playGame; //in progress
string playerName; //completed code
long Instructions; //in progress
long Quit; //completed code

int main()
{
     cout << "Please enter a number for the task you want to complete." << endl << endl;
     cout << "1) Play" << endl;
     cout << "2) Instructions" << endl;
     cout << "3) Quit" << endl;
     cin >> callPlayerChoice;
     cin.ignore(80,'\n');
     if(callPlayerChoice == 1) playGame = 1; 
     else if(callPlayerChoice == 2)  Instructions = 2; 
     else Quit = 3;

     if(Quit == 3)
     {
          system ("CLS");
          cout << "Thanks for playing!!! Hope you enjoyed!!!" << endl;
          cin.get();
          } 

     if(Instructions == 2)
     {
          system ("CLS");
          cout << "There are currently no instructions for this program." << endl;
          cin.get();
          }
     if(playGame == 1)
     {
          system ("CLS");
          cout << "Welcome to the game! Since you are starting on your journey," << endl;
          cout << "let me know a little bit about you.";

          cout << " What is your full name?" << endl << endl;
          getline(cin,playerName);
          cout << "Nice to meet you " << playerName << "." << endl;
          cin.ignore(80,'\n');
          }
     system ("PAUSE");
     return 0;
     }
Topic archived. No new replies allowed.