Hi everyone

I am currently learning C++ and i am writing lots of small bits of code to get praticed in coding in C++.

i am working on a user system atm which takes a signup from the user and puts it in a text file so the user and login after or delete there account.

however i am having some issues. one issue the first way i coded it it was only letting me login once before it could find the text in the file. so i setup a new thing that i was about to start. but i dont know how to read what i want from the file without calling on my predfined strings password and username.

i manged to call out the text in the file but i want to be able to take the text and split it up into parts so i can compare it with what the user has put in.

so i dcided i look in to flat file databases but i couldnt find anything that helped me figure it out in c++.

this is what i have coded so far (this code doesnt work atm it was alted when i was trying to print out the test in the file created. it also has a few bugs that i havent iorned out yet)

#include <iostream>
#include <fstream>

using namespace std;

char username[50];
char password[50];
char password2[50];
char username2[50];

ofstream users("intblonkers.txt", ios::app);
ifstream users_b;

void SignUp() {

	int check = 5;

	cout << "please enter a username: ";
	cin.getline(username, 50, '\n');

	users << username << "~";

	cout << "please enter a password: ";
	cin.getline(password, 50, '\n');

	users << password << "~";

	cout << "thank you for signing up.\n";

	users.close();
}

void login() {
	int a = 5;
	char account[50];


	do {
		cout << "Please enter your Username: ";
		cin.getline(username2, 50, '\n');

		cout << "please enter you Password: ";
		cin.getline(password2, 50, '\n');

		users_b.open("intblonkers.txt", ios::app);
		users_b.seekg(ios::beg);
		users_b >> account;

		cout << account << '\n';

		if (strcmp(password, password2) == 0 && strcmp(username, username2) == 0)
		{
			cout << "thank you for loging in this programe hasnt been finished yet but be shore to keep checking it out.\n";
			a = 0;
		}
		else
		{
			cout << "you have entered a wronge username or password please try again.\n";
		}
	} while (a != 0);
}

void accountremove() {
	int input;
	int a = 0;

	do {
		cout << "If you have created 2 accouts the login will know longer work. Please delete your account and make a new one.\n";
		cout << "1. go back.\n" "2. Delete account.\n";
		cout << "Selection: ";
		cin >> input;
		cin.ignore();

		switch (input) {
		case 1:
			{
				a = 0;
				break;
			}
		case 2:
			{
				a = 0;
				users.open("intblonkers.txt", ios::trunc);
				cout << "you have successfully deletted your account.\n";
				break;
			}
		default:
			{
				cout << "your selection was incorrect please try again.\n";
				break;
			}
		}
	}while (a != 0);
}

int main() {

	int input;
	int a = 5;

	do {
	cout << "1. login.\n" "2. Sign up.\n" "3. Delete Accout.\n" "4. Quit.\n";
	cout << "Selection: ";
	cin >> input;
	cin.ignore();

		switch (input) {
		case 1:
			{
				login();
				break;
			}
		case 2:
			{
				SignUp();
				break;
			}
		case 3:
			{
				accountremove();
				break;
			}
		case 4:
			{
				cout << "good bye.\n";
				a = 0;
			}
		default:
			{
				cout << "you have entered a incorrect value please try again.\n";
				break;
			}
		}
	} while (a != 0);
}

was wondering is anybody could help me to understand how to take a string and break it up.

or how to manage a flat file database.

atm i have set it to take the user input for password and username and put a ~ after it. like cgcgames~test~

was wondering how to take that string and split it up so that i can look at each part sepretatly.

also would like to know how to take more then one input so that you can make more the one account. atm it keeps the data but obvosily i dont know how to manage the data in the file so the program can read whats in the file and compare it to user input.

any help would be great :)

thank you,
Jordan

Fbody commented: Phrasing is difficult to understand, but an excellent first post none-the-less. :) +1

Recommended Answers

All 2 Replies

I'm having trouble understanding the phrasing of your post, but one of your issues seems to be that you're having trouble outputting to a file

Concerning your file output situation, please read this thread. It looks like that poster was having the same problem you are.

[edit]
BTW: Despite the difficult-to-understand phrasing, this is actually a pretty decent first post. Keep it up.

Thanks for the speedy replie.

but im not sure thats quite what i was looking for.

sorry for the difficulty understanding what was trying to say. I was trying to think of a way to word it hehe so it probably didnt come out the best.

what im looking for is a way to edit the string retured from a file. right now my code puts the user input (username and password) into a txt file adding ~ on the end. so if i was to open the file it would look like username~password~.

what i am wanting to do is take that information from the file (as you can see what i have done in the void login() fucntion) and make it so i can take the first string of charaters before the ~ and the second string of charaters after the ~. and compare it to the user input for login to make sure they entered the right accout details.

i manged to get this to work another way but i want to be able to close the program and re open it and still be able to login without having to signup again.

i then went on to say at the end i was also trying to see if i could get a flat file database to work. but im not sure how to do one of those and was wondering if thats the best option and how to implment one of them in the code.

i hope this makes more sence now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.