clearing the buffer

This is a discussion on clearing the buffer within the C++ Programming forums, part of the General Programming Boards category; at least I think it is called "clearing the buffer"... so, this is a menu driven program. If the first ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    clearing the buffer

    at least I think it is called "clearing the buffer"...
    so, this is a menu driven program. If the first option is chosen, you can input a number and it will store the number in a string. Then a "for" loop reads each character from that string and displays each character. After this is done the menu comes up again and you can repeat that over and over. When the menu comes up, you can also choose a second option. This option reads from an external file. It reads & displays each line to the end. When it is done, the menu comes up ready to do the next command. Here is the problem: If I select the second option again from the menu, it won't re-read the same file starting from the top. It just re-displays the last thing stored in memory.
    Is it possible to be able to re-read that same file over and over using the menu?
    The external file I used has three lines of numbers (shown below).


    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <cstdio>
    #include <string>
    using namespace std;
    
    #define inFile "file.txt"
    
    int main ()
    {
    
    int menuNumber; 
    int next;
    ifstream inStream;
    string number;
    char ch1;
    
    do
    {
        cout << "Enter 1 to type in a number" << endl;
        cout << "Enter 2 to get number list from an external file" << endl;
    
    	next = 0;
    	cin >> menuNumber;
    
    	switch (menuNumber) 
    	{
            case 1:
    			
                 cout << "Type in number: ";
                 cin >> number;      // option to read in string if typed in
                 break;
    
            case 2:
    			
                 inStream.open(inFile);
                 inStream >> number;   // option to read in first string from batch file
                 break;
     	}
    
    	if(menuNumber == 1)
    	cin.ignore(100, '\n');
    
    while(next != 1)
    {
        for(int i=0;i<(number.length());i++)
    	{
        number.at(i);      //This "for" loop reads every character from string "number"
    	ch1 = number.at(i);
    	cout << ch1;
    	} 
    
    	cout << endl;
    
    	if (menuNumber == 2)  
        inStream >> number;  //if reading from batch file, it reads the next line
    	else if (menuNumber == 1)
    	next = 1;    
    
    	if((menuNumber==2)&&(inStream.eof()))
    	{
    		cout << endl;
    		next = 1;
    	}
    }
    
    cout << endl;
    
    if (menuNumber == 2)
    inStream.close();
    
    }while(1!=2);
    
    }

    here is what my external file / batch file from notepad contains:
    Code:
    1142
    6851
    6981

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    if you know your batch file always contains 4 digit numbers why dont you just use an int array to store them, int number[4].

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    its just the sample here that contains 4 characters on each line. The lengths are 4 & 6 digits long

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You probably need to call inStream.clear() to clear the eofbit and/or failbit before reading from the same ifstream variable again. I'd do that just after closing it.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Quote Originally Posted by Brian_Jones
    Code:
     
    while(1!=2);
    will make sure that your external do-while loop keeps iterating.

    If you want to re-read your file, then I did it this way using seekg() and clear()
    Code:
    #include<iostream>
    #include<fstream>
    const std::string FILE_NAME = "in1.txt";
    int main() {
        std::ifstream infile(FILE_NAME.c_str());
        if (infile.is_open()) {
            std::cout << "File opened : " << FILE_NAME;
            std::string yes_no = "Y";
            do {
    
    
               std::string temp;
               while (infile >> temp) {
                   std::cout << "Read" << temp << std::endl;
               }
               infile.clear(); //Clear flags
               infile.seekg(0, std::ios::beg); //Reset to beginning
    
               std::cout << "Read again :(Y/N)";
               std::cin >> yes_no;
            }
            while (yes_no == "Y");
            infile.close();
        }
        else {
            std::cout << "File not found ";
        }
    
        return 0;
    }
    You can convert the value(temp in my case) to an int using stringstream

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    it's working good now, Thank you both!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. text input buffer clearing
    By red_Marvin in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 02:17 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 04:15 AM

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