Function Help

New coder here, Only learned what they taught me in school. In this code, I kinda got it down, but I am having a bit trouble with my functions. I am using C++ 4.9.9.2, I relize some of this wont work cause its not done, but I just need my function all figured out if anyone is willing to help. Thanks!

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
#include <iostream>

//Sean; p.1

using namespace std;

void function(int), cont(int), con(string);

     int seatNum ,  rep , array[16] , z = 1 ;
     string n,y,yesNo;
      
     int main (){

    cout << "Please enter a seat number 1-15 enter 0 for chart : ";
    cin >> seatNum;    
     

    if (array[seatNum] == 0){
    cout << "Seat     1=taken \n" <<endl;
    for (int x = 1; x < 16 ; x++)
    cout << x << "\t" << array[x] <<endl;
    con(cont);
 }

    if(arry[seatNum] == 0 && seatNum !=0 && seatNum <=15){
    cout << "Seat " << seatNum << " has been reserved." << endl;


    system("pause");
    return 0;
}

    void con(int cont){
    cout << "\nContinue y/n ?";
    cin >> cont;
    
    if(cont == "y"){
    cout << string (30, '\n') << endl;
    function(seatNum)
   }
    
    else if(cont != "n"){
    cout << cont << " is invalid. \n " << endl;
    con (cont);
 }
}
    
MiniclipSean wrote:
I am using C++ 4.9.9.2
No - there is C++98, C++03, C++11 (previously C++0x), and C++1y (soon to be C++14). The version number 4.9.9.2 appears to be from your IDE - would you by chance be using Dev-C++? Please read:
http://www.cplusplus.com/articles/36vU7k9E/
Don't use it unless an outdated college professor with tenure threatens your college career.
$ /usr/bin/c++ --version
c++ (GCC) 4.8.2


@OP: Describe the problem.
if you want people to read your code, you need to present it in a readable way. That mean layout!

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
#include <iostream>

//Sean; p.1

using namespace std;

void function(int); // never optimise function declaration
void cont(int);
void con(string);

int seatNum;
int rep;
int my_array[16]; // array is a misleading name for data
int z = 1 ;
string n;
string y;
string yesNo;

// use proper indentation
      
int main ()
{

    cout << "Please enter a seat number 1-15 enter 0 for chart : ";
    cin >> seatNum;    
     
    if (my_array[seatNum] == 0)
    {
        cout << "Seat     1=taken \n" <<endl;
        for (int x = 1; x < 16 ; x++)
            cout << x << "\t" << array[x] <<endl;
        con(cont);
    }

    if(arry[seatNum] == 0 && seatNum !=0 && seatNum <=15)
    {
        cout << "Seat " << seatNum << " has been reserved." << endl;

        system("pause");
        return 0;
    }


void con(int cont)
{
    cout << "\nContinue y/n ?";
    cin >> cont;
    
    if(cont == "y")
    {
        cout << string (30, '\n') << endl;
        function(seatNum);
    }
    else if(cont != "n")
    {
        cout << cont << " is invalid. \n " << endl;
        con (cont);
    }
}


now its readable, check your braces {} check your variable names and type, check your function names and parameters.
@L B Yes, I am using Dev C++, sorry about that.

@jaybob66, I was missing 1 brace, but that didnt change anything seeing as my problem was on line 31. Also sorry about not naming them good. I never thought about other people seeing them. I will work on it a tad to see if I can get everything names to a point where people can understand it! :D
Btw, code looks a lot nicer now :D

@ne555
line 35 says "conversion from `void (*)(int)' to non-scalar type `std::string' requested

From there its arry undeclared and this at 50 and 55 "ISO C++ forbids comparison between pointer and integer "

Changed the code a bit, found a few minor things, and I also explained EVERYTHING
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
67
68
69
70
71
#include <iostream>

//Sean; p.1

using namespace std;

void function(int); 
void cont(int);
void con(string);

int seatNum;
int rep;
int my_array[16]; 
int z = 1 ;
string n;
string y;
string yesNo;

// I explain in the code what is suppose to happen and what not...
      
int main ()
{

    cout << "Please enter a seat number 1-15 enter 0 for chart : ";
    cin >> seatNum;    //Standard cin
     
    if (my_array[seatNum] == 0)
    //If they enter 0, not anything else, a 2 columed chart will appear, on the right side 1-15 going down, on the left 0's going down
    //The 1's represent the seat is taken, 0's represent the seat is NOT taken and you can pick it.
    //Below is the whole chart
    {
        cout << "Seat     1=taken \n" <<endl;
        for (int x = 1; x < 16 ; x++)
            cout << x << "\t" << my_array[x] <<endl;
        con(cont); // <---- Main error I am having. This is suppose to go down below to the void con(int cont)
    }

//If the seat you choose is equal to 0 (not taken), the seat from the cin is real, and its smaller or = to 15,
// the stuff below it will print. I know thats a lot, but I think this works.
    if(my_array[seatNum] == 0 && seatNum !=0 && seatNum <=15)
    {
        cout << "Seat " << seatNum << " has been reserved." << endl;
        }
//systempause and return cause I am done with the Int part of the code. (This is what my class was told to use)
        system("pause");
        return 0;
    }

//Now for the void's. I am taking void con (declared as 'void cont(int);' and when  'con(cont)' get hit in the code
//the following will happen (I think i messed up in calling it or something)
void con(int cont)
{
    cout << "\nContinue y/n ?";
    cin >> cont;
    //cont is a void, should it be a string?
    
    if(cont == "y")
    {
        cout << string (30, '\n') << endl; // <-- If you continue, it will "clear" the screen and go back to choosing MORE seats
        function(seatNum);
    }
    
    // I dont know what Im doing here yet, haha, Im just tring to get the code to work.
    else if(cont != "n")
    {
        cout << cont << " is invalid. \n " << endl;
        con (cont);
    }
}

return;

1
2
    if (my_array[seatNum] == 0)
    //If they enter 0 
You've written `if the array holds a 0 in the entered position'
It should be if( seatNum == 0 ) according to your comment


con(cont); // <---- Main error I am having. This is suppose to go down below to the void con(int cont)
there is no `void con(int cont)'
there is `void cont(int)' and `void con(string)' Using meaningful names would make it less confusing.
If you want to call `void cont(int)' you may do it as `cont(42)' (your function is not making use of the argument, more on that later).


1
2
3
4
5
6
	//If the seat you choose is equal to 0 (not taken), the seat from the cin is real, and its smaller or = to 15,
	// the stuff below it will print. I know thats a lot, but I think this works.
	if(my_array[seatNum] == 0 && seatNum !=0 && seatNum <=15)
	{
		cout << "Seat " << seatNum << " has been reserved." << endl;
	}
You are risking out of bounds there. First verify that the seat is valid, then check if it is taken
Maybe a misuse of the language on my part, but I understand the message as `you can't take that seat because someone else already reserve it'.


1
2
3
4
5
6
7
void con(int cont)
{
	cout << "\nContinue y/n ?";
	cin >> cont;
	//cont is a void, should it be a string?

	if(cont == "y")
It doesn't matter how do you call that function, because the first thing you do is to discard the argument. Note that `cont(42)' `cont(-1)' `cont(0)' would all be equivalent.
Then it seems that you want to read a character, but you asked for an integer ¿?

Also, ¿how is that function supposed to interact? it doesn't care about its parameters and doesn't return anything.
1
2
3
4
5
bool keep_going(){
   char answer;
   std::cin>>answer;
   return answer=='y';
}



1
2
		cout << string (30, '\n') << endl; // <-- If you continue, it will "clear" the screen and go back to choosing MORE seats
		function(seatNum);
¿what is `function()' ?


Line 71 `return' ¿what is that doing there?
You made me laugh so hard, I feel so stupid. Im going to over my whole code with myself and try to name stuff diffrent (Like the cont and con thing)
Topic archived. No new replies allowed.