Reading and Writing From Text File & Label Problems

Hello. I have been working on a simple program (GamerNet), and I keep getting the same errors, no matter what modifications I use. Here's the source 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
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<conio.h>
using namespace std;


void title()
{
    cout<<"_________________________"
        <<"\n      GamerNet v2.0      "
        <<"\n-------------------------";
}

int main(int nNumberofArgs, char* pszArgs[])
{
    int ans, tries;
    tries = 0;
    string newUser, newPass, goodUser, goodPass, _pass, _user;
    string pass2, user2;

  start:
    system("cls"); //Clears the Screen
    title();
    cout<<"What would you like to do?"
        <<"\n1>Register for an Account"
        <<"\n2>Log in"
        <<"\n3>Exit GamerNet";
    cin>>ans;
    if (ans = 3) goto end;
    if (ans = 2) goto login;
    if (ans = 1) goto reg;

  reg:
    system("cls");
    cout<<"Enter New Username: ";
    getline(cin,newUser);
    cout<<"\nEnter New Password: ";
    getline(cin,newPass);

    //Write User Info to File
    ofstream new_data("Reg_Data.txt");
    new_data <<"Username: " << newUser
             <<"\nPassword: " << newPass;
    new_data.close();

    //Inform the User of Registration Success
    system("cls");
    cout<<"Welcome, " <<newUser <<" to GamerNet (v2.0)!"
        <<"\n >Here, you'll find the latest news of the gaming world!";
    system("pause");

  login:
    system("cls");
    cout<<"Enter Username: ";
    getline(cin,goodUser);
    cout<<"\nEnter Password: ";
    getline(cin,goodPass);

    //Read Data from Text File
    ifstream new2_data;
    new2_data.open("Reg_Data.txt");
    char output[100];
    if (new2_data.is_open())
    {
        getline(new2_data,_user);
        getline(new2_data,_pass);
    }
    new2_data.close();
    system("cls");

    //Verify Data from Text File
    if (goodUser == _user and goodPass == _pass)
    {
        cout<<"Login Successful!  Please enjoy our services!\n";
        system("pause");
        goto normal;
    }
    else
    {
        if (tries < 3)
        {
        cout<<"Invalid Login!  Please try again.\n";
        system("pause");
        tries++;
        }
        else
        {
        cout<<"Invalid Login!\n";
        system("pause");
        goto end;
        }
    }


    //Default Stop for All GamerNet (v2.0) Users
  normal:
    cout<<"\nLatest News: Miyamoto has released the NEW Super Mario Bros. Wii!"
        <<"\n  The game has already brought in millions for Nintendo.\n";
  end:
    cout<<"Exiting System...\n";
    system("pause");
    return 0;
}


I'm using CodeBlocks and the errors I am getting are a jump to label error and a cross initialization error (reading and writing of text file). I have searched the Web but couldn't find anything helpful. Help would be greatly appreciated.
Thanks!
Hi! I modified your code a bit to make it work, and also fixed some syntax and logical errors which would prevent it from working the way you would want... However, I would advice you to stop using labels. If you want to break your code to small parts use functions instead. Here is a working version of your 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
130
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<conio.h>
using namespace std;


void title()
{
    cout<<"_________________________"
        <<"\n      GamerNet v2.0      "
        <<"\n-------------------------";
}

int main(int nNumberofArgs, char* pszArgs[])
{
    int ans, tries;
    tries = 0;
    string newUser, newPass, goodUser, goodPass, _pass, _user;
    string pass2, user2;

    start:
    {
        system("cls"); //Clears the Screen
        title();
        cout<<"What would you like to do?"
            <<"\n1>Register for an Account"
            <<"\n2>Log in"
            <<"\n3>Exit GamerNet" << endl;
        cin>>ans;
        
        cin.clear();
        while (cin.get()!='\n');
        
        if (ans == 3) goto end;
        if (ans == 2) goto login;
        if (ans == 1) goto reg;
        
        goto start;
    }

    reg:
    {
        system("cls");
        cout<<"Enter New Username: ";
        getline(cin,newUser);
        cout<<"Enter New Password: ";
        getline(cin,newPass);
    
        //Write User Info to File
        ofstream new_data("Reg_Data.txt");
        new_data <<"Username: " << newUser
                 <<"\nPassword: " << newPass;
        new_data.close();
    
        //Inform the User of Registration Success
        system("cls");
        cout<<"Welcome, " <<newUser <<" to GamerNet (v2.0)!"
            <<"\n>Here, you'll find the latest news of the gaming world!";
        system("pause");
        goto start;
    }
    
    login:
    {
        system("cls");
        cout<<"Enter Username: ";
        getline(cin,goodUser);
        cout<<"Enter Password: ";
        getline(cin,goodPass);
    
        //Read Data from Text File
        ifstream new2_data;
        new2_data.open("Reg_Data.txt");
        //char output[100];
        if (new2_data.is_open())
        {
            new2_data.ignore(10); //ignoring "Username: "
            getline(new2_data,_user);
            new2_data.ignore(10); //ignoring "Password: "
            getline(new2_data,_pass);
        }
        new2_data.close();
        system("cls");
    
        //Verify Data from Text File
        if (goodUser == _user and goodPass == _pass)
        {
            cout<<"Login Successful!  Please enjoy our services!\n";
            system("pause");
            goto normal;
        }
        else
        {
            if (tries < 3)
            {
            cout<<"Invalid Login!  Please try again.\n";
            system("pause");
            tries++;
            goto login;
            }
            else
            {
            cout<<"Invalid Login!\n";
            system("pause");
            goto end;
            }
        }
    }

    //Default Stop for All GamerNet (v2.0) Users
    normal:
    {      
           system("cls");
           cout<<"Latest News: Miyamoto has released the NEW Super Mario Bros. Wii!"
               <<"\nThe game has already brought in millions for Nintendo.\n";
           system("pause");
    }
  
    end:
    {
        system("cls");
        cout<<"Exiting System...\n";
        system("pause");
        return 0;
    }
}

Thanks! While you modified my code, I was searching around some more and found my answer in this link: http://lists.apple.com/archives/Xcode-users/2005/Jun/msg00667.html
And I know I shouldn't be using labels, but right now I'm just a beginner and am experimenting with different techniques.
Last edited on
Topic archived. No new replies allowed.