-
Messed Up Output
Hey guys for some reason my txt file gets messed up when they type in a unknown name in my lookup function i can't seem to see why this is happing
Code:
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
//Global Functions
//My own little system("pause") function :)
void wait();
///////////////////
void lookup();
void addname();
///////////////////
struct info
{
char name[100];
char address[100];
char telnumber[100];
};
int main()
{
cout<<"----------------Address Book----------------"<<endl;
cout<<"-------------------By: Me-------------------"<<endl;
cout<<"What do you want to do?"<<endl;
cout<<"[1]Look Up Name [2]Add Name [3]Leave"<<endl;
int mainchoice;
int mainloop = 0;
while(mainloop==0)
{
cin>>mainchoice;
switch(mainchoice)
{
case 1:
lookup();
mainloop=1;
break;
case 2:
addname();
mainloop=1;
break;
case 3:
return 0;
break;
default:
cin.clear();
cout<<"Try Agian";
wait();
}
}
return 0;
}
void lookup()
{
cout<<"Enter the name of the person you want to lookup"<<endl;
info lperson;
cin>>lperson.name;
char namelook[100];
fstream lookname("address.txt");//Something in here im not sure what though
while(!lookname.eof())
{
cin.clear();
lookname.getline(namelook,100,'\n');
if(!strcmpi(namelook,lperson.name))
{
lookname.getline(lperson.address,100,'\n');
lookname.getline(lperson.telnumber,100,'\n');
break;
}
}
if(strcmpi(namelook,lperson.name))
{
cout<<"Could Not Find Name Try Agian"<<endl;
lookname.close();
lookup();
exit(0);
}
lookname.close();
cout<<lperson.name<<endl;
cout<<lperson.address<<endl;
cout<<lperson.telnumber<<endl;
wait();
}
void addname()
{
cin.ignore(80,'\n');
cout<<"What is the name of the person you wish to add?"<<endl;
info aperson;
cin.getline(aperson.name,100,'\n');
cout<<"What is"<<aperson.name<<"Address?"<<endl;
cin.getline(aperson.address,100,'\n');
cout<<"And finnaly what is their telephone number(no need to enter a dash)"<<endl;
cin.getline(aperson.telnumber,100,'\n');
ofstream newname("address.txt",ios::app);
newname<<aperson.name<<endl;
newname<<aperson.address<<endl;
newname<<aperson.telnumber<<endl;
newname.close();
cout<<"Succesfully Added "<<aperson.name<<" to the address book"<<endl;
wait();
}
void wait()
{
cout<<"Press any key and enter to continue..."<<endl;
cin.ignore(80,'\n');
char wait[5];
cin>>wait;
}
If the person types a wrong name it adds stuff onto the first line in the file eg. if I enter lady(which doesn't exist in the file) it adds to my name brian which is at the top of the file like this
Code:
BriBrian
11520
5311145
and if I keep entering a invalid entry it just adds another Bri to my name
Thx In Advance Guys
-
I compiled and ran it, and I didn't see that problem you described. I did however find another one:
Code:
newname << endl; //I added this line
newname<<aperson.name<<endl;
newname<<aperson.address<<endl;
newname<<aperson.telnumber<<endl;
You need that extra line so that the new entry is appended on a new line. Other than that, the program works fine for me. Describe exactly what steps you performed that lead to the error.
-
First I Enter My Info
Brian
My Address
My Phone No.
*Program Exits*
Reopen Program
Enter A Fake Name Such As Loser
Says Cannot Find Name
Then I open the file and This is what it looks like
BriBrian
My Address
My Phone No.
-
This behavior does not occur for me. What compiler/OS are you using?
-
Using Dev C++ 4.9.8.9
Windows ME
-
It may be that the implementation of certain functions is different with your compiler. What you should do is set a breakpoint when the program rejects a lookup name, then step through the code to find where the error occurs. There's nothing I can do, short of downloading your compiler and debugging it myself.
Also, make sure you have the latest version of Dev C++, and if you do, reinstall it.
-
Thanks for all your help benny I appricate it :)
-
No problem. Have you found out what was causing the error yet?
-
Nope im not sure how to use my debugger NOOB :) but anyways i commented out my
Code:
if(strcmpi(namelook,lperson.name))
{
cout<<"Could Not Find Name Try Agian"<<endl;
lookname.close();
lookup();
exit(0);
}
and the same thing still happened ill keep checking maybe its the way im handling the file ill find it soon enough im sure :)
-
Yay Benny i found it :) i had to change this
Code:
fstream lookname("address.txt");
To This
Code:
fstream lookname("address.txt",ios::in);
I don't know why the problem was there in the first place but as long as it goes away im happy :P