Confused myself

Hey,
I'm trying to get my program so that, at any question, pressing the 'r' key will return to, main (). However i am also trying to carry out other if-else funtions using numbers and i have managed to get myself confused.

Here is the code

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
int takeaway ()
{
    Result a,b ;
    int c,i,count = 0 ;
    char y;
    string home;
    do 
    {
    int *p;   

cout << "\n How many numbers are you trying out?" ;
getline (cin, home);
if (home=="r" || home == "0")
{    
    return (0);
}
else
{
 i=atoi(home.c_str());
    p= new (nothrow) int[i];
    for (c=0; c<i; c++)
    {
        cout << "\n Enter what you want taken away from Integer?";
  getline (cin, home);
  if (home=="r")
  {
      return (0);
  }
  else
  {
  cout << "\n Enter starting integer: ";
  getline (cin,home);
cin >> p[c]; 
a=p[c];
if (home=="r")
{
    return (0);
}
else
{
p[c] = atoi( home.c_str() );
a= p[c];
while (a>3)
a = a-b; 
count++;
cout << "\n Remainder: " << a  ;
{       
  cout<< "\n Number of times repeated: "  << count ;         
       }}
}
    }
    cout << "\n Go back to home screen? (y/n) " ;
    getline (cin,home);
    if (home=="y" || home=="r")
    {
        return (1) ;
    }
    else if (home=="n")
    {
        end () ;
    }
        else
    {
        cout << "Invalid " << endl ; //next loop writes "Enter choice: " after this
    }

        delete [] p;
}
    }while (i !='x' && i !='X');
    
}

int divide () 
{
    Result a; 
    int c,i ;
    char y;
    string home;
    do 
       
    {
    int *p;    
    {
cout << "\n How many numbers are you trying out?"  ;
getline (cin,home);
if (home=="r")
{    
    return (0);
}
else
    for (c=0; c<i; c++)
    {
   i=atoi(home.c_str());
   p = new (nothrow) int[i];    
  cout << "\n Enter starting integer: ";
cin >> p[c]; 
a=p[c];
if (a=='r')
{
    return (0);
}
else 
{
while (a>3)
a=a%4;
       
cout << "\n Remainder: " << a  ;  
    }
    }
   cout << "\n Go back to home screen? (y/n) " ;
    cin >> y;
    if (y=='y' || y=='r')
    {
       return (0);
    }
    else if (y=='n')
    {
        end () ;
    }
        else
    {
        cout << "Invalid " << endl ; //next loop writes "Enter choice: " after this
    }
            
    }        
          delete [] p;
    }while (i !='x' && i !='X');

}


Any help would be nice

Thanks
Do something about that code formatting of yours. I have troubles even figuring out where your blocks start and where they end.
Last edited on
Choosing better variable names would help too. a, b, c, y and p are not particularly helpful for understanding what the code is supposed to do.
From what I gather, you want to give the user a bunch of options, and if the user inputs 0 or R for any of those options, you'll start over? If so, you could do something like this:

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
int main()
{
    bool Running = true;
    string choice;
    
    while(Running)
    {
        cout << "Choose: 1) This, 2) This, R) Restart\n> ";
        
        cin >> choice;
        
        switch(choice)
        {
            case "1":
                // Do this
                break;
            case "2":
                // Do this
                break;
            case "R": // or use ifstatements to have R || r
                // User restarts
                continue;
                break;
            default:
                // Error
                break;
        }
        
        cout << "Choose next part: 1)...";
        
        // switch/ifstatements here
    }
}


Or to go back to a previous section, you could use progress markers:

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
int main()
{
    bool Running = true;
    int choice, progress = 1;
    
    while(Running)
    {
        switch(progress)
        {
            case 1:
                cout << "Choose: 1) Do this, 2) Go to part2\n> ";
                cin >> choice;
                if(choice == 1){
                    cout << "Doing section 1";
                } else if(choice == 2){
                    progress = 2;
                    cout << "Moving to next section";
                    continue;
                } else {
                    // ERROR
                }                
                break;
            case 2:
                cout << "Choose #2: 1) This, 2) This, 3) Go to part1, 4) Go to part3\n";
                cin >> choice;
                //...
                if(choice == 3){
                    progress = 1;
                    continue;
                } else if(choice == 4){
                    progress = 3;
                    continue;
                }                
                break;
            case 3: //...
        }
    }
}
Thank you so much WhiteWind been a great help.

Yeah im going to start naming my varibles

Thanks again
Topic archived. No new replies allowed.