Infinite loop help help!

It always goes into infinite loop at funtion option one, not main. i don't know why. i have tried for weeks. and also at the function option one, 'ins >> nextnum' will input a bunch of numbers into the int nextnum. how to input just one digit at a time into int. thank you soooo much for helping me out. it is due monday.

#include
#include
#include
#include
#include
using namespace std;

void option_one(ifstream&);

int main ()
{

ifstream ins;
int selection;
string filename;


cout << "* This is a Program that reads a file that has ISBNs,"<<endl;
cout << "* It also check the validity of the ISBNs. "<<endl;
cout << "* MAIN MENU: "<<endl;
cout << "* 1. Open the isbn.txt file and check its validity"<<endl;
cout << "* 2. Exit Program"<<endl;

while (selection !=2)
{
cout << "Please enter your selection:";
cin >> selection;
switch(selection)
{
case 1:
ins.open("isbn.txt");
if (ins.fail ())
{
cerr <<"*** ERROE: Cannot open " << ins
<< "for input."<<endl;
return EXIT_FAILURE;
}
option_one(ins);
break;

default:
break;
}
}
system("pause");
return 0;
}



void option_one(ifstream& ins)
{
char checker;
string next;
int accum;
const int divider = 11;
const char NWLN = '
';
int nextnum;
while (!ins.eof())
{
accum = 0;
while (checker != NWLN)
{
ins >> nextnum;
cout << nextnum;
accum = nextnum;
checker = nextnum;
}

}
}


file: isbn.txt
0-201-88337-6
0-13-117334-0
0821211315
1-57231-866-X

Comments

  • I think the simplest solution is to read the whole line then parse out the individual integers. After that, you can do whatever you want with the integers.

    [code]
    #include
    #include


    void option_one(ifstream& ins)
    {
    std::string line;
    int part1,part2,part3,part4;
    while (!ins.eof())
    {
    ins >> line;
    cout << line.c_str() << endl;
    sscanf(line.c_str(),"%d-%d-%d-%d",&part1,&part2,&part3,&part4);
    cout << part1 << " " << part2 << " " << part3 << " " << part4<< endl;

    }
    }
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion