Help me find the error, because I can't

So I am writing this code for my summitive in computer science (Grade 11). First thing you need to know is all this writing into and getting data from files was not included in the course, this is something I am exploring with. Also I am aware there are places with code that is not required but that is not the problem with my code from what I can tell.

PS. This code will only run on windows computers as it includes this library
 
#include <windows.h> 


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
/* 
 * File:   main.cpp
 * Author: dat boi
 *
 * Created on January 10, 2017, 1:40 PM
 */

#include <cstdlib>
#include <iostream>
#include <string>
#include <windows.h>
#include <fstream>

using namespace std;

/*
 * 
 */
int EvansTradingSystem();

string newUserAccountSetUp(string username, string password);

string login(){
    string username;
    string password;
    
    cout << "Login" << endl;
    cout << "Username: "; cin >> username;
    cout << "Password: "; cin >> password;
    
    newUserAccountSetUp(username, password);
}

string newUserAccountSetUp(string username, string password) {

    int userBalance;
    ifstream inFile;
    ofstream outFile;
    string passwordCheck;
    bool passwordMatch = false;
    int count = 5;
    
    inFile.open(username.c_str());
    
    if(inFile.fail()){
        
        inFile.close();
        
        userBalance = 100;
        
        cout << "Create your Account\n";
        cout << "Choose your username: "; cin >> username;    
        cout << "Create your password: "; cin >> password;
        cout << "Confirm your password: "; cin >> passwordCheck;

        if(password == passwordCheck){
                passwordMatch = true;
            }else{
                system("CLS");
            }

        while(passwordMatch != true){
            cout << "Passwords do not match\n";
            cout << "Attempts: " << count << endl;   
            cout << "Create your password: "; cin >> password;
            cout << "Confirm your password: "; cin >> passwordCheck;

            if(password == passwordCheck){
                passwordMatch = true;
            }else{
                system("CLS");
                count--;
            }

            if(count == 0){
                cout << "Passwords do not match";
                return 0;
            }

        }

        outFile.open(username.c_str());
        outFile << username << endl;
        outFile << password << endl;
        outFile << userBalance << endl;
        outFile.close();
    }
    
    inFile >> username >> password >> userBalance;
    
    outFile.open(username.c_str());
    outFile << username << endl;
    outFile << password << endl;
    outFile << userBalance << endl;
    outFile.close();
    
    inFile.close();
}

int EvansTradingSystem(){
    
    int userBalance; // ($$$)
    ofstream outFile;
    ifstream inFile;
    string username;
    string password;

    inFile.open(username.c_str());
    inFile >> username >> password >> userBalance;
    inFile.close();
    
    cout << userBalance;
    
    outFile.open(username.c_str());
    outFile << username << endl; 
    outFile << password << endl; 
    outFile << userBalance << endl;
    outFile.close();
    
    cout << "\n\n";
    return 0;
}
Last edited on
A number of issues with the code.

Function string login() should return a string, but doesn't return anything.
Function string newUserAccountSetUp() should return a string, but doesn't return anything. Actually under some circumstances it returns an integer value - which is not a string.

In main(), the filename is the default value of empty string, "".
1
2
3
    ifstream inFile;
    string username;
    inFile.open(username.c_str());


Don't know what else might be wrong, those were just things which stood out.
Thanks so much for the advice! After looking over my code with my teacher we ended up fixing the error with your help Chervil.
Topic archived. No new replies allowed.