Hello adeel zamann,
Here is your code with code tags and some blank lines:
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include <iostream>
#include <fstream>
#include <limits> // <--- Added.
#include <string> // <--- Added.
#include <windows.h>
#undef min
#undef max
#include <cstdlib> // <--- Changed.
using namespace std;
int main()
{
constexpr int SLEEP_TIME{ 2000 }; // <--- Added.
string name, password, inName, inPassword, registerName, registerPassword;
Menue:
system("color 1f");
system("cls");
string select; // <--- Should be an "int" or a "char", but not a "string".
//cout << "Enter 1 for Create account\n";
//cout << "\nEnter 2 for Login account\n";
//cout << "\nEnter 3 for Exite\n\n";
cout << "\n"
" 1. for Create account\n"
" 2. for Login account\n"
" 3. for Exite\n"
" Enter choice: ";
cin >> select;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
if (select == "1")
{
system("color 2f");
system("cls");
cout << "\n\t\t\t\t\t\tCreate Account";
std::ofstream create_account("data.txt", std::ios::app);
//create_account.open("data.txt", std::ios::app);
cout << "\n\n\n" << "New Username: ";
std::getline(cin, registerName);
cout << "\nNew Password: ";
std::getline(cin, registerPassword);
create_account << "Name is " << registerName << '\n';
create_account << "password is\t" << registerPassword << '\n' << '\n';
create_account.close();
system("color 3f");
system("cls");
//cout << "\n\t\t\t\t\t\tAccount Created";
std::cout << "\n\n" << std::string(48, ' ') << "Account Created\n";
Sleep(SLEEP_TIME); // <--- Changed.
goto Menue;
}
else
{
if (select == "2")
{
Login:
system("color 4f");
system("cls");
cout << "\t\t\t\t\t\tLogin";
std::ifstream Login;
Login.open("data.txt", ios::out | std::ios::app);
Login.ignore(16, '\n');
getline(Login, name, '\n');
Login.ignore(11, '\t');
getline(Login, password, '\n');
Login.close();
cout << "\n\n\n" << "Enter Username: ";
cin >> inName;
cout << "Enter Password: ";
cin >> inPassword;
if (inName == name && inPassword == password)
{
system("color 5f");
system("cls");
cout << "\t\t\t\t\t\tLogin Successful\n" << "\n\n" << "Welcome, " << inName << "\n";
system("pause");
goto Menue;
}
else
{
system("color 6f");
system("cls");
cout << "\t\t\t\t\t\tincorrect name or password\n";
Sleep(5 * 400);
goto Login;
}
}
else
{
if (select == "3")
{
system("color 8f");
system("cls");
cout << "\n\n\n\n\n\n\n\t\t\t\t\t\tGood Bye";
Sleep(5 * 400);
return 0;
}
else
{
system("color 9f");
system("cls");
cout << "\t\t\t\t\t\tInvalid Selection";
Sleep(5 * 400);
goto Menue;
}
}
}
}
|
I did this so the line numbers I refer to will match.
The blank lines make the code easier to read and follow. This is just one suggestion. How you choose to implement this is still your choice.
Starting with the "include" files you can see the changes and additions I made.
Your program defines "std::string"s and uses "std::getline" both of which need the header file "string" to work. Also some of the "cout" statements will also need the "string" header file to properly output the "std::string"s
"stdlib.h" is a C header file and it would be better to use the C++ version of "cstdlib".
Go to the top left of the page and under the "C++" box either click on the "Reference" link or open it in another tab. There it starts with all the C header files that have a C++ counterpart that you should use.
When you have a program that used the "windows.h" file and the "limits" file yo need to undefine the "min" and "max" macros that "windows.h" defines because "limits" will define "min" and "max" in a different way. I concede that this may not be the best way to do this, but for now it is what I know.
Line 14 works with line 64 and the others, which I have not changed yet, to better implement the delay time rather than using the magic numbers as you have. I am a bit perplexed as to why you used "5 * 400" instead of "2000"?
Lines 28 - 33 are just a different way to do what you did, but it does make it easier to work with and give you an idea of what the output will look like.
When you mix formatted input, as in line 33, with unformatted input of "getline". You need line 35 to clear the input after a formatted input and before an unformatted input with "getline".
In other words formatted input will leave the new line in the input buffer and the unformatted input of "getline" will extract the new line from the input buffer and move on not allowing any input from the key board or a file. To use with a file stream just replace the "cin" with the file stream name.
Line 37 goes back to the comment on line 23. if "select" contains 1 character, (1, 2 or 3), everything is fine, but "select" as a string could contain more than 1 character and the comparison would never be equal. Also you do not need a string for this as you should only be entering 1 character.
Whether you write:
1 2
|
system("cls");
system("color 5f");
|
or
1 2
|
system("color 2f");
system("cls");
|
It seems more logical to change the colour before you clear the screen. Unless there is a reason for what you did.
The "system" calls should be avoided if possible. 1 not everyone can use them. And 2 it leaves your computer open to attack.
Your code:
1 2
|
std::ofstream create_account;
create_account.open("data.txt",ios::out|std::ios::app);
|
Line 1 creates an output stream when you use "ofstream", so in line 2 the "ios::out" is ignored as it is already an output stream. The "ios::app" is fine.
Unless you are new to file streams, most people write just 1 line:
std::ofstream create_account("data.txt", std::ios::app);
This not only creates the files stream it open the file at the same time.
Lines 48 and 51 are a better choice the the formatted input you started with.
In lines 53 and 54 I would loose the
"Name is " <<
and
"password is\t" <<
part and just output the
registerName
and
registerPassword
. Also you do not nee the extra "\n" on line 54. This will make reading the file easier when the time comes. Your output file should look like:
name
password
name
password
name
password
name
password
|
When it comes time to reading the file it will be much easier.
If you need the format of:
Name is asdfads
password is asdfa
Name is asdaadf adgag
password is adfasd
|
Then write a function and a different file for this.
Something to be careful of. Using a "\t" at the beginning of a "string"is OK, but when used in the middle of a "string the "\t" may not space the output correctly. This depends on how many characters come before the "\t".
Line 62 is another way to write line 61. There is also the possibility that your tab may be set larger than someone else's tab and the output may not look right.
Line 64 shows how to use the constant variable. This way you only have 1 place to change the variable with having to hunt through the entire program to make several changes.
Andy