How to switch back to main

Hi there, I am trying

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

int main()
{
    char selection;

    do
    {
        cout << "  Writing Data To File - \n";
        cout << "  ====================================\n";
        cout << "  1.  Start Program\n";
        cout << "  2.  Exit\n";
        cout << "  ====================================\n";
        cout << "  Enter your selection: ";
        cin >> selection;
        cout << endl;

        switch (selection)
        {
            case '1':
                cout << "Lets Begin";
                cout << "\n";
                break;

            case '2':
                cout << "Goodbye.\n";
                break;

            default: cout <<selection << " is not a valid menu item.\n";

                cout << endl;
        }

    }while (selection != '2');


As you can probably see I want the program to terminate upon the user inputting the number 2 when prompted, and this works fine, however I am trying to make it so that when the user enters the number 1, the program then starts which is this next bit -

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
    string tName, tDob, tPob; //declare a string to hold the user input





cout <<"Please enter your name: "; //prompt the user to enter a line of text
getline(cin, tName); //read in the line of text and store it in "input"
cout <<"\nPlease enter your date of birth (DD/MM/YYYY): ";
getline(cin, tDob);
cout <<"\nPlease enter your place of birth: ";
getline(cin, tPob);
cout <<"\nExit the program? y/n";
cin  << exit;


//
ofstream outfile("output.txt"); // ofstream is the data type in C++ for output files / declare and open an output file called output.txt.

//print out the input string to the file with a new line.

outfile << tName << "; " << tDob << "; " << tPob << endl;


//close the output file
outfile.close();
    }


As you can see, I am writing user input to a file, this isnt so much the problem, the problem im having is I dont know how to get the program to run once the user has entered the number 1.
(1) You forgot to declare "exit" as a string variable

(2) cin << exit;

Use the reverse insertion operator for cin.

(3) Your program doesnt actually end if '2' is entered, it simply continues to the second part of your code. You need to add a "return 0;" under case '2' before the break if you want it to end.


A goto statement is a quick way to solve your problem. Simply add a goto statement under case '1' before the break, and add the goto label right before the start of the program. See below:

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 <fstream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
    char selection;

    do
    {
        cout << "  Writing Data To File - \n";
        cout << "  ====================================\n";
        cout << "  1.  Start Program\n";
        cout << "  2.  Exit\n";
        cout << "  ====================================\n";
        cout << "  Enter your selection: ";
        cin >> selection;
        cout << endl;

        switch (selection)
        {
            case '1':
                cout << "Lets Begin";
                cout << "\n";
                goto start;
                break;

            case '2':
                cout << "Goodbye.\n";
                return 0;
                break;

            default: cout <<selection << " is not a valid menu item.\n";

                cout << endl;
        }

    }while (selection != '2');


start:
   string tName, tDob, tPob, exit; //declare a string to hold the user input

cout <<"Please enter your name: "; //prompt the user to enter a line of text
getline(cin, tName); //read in the line of text and store it in "input"
cout <<"\nPlease enter your date of birth (DD/MM/YYYY): ";
getline(cin, tDob);
cout <<"\nPlease enter your place of birth: ";
getline(cin, tPob);
cout <<"\nExit the program? y/n";
cin  >> exit;


//
ofstream outfile("output.txt"); // ofstream is the data type in C++ for output files / declare and open an output file called output.txt.

//print out the input string to the file with a new line.

outfile << tName << "; " << tDob << "; " << tPob << endl;


//close the output file
outfile.close();
    }
Last edited on
If line 27 is executed, the program will automatically jump to line 43.
I think it would be better practice to make use of functions rather than goto
I think it would be better practice to make use of functions rather than goto


Thats also a solution, and I guess more in line with normal programming practice.
Thanks for the help guys! My only problem now is when I run the program and select option 1, it throws up all the questions when they should appear one by one, how do I fix this :/ sorry to be a pain im a real noob with c++!
Also, what would I have to do to the program to so that when the user has finished entering the data, the program willoutput a copy of what is in the
file? (read from the file)
This might sound like a bit of a bad answer, but there is plenty of material out there! Many people have already asked these questions and many people have given them solutions. Find them and interpret them into your own code as your OWN solution. That will help you develop better skills and can be pretty rewarding when YOU figure out what to do with what is given.
Topic archived. No new replies allowed.