Linking diffrent funtions

Hey,
I am very new to C++ and am tryng to write a program where after a task has been compleated the program links back to the start. The only problem im having is that they are in diffrent funtions.

Here is the code (ts very messy and incomplete but its one of my first tries)

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
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <string>
#include <sstream>
#include <new>
using namespace std;

int second ()
{
    int (main);
    int a,c,i,n,b,r ;
    int *p;
    string finish,go;
    char y;
    finish = "Have a nice day, and thanks for playing!" ;
    

cout << "\n How many numbers are you trying out?"  ;
cin >> i;
p= new (nothrow) int[i];
if (p==0)
{    
cout << finish;
}
else
{
    for (c=0; c<i; c++)
    {
  cout << "\n Enter starting integer: ";
cin >> p[c]; 
a=p[c];
while (a>3)
a=a-4;
       
cout << "\n Remainder: " << a  ;  
    }
    cout << "\n Go back to home screen? (y/n) " ;
    cin >> y;
    if (y=='y')
    {
        return ();
    }
    else if (y=='n')
    {
        cout << finish;
    }
            
            
          delete [] p;
}


}

void third () 
{
    
}

int main ()
{
    char choise;
    loop:
    cout << "Do you want to - or / " ;
    cin >> choise ;
    
    if (choise=='-')
    {
        second () ;
    }
  
    else if (choise=='/')
    { 
        third () ;
    }
    else 
    { 
        cout << "invalid selection" ;
        goto loop;
    }
return 0;
}


im trying to link the end of 'second' to the beging of 'main'

thanks in advance.
Don't use goto ;p

Use the do-while loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
do
{
    char choice;
    cout << "Enter choice: ";
    cin >> choice;
    if(choice == '-')
    {
        second(); //you should give your functions meaninful names,
    }
    else if(choice == '/')
    {
        third(); //for example, PlayGame() and DoGame() are meaningful
    }
    else
    {
        cout << "Invalid, "; //next loop writes "Enter choice: " after this
    }
}while(choice != 'x' && choice != 'X'); //exit when they type x or X 


By the way,

int (main);
This makes an integer named main. It'd be the same even without the ()

int a,c,i,n,b,r ;
Again, give your variables meaningful names! The only time you don't need to is for obvious for-loop variables like i, j, k, etc...

Your indenting on lines 17-49 is really bad, you should fix it up so it is easier to read ;)
Last edited on
Didn't know that even existed but thanks.

That wasnt the orginal problem though :P

Say i have just compleated the 'second ()' and wish to return to 'main()'
How could this be done?

Thanks alot for your help

It will return to main when the function ends, because main called it originally.
Topic archived. No new replies allowed.