Flush problem

This is a discussion on Flush problem within the C++ Programming forums, part of the General Programming Boards category; When i load data into an array, I want it to find errors from the input (invalid character for quantity ...

  1. #1
    Furms
    Guest

    Flush problem

    When i load data into an array, I want it to find errors from the input (invalid character for quantity and price). I know a flush should be used to "flush" the rest of the line and an attempt to continue to the next line.

    The code i wrote is:
    Code:
    ...
    fsinventoryFile>> invArray[index].itemCode;.
    ...
    ...
    while(!fsinventoryFile.eof() )
    {                                             //begin read from file
    fsinventoryFile >> invArray[index].quantity  
                          >> invArray[index].price;
    
    cout<<"Record " <<index << " item code is:\t " <<invArray[index].itemCode <<endl;
    cout<<"Record " <<index << " quantity is:\t " <<invArray[index].quantity<<endl;
    cout<<"Record " <<index << " price is:\t " <<invArray[index].price<<endl;
    
    while(!(invArray[index].price ) ||    (fsinventoryFile >> invArray[index].quantity))
    {
      cin.clear();
      while(cin.get(aCh) && aCh !='\n');  //flush
      cout <<"Invalid input on: " <<invArray[index].itemCode
                    << flush<<endl;
    
    } 
        index++;
      fsinventoryFile>> invArray[index].itemCode;
    
    }//end-while
    Any help of this would be apprecited.....thanks!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So what exactly is your question?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Furms
    Guest
    My questions is that when i run it, the flush doesn't work. It does not find the invalid characters for the quantity or price and does not move on to the next line.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your problem (well, one of them) is more than likely on this line:
    Code:
    while(!(invArray[index].price ) ||    (fsinventoryFile >> invArray[index].quantity))
    Going back a few lines, this:
    Code:
    fsinventoryFile >> invArray[index].quantity  >> invArray[index].price;
    ... will probably not touch the variables if the input data is bad. It will not set quantity to 0 upon reading a character.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Furms
    Guest
    I tried a different approach but it still is not working....for example if the quantity in the file is "10" and someone wrote "1F" then it should show an error msg and flush out that line...so far no luck..
    A book im using has an example like this...but still not a big help

    Code:
    while(!(cin >>invArray[index].price))
    {   cin.clear();
         while(cin.get(aCh) && aCh !='\n');  //looks for error
       cout <<"Invalid input on: " << invArray[index].itemCode;
    }
    while(!(cin >> invArray[index].quantity))
    {
       cin.clear();
      while(cin.get(aCh) && aCh !='\n');
      cout <<"Invalid input on: " << invArray[index].itemCode;
    }
    Any other advice out there or changes i should make to this code??

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    2
    Here is the file with the input just in case you need more information and/or interested in helping an unexperienced c++ programer....it would help me huge.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    2
    Here is the input file....
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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