How return back to main from func.

I'm beginner so pleas explain me how i can move back to main from function as simple u can thx.


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



void Super(){


   char choice;
   std::cout << "Super pays 5$.\n";
   std::cout << "Do you want use super wash?\n";
   std::cout << "Y/N :";
   std::cin >> choice;
    switch(choice){
        case 'Y':
        std::cout << "Good choice your car look's clean.\n";
        break;
        case 'y':
        std::cout << "Good choice your car look's clean.\n";
        break;
        case 'N':

        break;
        case 'n':

        break;
        default:
        std::cout << "You have entered unable choice!\n";
    }



}

void Premium(){

 char choice;
   std::cout << "Premium pays 7$.\n";
   std::cout << "Do you want use premium wash?\n";
   std::cout << "Y/N :";
   std::cin >> choice;
    switch(choice){
        case 'Y':
        std::cout << "Good choice your car look's clean.";
        break;
        case 'y':
        std::cout << "Good choice your car look's clean.";
        break;
        case 'N':

        break;
        case 'n':

        break;
        default:
        std::cout << "You have entered unable choice!\n";
    }

}

void Ultra(){

 char choice;
   std::cout << "Ultra pays 10$.\n";
   std::cout << "Do you want use ultra wash?\n";
   std::cout << "Y/N :";
   std::cin >> choice;
    switch(choice){
        case 'Y':
        std::cout << "Good choice your car look's clean.";
        break;
        case 'y':
        std::cout << "Good choice your car look's clean.";
        break;
        case 'N':

        break;
        case 'n':

        break;
        default:
        std::cout << "You have entered unable choice!\n";
    }


}


int main(){


    std::cout << "Welcomen to car wash!\n";
    std::cout << "Pleas choose wash Super, Premium, Ultra!\n";
    std::cout << "1.)Super.\n";
    std::cout << "2.)Premium.\n";
    std::cout << "3.)Ultra.\n";



    int x[3];


  std::cin >> x[3];
  switch(x[3]){
      case 1:
      Super();
      break;
      case 2:
      Premium();
      break;
      case 3:
      Ultra();
      break;
      default:
      std::cout << "Unable choice!";
  }
 return 0;
}

You're creating an array of size 3, and then access element 4 in the array which is the null character('\0').
Still how i move function back to main?
Before i tryed call main(); also but diden't work, but finally released had no definition lol.

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


void Premium();
void Ultra();
void Super();
int main(){


    std::cout << "Welcomen to car wash!\n";
    std::cout << "Pleas choose wash Super, Premium, Ultra!\n";
    std::cout << "1.)Super.\n";
    std::cout << "2.)Premium.\n";
    std::cout << "3.)Ultra.\n";



    int x[4];


  std::cin >> x[3];
  switch(x[3]){
      case 1:
      Super();
      break;
      case 2:
      Premium();
      break;
      case 3:
      Ultra();
      break;
      default:
      std::cout << "Unable choice!";
  }
 return 0;
}

void Super(){


   char choice;
   std::cout << "Super pays 5$.\n";
   std::cout << "Do you want use super wash?\n";
   std::cout << "Y/N :";
   std::cin >> choice;
    switch(choice){
        case 'Y':
        std::cout << "Good choice your car look's clean.\n";
        break;
        case 'y':
        std::cout << "Good choice your car look's clean.\n";
        break;
        case 'N':
        main();
        break;
        case 'n':
        main();
        break;
        default:
        std::cout << "You have entered unable choice!\n";
    }



}

void Premium(){

 char choice;
   std::cout << "Premium pays 7$.\n";
   std::cout << "Do you want use premium wash?\n";
   std::cout << "Y/N :";
   std::cin >> choice;
    switch(choice){
        case 'Y':
        std::cout << "Good choice your car look's clean.";
        break;
        case 'y':
        std::cout << "Good choice your car look's clean.";
        break;
        case 'N':
        main();
        break;
        case 'n':
        main();
        break;
        default:
        std::cout << "You have entered unable choice!\n";
    }

}

void Ultra(){

 char choice;
   std::cout << "Ultra pays 10$.\n";
   std::cout << "Do you want use ultra wash?\n";
   std::cout << "Y/N :";
   std::cin >> choice;
    switch(choice){
        case 'Y':
        std::cout << "Good choice your car look's clean.";
        break;
        case 'y':
        std::cout << "Good choice your car look's clean.";
        break;
        case 'N':
        main();
        break;
        case 'n':
        main();
        break;
        default:
        std::cout << "You have entered unable choice!\n";
    }


}
I have no idea what the last piece of code was :S Scrap it. Return to the code in the original post. You're problem lies on line 99 and 102 on the opriginal post. Why are you creating an array? Why are you assigning the third element a value?
CodeSmoker wrote:
how i can move back to main from function
use a do while loop, while loop or the goto statement (which is evil, but does exactly what you want to happen i think)

read it here..
http://cplusplus.com/doc/tutorial/control/
What blackcoder said. Once you called a method, that method will be executed, and afterwards the execution will resume after the call. So basically, it's not like you would have to return to the main function, it's that it's impossible NOT to return to it (unless you force the program into an infinite loop or something like that O_O).

Put the whole content of the main function in a do while loop, it would look like this:
1
2
3
4
5
do
{
//Block of code
}
while (condition) // Boolean expression like in the If statement. If it's true, repeat the block. 


With minor changes in your code you could achieve what you want.

Oh, and don't use the goto statement. You will make Dijkstra cry.
http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html
A do/while loop is recommended for what you want to do.

You may only use a relative of the goto statement if you're programming in Assembly. Even then, you'll still probably make Dijkstra cry.

K bye.

-Albatross

P.S.- Your variable ought to be just a plain int. No brackets. :)
Allright ty all.
Topic archived. No new replies allowed.