Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
Being that this is 3 years old I really don't remember it too well. I'm confused by how many uses of char[] there are, etc. Anyway, line_in is declared in the code I posted, so you missed something in a copy/paste or changed it afterwards. It should be type string and the declaration should be at the top of the function in question.

-Lee
 
help guys..

hi guys im new in this forum and im searching for a code to learn about using "ifstream and ofstream" thats when i came upon this forum..

im a newbie when it comes to coding so thats why i need some help..

we are to create a login program in c++ using those above and i dont know where to start..

this is what our prof output:
........................................................................................................................
Username:
Password:

1.choose a different username:
2.choose a different password:
3.Exit
........................................................................................................................
might be simple to others..

the thing is he said that if the username or password is incorrect, meaning it woud not login.. and should show that the username or password is wrong..

he uses "if, while, else"

he also used "switch"
i think he opened a different source file for each in "username, password and switch"

i dont know how did it.. and the username and password shows up in 2 different notepad and automatically updates itself when changing username and password..

if someone can help me with this it will be deeply appreciated.. tnx guys..
 
Start over, use nothing but C++ strings and you get overloaded == signs and so on to compare the strings. No size, terminating zeros and so on to keep in mind. I assume this is not going to be used for a real login anyway right?


Edit: I'll add a bit to this. Have you done anything at all yet? Do you know how to print a string or get a string from stdin to a variable? If not, spend some time reading in your course book before attempting to solve it. It's not much to the exercise except, io to the terminal, and comparison of variables.
 
Last edited:
Plz add username and password to my project (plz i need help)

Code:
#include<iostream>
#include<stdlib.h>
#include<ios>
#include<fstream>
#include<iomanip>
#include<string>
#include<conio.h>
#include<windows.h>
using namespace std;

int add_record(int i);
int display_record(int i);
int edit_record(int i);
int detete_record(int i);
int search_record(int i);


struct
{
	
	
	int  id;
	char sName[50];
	char sParentName[50];
	char sAddress[50];
	char sIC[50];
	char sPhoneNo[50];
	char sMobileNo[50];
	char sSchool[50];
	char sQualification[50];
	char sMajor[50];
	
	
	
}mv[100];
int main()
{
	int iname=0;
	fstream f;
	int i=0;
	ifstream ifile;
	ifile.open("Save Data.txt");
	while(ifile.peek()!=EOF)
	{
		ifile>>mv[i].id;
		ifile>>mv[i].sName;
		ifile>>mv[i].sParentName;
		ifile>>mv[i].sAddress;
		ifile>>mv[i].sIC;
		ifile>>mv[i].sPhoneNo;
		ifile>>mv[i].sMobileNo;
		ifile>>mv[i].sSchool;
		ifile>>mv[i].sQualification;
		ifile>>mv[i].sMajor;
		i++;
	}
	fflush(stdin);
	ifile.close();

	while(true)
	{
		system("cls");
		int ch;
		cout<<endl;
		cout<<"\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
		cout<<"\t\t|---- Student Registration System ----|"<<endl;
		cout<<"\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
		cout<<"\t\t"<<endl;
		cout<<"\t\t"<<endl;
		cout<<"\t\t|There Is "<<i<<" Entry(ies) In The Database|"<<endl;
		cout<<"\t\t---------------------------------------"<<endl;
		cout<<endl;
		cout<<"\t\t   .:::::::::::::::::::::::::::::::::.\n";
		cout<<"\t\t   .::| 1.Add Record(s)           |::."<<endl;
		cout<<"\t\t   .::|                           |::."<<endl;
		cout<<"\t\t   .::| 2.Show All The Record(s)  |::."<<endl;
		cout<<"\t\t   .::|                           |::."<<endl;
		cout<<"\t\t   .::| 3.Find Record(s)          |::."<<endl;
		cout<<"\t\t   .::|                           |::."<<endl;
		cout<<"\t\t   .::| 4.Modified Record(s)      |::."<<endl;
		cout<<"\t\t   .::|                           |::."<<endl;
		cout<<"\t\t   .::| 5.Remove Record(s)        |::."<<endl;
		cout<<"\t\t   .::|                           |::."<<endl;
		cout<<"\t\t   .::| 6.Quit                    |::."<<endl;
		cout<<"\t\t   .:::::::::::::::::::::::::::::::::.\n";
		cout<<endl;
		cout<<"\tPlease Enter Your Choice by Choosing a Number {1-6} -> ";

		cin>>ch;
		switch(ch)
		{
			
		case 1:
			i=add_record(i);
			system("cls");
			
			break;
		case 2:
			display_record(i);
			system("cls");
			break;
		case 3:
			search_record(i);
			system("cls");
			break;
		case 4:
			edit_record(i);
			system("cls");
			break;
		case 5:
			i=detete_record(i);
			system("pause");
			system("cls");
			break;
		case 6:
			cout<<"\n\nThank You For Using This Program...."<<endl;
			exit(i);
			break;
		default:
			cout<<"\n\nInvalid Choice!!!"<<endl;
			system("pause");
			system("cls");
			break;
		}
	}
	return 0;
}
int add_record (int i)
{
	int iname=0;
	char reply;
	int x;
	ofstream ofile;
	while(1)
	{
		system("cls");
		mv[i].id=i+1;
		cout<<"\t.::|You Are Entering the Details for Student ID = [ "<<i+1<<" ]|::."<<endl;
		cout<<""<<endl;
		cout<<""<<endl;
		cout<<""<<endl;
		cout<<"Enter Student Name           : ";
		cin>>mv[i].sName;
		cout<<""<<endl;
		cout<<"Enter Student Parent Name    : ";
		cin>>mv[i].sParentName;
		cout<<""<<endl;
		cout<<"Enter Student Address        : ";
		cin>>mv[i].sAddress;
		cout<<""<<endl;
		cout<<"Enter Student IC             : ";
		cin>>mv[i].sIC;
		cout<<""<<endl;
		cout<<"Enter Student Phone No.      : ";
		cin>>mv[i].sPhoneNo;
		cout<<""<<endl;
		cout<<"Enter Student Mobile No.     : ";
		cin>>mv[i].sMobileNo;
		cout<<""<<endl;
		cout<<"Enter Student School         : ";
		cin>>mv[i].sSchool;
		cout<<""<<endl;
		cout<<"Enter Student Qualification  : ";
		cin>>mv[i].sQualification;
		cout<<""<<endl;
		cout<<"Enter Student Major          : ";
		cin>>mv[i].sMajor;
		cout<<""<<endl;
		i++;
		cout<<""<<endl;
		cout<<"DATA ADDED SUCCESSFULLY!\n";
		cout<<endl;
		

		ofile.open("SaveData.txt",ios::trunc);
		for(x=0;x<i;x++)
		{
			ofile<<mv[x].id<<endl;
			ofile<<mv[x].sName<<endl;
			ofile<<mv[x].sAddress<<endl;
			ofile<<mv[x].sIC<<endl;
			ofile<<mv[x].sPhoneNo<<endl;
			ofile<<mv[x].sMobileNo<<endl;
			ofile<<mv[x].sSchool<<endl;
			ofile<<mv[x].sQualification<<endl;
			ofile<<mv[x].sMajor<<endl;
		}
		fflush(stdin);
		ofile.close();
		cout<<"\nDo you want to Continue ?"<<endl;
		cout<<"Y = Yes : "<<endl;
		cout<<"N = No "<<endl;
		cout<<"Please Enter your choice here -> ";
		cin>>reply;
		if(reply=='n')
		{
			return i;
			break;
			
		}
	}
	return i;
}
int display_record(int i)
{
	int iname=0;
	int y;
	system ("cls");
	cout<<"\t\t------Display Report------\n\n"<<endl;
	for(y=0;y<i;y++)
	{
		cout<<"\t\t[Student's ID           ]\t\t ="<<mv[y].id<<endl;
		cout<<"\t\t[Student's Name         ]\t\t ="<<mv[y].sName<<endl;
		cout<<"\t\t[Student's Parent Name  ]\t\t ="<<mv[y].sParentName<<endl;
		cout<<"\t\t[Student's Address      ]\t\t ="<<mv[y].sAddress<<endl;
		cout<<"\t\t[Student's IC Number    ]\t\t ="<<mv[y].sIC<<endl;
		cout<<"\t\t[Student's Phone Number ]\t\t ="<<mv[y].sPhoneNo<<endl;
		cout<<"\t\t[Student's Mobile Number]\t\t ="<<mv[y].sMobileNo<<endl;
		cout<<"\t\t[Student's School       ]\t\t ="<<mv[y].sSchool<<endl;
		cout<<"\t\t[Student's Qualification]\t\t ="<<mv[y].sQualification<<endl;
		cout<<"\t\t[Student's Major        ]\t\t ="<<mv[y].sMajor<<endl;
		cout<<endl;
	}
	cout<<"\t\t------End Of Report------"<<endl;
	system("pause");
	return 0;
}
int edit_record(int i)
{
	int x;
	int iname=0;
	ofstream ofile;
	if (i<0)
	{
		cout<<"\nNothing To Edit!\n";
		system("pause");
		return i;
	}
	int id,z;
	system("cls");
	cout<<"\nEnter The ID Number You Want To Edit:";
	cin>>id;
	if(id>i||id<0)
	{
		cout<<"\nNot Valid ID!\n";
		system("pause");
		return i;
	}
	for (z=0;z<=i;z++)
	{
		if(id==mv[z].id)
		{
			cout<<"\nEditing Student Details For ID : "<<mv[z].id<<endl<<endl;
			cout<<"OLD Name :"<<mv[z].sName;
			cout<<"-->NEW Name: ";
			cin>>mv[z].sName;
			cout<<"OLD Parent Name : "<<mv[z].sParentName;
			cout<<"-->NEW PARENT NAME: ";
			cin>>mv[z].sParentName;
			cout<<"OLD Address : "<<mv[z].sAddress;
			cout<<"-->NEW ADDRESS : ";
			cin>>mv[z].sAddress;
			cout<<"OLD IC No : "<<mv[z].sIC;
			cout<<"-->NEW IC NO : ";
			cin>>mv[z].sIC;
			cout<<"OLD Phone No : "<<mv[z].sPhoneNo;
			cout<<"-->NEW PHONE NO : ";
			cin>>mv[z].sPhoneNo;
			cout<<"OLD Mobile No : "<<mv[z].sMobileNo;
			cout<<"-->NEW MOBILE NO : ";
			cin>>mv[z].sMobileNo;
			cout<<"OLD School : "<<mv[z].sSchool;
			cout<<"-->NEW SCHOOL : ";
			cin>>mv[z].sSchool;
			cout<<"OLD Qualification : "<<mv[z].sQualification;
			cout<<"-->NEW QUALIFICATION : ";
			cin>>mv[z].sQualification;
			cout<<"OLD Major : "<<mv[z].sMajor;
			cout<<"-->NEW MAJOR : ";
			cin>>mv[z].sMajor;
			cout<<endl;

			cout<<"DATA EDITED SUCCESSFULLY!!";
			system("pause");
		}
	}
	ofile.open("SaveData.txt",ios::trunc);
	if(ofile.fail())
	{
		cout<<"error writting to Directory\n\n";
	}
	for(x=0;x<i;x++)
	{
		ofile<<mv[x].id<<endl;
		ofile<<mv[x].sName<<endl;
		ofile<<mv[x].sAddress<<endl;
		ofile<<mv[x].sIC<<endl;
		ofile<<mv[x].sPhoneNo<<endl;
		ofile<<mv[x].sMobileNo<<endl;
		ofile<<mv[x].sSchool<<endl;
		ofile<<mv[x].sQualification<<endl;
		ofile<<mv[x].sMajor<<endl;
	}
	fflush(stdin);
	ofile.close();
	return 0;
}
int search_record(int i)
{
	int iname=0;
	system("cls");
	if(i<0)
	{
		cout<<"\nNothing To Search!\n";
		system("pause");
		return i;
	}
	int id,z;
	cout<<"Enter The ID Number You Want To Search : ";
	cin>>id;
	if(id>i||id<0)
	{
		cout<<"\nNot Valid ID!\n";
		system("pause");
		return i;
	}
	for (z=0;z<=i;z++)
	{
		if (id==mv[z].id)
		{
			cout<<"\t\t[Student ID           ]\t\t="<<mv[z].id<<endl;
			cout<<"\t\t[Student Name         ]\t\t="<<mv[z].sName<<endl;
			cout<<"\t\t[Student Parent Name  ]\t\t="<<mv[z].sParentName<<endl;
			cout<<"\t\t[Stduent Address      ]\t\t="<<mv[z].sAddress<<endl;
			cout<<"\t\t[Student IC No        ]\t\t="<<mv[z].sIC<<endl;
			cout<<"\t\t[Student Phone No     ]\t\t="<<mv[z].sPhoneNo<<endl;
			cout<<"\t\t[Student Mobile No    ]\t\t="<<mv[z].sMobileNo<<endl;
			cout<<"\t\t[Student School       ]\t\t="<<mv[z].sSchool<<endl;
			cout<<"\t\t[Student Qualification]\t\t="<<mv[z].sQualification<<endl;
			cout<<"\t\t[Student Major        ]\t\t="<<mv[z].sMajor<<endl;
			cout<<endl<<endl;
			cout<<"DATA SEARCH SUCCESSFULLY!";
			system("pause");
		}
	}
	return 0;
}
int detete_record(int i)
{
	int iname=0;
	ofstream ofile;
	int id,z,a=0;
	system("cls");
	if(i<=0)
	{
		cout<<"\nNothing To Delete!\n";
		system("pause");
		return 1;
	}
	cout<<"Enter The ID Number You Want To Delete : ";
	cin>>id;
	if (id>i||id<0)
	{
		cout<<"\nNot Valid ID!\n";
		system("pause");
		return i;
	}
	for(z=0;z<=i;z++)
	{
		if (id==mv[z].id)
		{
			cout<<"\t\t[Student ID           ]\t\t="<<mv[z].id<<endl;
			cout<<"\t\t[Student Name         ]\t\t="<<mv[z].sName<<endl;
			cout<<"\t\t[Student Parent Name  ]\t\t="<<mv[z].sParentName<<endl;
			cout<<"\t\t[Stduent Address      ]\t\t="<<mv[z].sAddress<<endl;
			cout<<"\t\t[Student IC No        ]\t\t="<<mv[z].sIC<<endl;
			cout<<"\t\t[Student Phone No     ]\t\t="<<mv[z].sPhoneNo<<endl;
			cout<<"\t\t[Student Mobile No    ]\t\t="<<mv[z].sMobileNo<<endl;
			cout<<"\t\t[Student School       ]\t\t="<<mv[z].sSchool<<endl;
			cout<<"\t\t[Student Qualification]\t\t="<<mv[z].sQualification<<endl;
			cout<<"\t\t[Student Major        ]\t\t="<<mv[z].sMajor<<endl;
			cout<<endl<<endl;
		
			
					strcpy(mv[z].sName,mv[z+1].sName);
					strcpy(mv[z].sParentName,mv[z+1].sParentName);
					strcpy(mv[z].sAddress,mv[z+1].sAddress);
					strcpy(mv[z].sIC,mv[z+1].sIC);
					strcpy(mv[z].sPhoneNo,mv[z+1].sPhoneNo);
					strcpy(mv[z].sMobileNo,mv[z+1].sMobileNo);
					strcpy(mv[z].sSchool,mv[z+1].sSchool);
					strcpy(mv[z].sQualification,mv[z+1].sQualification);
					strcpy(mv[z].sMajor,mv[z+1].sMajor);

		}	
		if (mv[z].id>id)
		{
				strcpy(mv[z].sName,mv[z+1].sName);
					strcpy(mv[z].sParentName,mv[z+1].sParentName);
					strcpy(mv[z].sAddress,mv[z+1].sAddress);
					strcpy(mv[z].sIC,mv[z+1].sIC);
					strcpy(mv[z].sPhoneNo,mv[z+1].sPhoneNo);
					strcpy(mv[z].sMobileNo,mv[z+1].sMobileNo);
					strcpy(mv[z].sSchool,mv[z+1].sSchool);
					strcpy(mv[z].sQualification,mv[z+1].sQualification);
					strcpy(mv[z].sMajor,mv[z+1].sMajor);
					cout<<"DATA DELETE SUCCESSFULLY!";
					
		}
	}
	ofile.open("SaveData.txt",ios::trunc);
	if(ofile.fail())
	{
		{
			cout<<"error writting to directory\n\n";
		}
	}
	for (z=0;z<i;z++)
	{
						
						ofile<<mv[z].id<<endl;
						ofile<<mv[z].sName<<endl;
						ofile<<mv[z].sAddress<<endl;
						ofile<<mv[z].sIC<<endl;
						ofile<<mv[z].sPhoneNo<<endl;
						ofile<<mv[z].sMobileNo<<endl;
						ofile<<mv[z].sSchool<<endl;
						ofile<<mv[z].sQualification<<endl;
						ofile<<mv[z].sMajor<<endl;
	}
	fflush(stdin);
	ofile.close();
	return--i;
}
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.