Extra Record?!?

This is a discussion on Extra Record?!? within the C++ Programming forums, part of the General Programming Boards category; yet another question from me... LOL okay i have written this code to read records from a text file but ...

  1. #1
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41

    Extra Record?!?

    yet another question from me... LOL
    okay i have written this code to read records from a text file but the problem is that it is displaying an extra blank record at the end.

    //note this is just a function of my program

    Code:
    void readFile(info& vars)
    {
            clrscr();
            char str[20];
            int ctr=0,ctr2=0;
            ifstream read;
            read.open(vars.fn);
            clrscr();
            cout<<"Notepad Database: Read Records"<<endl;
            cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<endl;
            cout<<"You are viewing the file "<<vars.fn<<endl<<endl;
            cout<<setiosflags(ios::left);
            cout<<"----------------------------------------------------"<<endl;
            cout<<" "<<" | "<<setw(10)<<"First"<<" ";
            cout<<setw(10)<<"Last"<<" ";
            cout<<setw(10)<<"Month"<<" ";
            cout<<setw(10)<<"Day"<<" ";
            cout<<setw(10)<<"Year"<<endl;
            cout<<"----------------------------------------------------";
            cout<<endl;
            do
            {
                    ctr2++;
                    if(ctr2==1)
                    {
                            cout<<setiosflags(ios::left);
                            read.getline(vars.fName[ctr],sizeof(vars.fName[ctr]));
                            cout<<ctr+1<<" | "<<setw(10)<<vars.fName[ctr]<<" ";
                    }
                    if(ctr2==2)
                    {
                            read.getline(vars.lName[ctr],sizeof(vars.lName[ctr]));
                            cout<<setw(10)<<vars.lName[ctr]<<" ";
                    }
                    if(ctr2==3)
                    {
                            read.getline(vars.month[ctr],sizeof(vars.month[ctr]));
                            cout<<setw(10)<<vars.month[ctr]<<" ";
                    }
                    if(ctr2==4)
                    {
                            read.getline(vars.day[ctr],sizeof(vars.day[ctr]));
                            cout<<setw(10)<<vars.day[ctr]<<" ";
                    }
                    if(ctr2==5)
                    {
                            read.getline(vars.year[ctr],sizeof(vars.year[ctr]));
                            cout<<setw(10)<<vars.year[ctr];
                            cout<<endl;
                            cout<<"----------------------------------------------------"<<endl;
                            ctr++;
                            ctr2=0;
                    }
            }
            while(!read.eof());
            cout<<endl;
            cout<<"Please press any key to return to the menu...";
            getch();
    }

  2. #2
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    please i really need help ;\

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not just add the blank entry outside the while loop?

  4. #4
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    Originally posted by master5001
    Why not just add the blank entry outside the while loop?
    i do not want the blank record!!

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Ever hear of a switch? http://www.cprogramming.com/tutorial/lesson5.html

    Also, ensure that there's not an extra line at the end of your file... that would cause you to read in an extra blank one..

  6. #6
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    yes i have heard of switch, it is giving me the exact same problem tho... always one extra blank record at the end!!

    Code:
    void readFile(info& vars)
    {
            clrscr();
            char str[20];
            int ctr=0,ctr2=0;
            ifstream read;
            read.open(vars.fn);
            clrscr();
            cout<<"Notepad Database: Read Records"<<endl;
            cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<endl;
            cout<<"You are viewing the file "<<vars.fn<<endl<<endl;
            cout<<setiosflags(ios::left);
            cout<<"----------------------------------------------------"<<endl;
            cout<<" "<<" | "<<setw(10)<<"First"<<" ";
            cout<<setw(10)<<"Last"<<" ";
            cout<<setw(10)<<"Month"<<" ";
            cout<<setw(10)<<"Day"<<" ";
            cout<<setw(10)<<"Year"<<endl;
            cout<<"----------------------------------------------------";
            cout<<endl;
            do
            {
                    //ctr++;
                    ctr2++;
                    switch(ctr2)
                    {
                            case 1:
                            {
                                    read.getline(vars.fName[ctr],sizeof(vars.fName[ctr]));
                                    cout<<ctr+1<<" | "<<setw(10)<<vars.fName[ctr]<<" ";
                                    break;
                            }
                            case 2:
                            {
                                    read.getline(vars.lName[ctr],sizeof(vars.lName[ctr]));
                                    cout<<setw(10)<<vars.lName[ctr]<<" ";
                                    break;
                            }
                            case 3:
                            {
                                    read.getline(vars.month[ctr],sizeof(vars.month[ctr]));
                                    cout<<setw(10)<<vars.month[ctr]<<" ";
                                    break;
                            }
                            case 4:
                            {
                                    read.getline(vars.day[ctr],sizeof(vars.day[ctr]));
                                    cout<<setw(10)<<vars.day[ctr]<<" ";
                                    break;
                            }
                            case 5:
                            {
                                    read.getline(vars.year[ctr],sizeof(vars.year[ctr]));
                                    cout<<setw(10)<<vars.year[ctr]<<" ";
                                    cout<<endl;
                                    cout<<"----------------------------------------------------"<<endl;
                                    ctr++;
                                    ctr2=0;
                                    break;
                            }
                            default:
                            {
                                    cout<<"error";
                                    getch();
                                    _exit(0);
                                    break;
                            }
                    }
            }
            while(!read.eof());
            cout<<endl;
            cout<<"Please press any key to return to the menu...";
            getch();
    }

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Oops my bad. I was reading your question (not your code). I'd also recommend making sure that you don't have an extra line in your input file. Or you could check and make sure you don't read in a blank line. If you read in a blank line in your current file you will output one. Fix that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 10:24 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. updating and deleting a record
    By Burritt in forum C Programming
    Replies: 2
    Last Post: 04-19-2003, 05:58 PM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. behind and confused
    By steviecrawf in forum C Programming
    Replies: 1
    Last Post: 11-09-2001, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21