Click here to Skip to main content
15,910,886 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I tried to create a test login program.
(I know this isn't safe but I'm trying to create some things to learn).
This line gives an error: file.open(username + ".txt");
I think the + in that line doesn't work but I'm not sure.


This is the error:
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\fstream|702|note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]|


This is the code:
C++
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

enum MainProgramParts{ SUCCESS, FAILED, STOP, OPTIONS };
MainProgramParts options();
MainProgramParts success();
MainProgramParts failed();

int main()
{
        MainProgramParts nextPart = OPTIONS;

        while(nextPart != STOP){
        switch(nextPart){
        case OPTIONS:
            nextPart = options();
            break;
        case SUCCESS:
            nextPart = success();
            break;
        case FAILED:
            nextPart = failed();
            break;
        }
    }
}

MainProgramParts options(){
    int choice;

    cout << "1: Register" << endl;
    cout << "2: Login" << endl << endl;
    cin >> choice;

    if(choice == 1)
    {
        string username;
        string password;

        cout << "Username: ";
        cin >> username;
        cout << "Password: ";
        cin >> password;

        ofstream file;
        file.open(username + ".txt");
        file << username << endl << password;
        file.close();

        main();

    if(choice == 2)
    {
    string username;
    string password;
    string un;
    string pw;

    cout << "Username: ";
    cin >> username;
    cout << "Password: ";
    cin >> password;

    ifstream read(username + ".txt");
    getline(read, un);
    getline(read, pw);

    if(un == username && pw == password)
    {
        return SUCCESS;
    }
    else
    {
        return FAILED;
    }
}
}
}

MainProgramParts success(){
    cout << "Successfully logged in!";
}

MainProgramParts failed(){
    cout << "Incorrect password / username!";
}



Thanks,
~Steff
Posted
Updated 18-Aug-15 7:23am
v3
Comments
jeron1 19-Aug-15 11:51am    
As an aside, don't call main() explicitly, like you are in the options() function.

SA is correct.

You may find it better to use visual-studio-community[^]. At least take a look at it.

Your code would then work fine because VSC is C++11 compliant.

Passing a std::string to open() is allowed in C++11. You may simply concatenate the two std::strings making your file name using the + operator.

C++
file.open(username + ".txt");


In other compilers you will have to pass a char* (C string). You may use c_str() to get a C string from a std::string after concatenating the two strings.

C++
file.open((filename + ".txt").c_str()); 
 
Share this answer
 
v2
Comments
CPallini 19-Aug-15 2:31am    
5.
Sergey Alexandrovich Kryukov 19-Aug-15 2:33am    
5ed. I completely missed the mention of C++11.
—SA
No, this is not the concatenation, which is a valid std::string operator: http://www.cplusplus.com/reference/string/string/operator+[^].

The problem is the type, as the expected argument type is old lame char*. Use the c_str operation to get a "C string" out of your std::string instance: http://www.cplusplus.com/reference/string/string/c_str[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900