Click here to Skip to main content
15,902,886 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a mini c project I working right now. I try to login after I register a new account,but even I input the correct information still fail to login, can't find the error. The code is quite a lots, but I tried my best to minimize.

Code:

This is the main interface when the console run, for choosing login either register.If you are first-time login user, you have to register a new account before login.

C++
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>

void customer();
void customerres();
void customerlogin();
void fordelay();

int i, j;
int main_exit;

struct date
{
	int month, day, year;
};

struct
{
	int acc_no, age;
	char password[20];
	char repassword[20];
	char name[60];
	char user[60];
	char address[60];
	char gender[15];
	char acc_type[10];
	double phone;
	float amt;
	struct date dob;
	struct date deposit;
	struct date withdraw;
}add, upd, check;

void fordelay(int j)
{
	int i, k;
	for (i = 0;i < j;i++)
		k = i;
}

void main()
{
	system("cls");
	int choose;

	printf("\n1. Login\n2. Register\n\n\nEnter your choice:");
	scanf("%d", &choose);

	switch (choose)
	{
	case 1:
		customerlogin();
		break;

	case 2:
		customerres();
		break;
	}
}


And this is the register feature, I add two feature which are check the availability of account number, and check the re-type password match or not. For checking the account number availability, I use POINTER to do it. After input the rest of user's information, the program will save it into a txt file.

C++
void customerres()
{
	int checkpassword;
	FILE* ptr;
	ptr = fopen("record.txt", "a+");
account_no:
	system("cls");
	printf("Register New Account");
	printf("\n\n\nEnter today's date(dd/mm/yyyy):");
	scanf("%d/%d/%d", &add.deposit.day, &add.deposit.month, &add.deposit.year);
	printf("\nEnter the Account Number:");
	scanf("%d", &check.acc_no);
	while (fscanf(ptr, "%d %s %s %s %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n", &add.acc_no, add.password, add.repassword, add.user, add.name, &add.dob.month, &add.dob.day, &add.dob.year, &add.age, add.address, add.gender, &add.phone, add.acc_type, &add.amt, &add.deposit.month, &add.deposit.day, &add.deposit.year) != EOF)
	{
		if (check.acc_no == add.acc_no)
		{
			printf("Account no. already in use!");
			fordelay(1000000000);
			goto account_no;

		}
	}
	add.acc_no = check.acc_no;
	printf("\nEnter the Username:");
	scanf("%s", add.user);
	checkpassword = 0;
	while (checkpassword == 0)
	{
	password:
		printf("\nEnter the password:");
		scanf("%s", add.password);
		printf("\nRe-enter the password:");
		scanf("%s", add.repassword);
		if (strcmp(add.password, add.repassword) == 0)
		{
			checkpassword = 1;
			printf("Password match!\n");
		}
		else
		{
			printf("Password don't match!\n");
			fordelay(1000000000);
			goto password;
		}
	}
	printf("\nEnter your name:");
	scanf("%s", add.name);
	printf("\nEnter the Date Of Birth(dd/mm/yyyy):");
	scanf("%d/%d/%d", &add.dob.day, &add.dob.month, &add.dob.year);
	printf("\nEnter the age:");
	scanf("%d", &add.age);
	printf("\nEnter the address:");
	scanf("%s", add.address);
	printf("\nEnter the gender:");
	scanf("%s", add.gender);
	printf("\nEnter the phone number: ");
	scanf("%lf", &add.phone);
	printf("\nEnter the amount to deposit:$");
	scanf("%f", &add.amt);
	printf("\nType of account:\n\t#Saving\n\t#Current\n\tEnter your choice:");
	scanf("%s", add.acc_type);

	fprintf(ptr, "%d %s %s %s %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n", &add.acc_no, add.password, add.repassword, add.user, add.name, add.dob.month, add.dob.day, add.dob.year, add.age, add.address, add.gender, add.phone, add.acc_type, add.amt, add.deposit.month, add.deposit.day, add.deposit.year);


	fclose(ptr);
	printf("\nAccount created successfully!");
add_invalid:
	printf("\n\n\n\t\tEnter 1 to go to the main menu and 0 to exit:");
	scanf("%d", &main_exit);
	system("cls");
	if (main_exit == 1)
	{
		main();
	}
	else if (main_exit == 0)
	{
		exit(0);
	}
	else
	{
		printf("\nInvalid!\a");
		goto add_invalid;
	}
}


In this code, I did a function to check do the user has entered the correct information. If the account number, username, and password are match, it will log in successfully, but I failed here.

C++
void customerlogin()
{
	system("cls");
	FILE* ptr;
	ptr = fopen("record.txt", "r");

	printf("\nAccount number: ");
	scanf("%d", &check.acc_no);
	printf("Username: ");
	scanf("%s", check.name);
	printf("Password: ");
	scanf("%s", check.password);

	int checkcredential, choose, findacct, login;
	choose = 0;
	checkcredential = 0;
	findacct = 0;
	login = 0;
	while (fscanf(ptr, "%d %s %s %s %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n", &add.acc_no, add.password, add.repassword, add.user, add.name, &add.dob.month, &add.dob.day, &add.dob.year, &add.age, add.address, add.gender, &add.phone, add.acc_type, &add.amt, &add.deposit.month, &add.deposit.day, &add.deposit.year) != EOF)
	{
		if (check.acc_no == add.acc_no)
		{
			if (strcmp(check.name, add.name) == 0)
			{
				if (strcmp(check.password, add.password) == 0)
				{
					fclose(ptr);
					login = 1;
					customer(check.acc_no);
				}
				else
				{
					checkcredential = 1;
				}
			}
			else
			{
				checkcredential = 1;
			}
		}
		else
		{
			findacct = 1;
		}
	}
	fclose(ptr);
	if (login == 0)
	{
		if (findacct == 1 || checkcredential == 1)
		{
			printf("\nSorry, wrong credentials!\n\nEnter 1 to try again and 0 to exit:");
			scanf("%d", &main_exit);
			system("cls");
			if (main_exit == 1)
			{
				customerlogin();
			}
			else if (main_exit == 0)
			{
				exit(0);
			}
		}
	}
}

void customer()
{
	printf("/n Login Successful!");
}



Thanks for any correction about my code, I appreciated it.

What I have tried:

I try many ways to edit my code but still failed, seem like I didn't really catch the error correctly...
Posted
Updated 4-Nov-19 0:04am
v6
Comments
OriginalGriff 4-Nov-19 5:15am    
If we wanted to post on SO, we'd post on SO. We don't - because we post here. Saying "!I did the same thing elsewhere" is just rude and doesn't improve your changes of hettign an answer...
Member 14643779 4-Nov-19 5:23am    
I am sorry that I did not notice the inappropriateness of my words. I am very grateful for your lesson.

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
I would suggest you start again. Your code seems to be far too complex for such a simple system, and there are one or two things that are a waste of time.

Start with a system that just takes a userid and a password, so you can minimise the amount of code that needs to be tested.

For a registration ask for the user's details and then search the database (or text file) to see if those details already exist. If so, ask for different details.

For a login request, you just need to ask for the two fields, and check if they exist in the database. If so accept the status, else print a message that the details are incorrect. If it fails three times then terminate the application.

Once you have this simple part working successfully you can start adding other fields to your database: date the account is created - you can get this from the system, no need for the user to type it in. Date of birth, but not age, as that will be wrong tomorrow, and can be calculated at any time. And there is no point in storing the retyped password after you have verified it. Also you need to think about hashing your passwords so they are not stored in clear text.
 
Share this answer
 

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