Thread: delete invalid data from an array

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    44

    delete invalid data from an array

    Hi i have a program that reads data from a file.. if the quantity or date is wrong it would ignore that entry completely for further comparison how would i go about putting only valid types into the array..

    text file :

    Code:
    10212 Nescafe 20 1/12/05
    13114 Pepsi -11 10/27/05

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    #define SIZE 100
    using namespace std;
    
    class store
    {
          public:
                 int custnum;
                 string product;
                 int quantity;
                 char tmp[20];
                 char month[80];
                 char day[80];
                 char year[80];
    };
    
    store STORE1[SIZE];
    
    int num_lines(char name[]) // this function counts the number of lines in file
    {
    char templine[100];
    int line=0;
        fstream in_file(name,ios::in);
        if(!in_file)
        {
          cout << "error opening file";
          exit(1);
        }
        while (!in_file.eof()){
          in_file.getline(templine,100);
          line++;
          
                    }
                    in_file.close();
                    return line;
    
    }
    
    void period()
    {
    
    }
    
    void all()
    {
         cout << "THIS IS A TEST TO SEE IF THE FUNCTION WORKS" << endl;
    }
    
    void menu()
    {
         int option,line,tmp;     
         char name[20],c;
         cout << "Enter name of file to load:";
         cin >> name;
         line = num_lines(name);
    
         fstream in_file(name,ios::in);
         if(!in_file)
         {
             cout << "error opening file";
             exit(1);
         }
         for(int x=0;x<line;x++)
         {
                 in_file >> STORE1[x].custnum;
                 in_file >> STORE1[x].product;
                 in_file >> STORE1[x].quantity;
                 in_file.getline(STORE1[x].month,100,'/');
                 in_file.getline(STORE1[x].day,100,'/');
                 in_file >> STORE1[x].year;
                 in_file.ignore(80,'\n'); 
                 tmp = atoi(STORE1[x].month);
                 if(tmp > 12 || tmp < 1 || strlen(STORE1[x].month) < 3)
                          cout << "Line " << x << " has invalid date" << endl; 
                 if(STORE1[x].quantity < 0) 
                         cout << "Line " << x << " has invalid quantity " << endl; 
          }  
    
    
    
         in_file.close(); 
         cout << "The month is " << STORE1[1].year << endl;
    
         
         do {
       
         cout << "Please select a menu option:" << endl;
         cout << "1)Product sales in a given period" << endl;
         cout << "2)Information about all sales" << endl;
         cout << "3)Quit the program" << endl;
         cout << "Option (1,2,3):";
         cin >> option;
       
         switch(option) {
         case 1: 
              period();
              break;
         case 2:
              all();
              break;
              }
       
         } while(option!=3);
    }
    
    int main()
    {
        menu();
        system("PAUSE");
        return 0;   
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Read the input stream into temp variables, then do the checks against those. If they're valid, then set the values equal to the actual array.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    while (!in_file.eof())
    Bad, bad, bad. What happens if an error occurs while you are reading from the file which prevents you from reading any more data from the file? Will your loop ever end?

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    i modified it to this

    Code:
         for(int x=0;x<line;x++)
         {
                 in_file >> STORE1[x].custnum;
                 in_file >> STORE1[x].product;
                 in_file >> STORE1[x].quantity;
                 in_file.getline(STORE1[x].month,100,'/');
                 in_file.getline(STORE1[x].day,100,'/');
                 in_file >> STORE1[x].year;
                 in_file.ignore(80,'\n'); 
                 tmp = atoi(STORE1[x].month);
                 if(tmp > 12 || tmp < 1 || strlen(STORE1[x].month) < 3)
                          cout << "Line " << x << " has invalid date" << endl; 
                 if(STORE1[x].quantity < 0) 
                         cout << "Line " << x << " has invalid quantity " << endl; 
          }
    but it will store the array where the value of x is.. i want valid data to be stored 0,1,2 not 1,4,5 etc...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  2. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM