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: