Thread: Messed Up Output

  1. #1
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820

    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
    Woop?

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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.
    Woop?

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    This behavior does not occur for me. What compiler/OS are you using?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Using Dev C++ 4.9.8.9
    Windows ME
    Woop?

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Thanks for all your help benny I appricate it
    Woop?

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    No problem. Have you found out what was causing the error yet?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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
    Woop?

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  2. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  3. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  4. Messed up output
    By Extropian in forum C Programming
    Replies: 4
    Last Post: 08-03-2005, 09:40 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM