For some reason im getting stuck in a loop after i enter in my "numberinput"

i have a mimic case that does the same thing but with characters instead of numbers, and it seems to work perfectly

all this is doing is checking to see how many times the user defined number is inside a premade file.

case '2':
			infile.open("numbers.dat");
			
			if (infile.fail())
			{
				cout << "Error opening input file -- program terminated!\n";
				return 0;
			}
			
			//number input
			cout << "\nEnter number to count: ";
			cin >> numberinput;
			cout << endl;
			
			//priming read
			infile >> number;
			
			//looping count
			while (!infile.eof()) 
			{	
				if (number == numberinput)					
				{	
					count++;
					infile >> number;
				}
					else 
					{
						infile >> number; //reset read
					}	
			}
			
			cout << "\nThere is/are " << count << " occurence(s) of that number in the file.\n";
			
			count = 0;
			
			//file close and clear
			infile.clear();
			infile.close();
			break;

It would help if you'd describe the problem. Getting stuck is not an explanation.

Do you mean the while loop in the case '2'??? By the way, you can condense your loop to be more readable...

//*** from ***
//priming read
infile >> number;

//looping count
while (!infile.eof()) {	
  if (number == numberinput) {	
    count++;
    infile >> number;
  }
  else {
    infile >> number; //reset read
  }	
}

//*** to ***
while (!infile.eof()) {
  inflie >> number;
  if (number == numberinput) { count++; }
}

sorry. got an epic headache and totally forgot to say what was happening. lol

i get to the point where i input a number for the case to search and it just stop and nothing happens. which im assuming it to just be looping into nothingness.

but im gonna try condensing it and seeing what happens.

also sorry for the big spacey coding. its how she makes us code.
basically shes blind. and things close together give her headaches.

update:
still getting stuck in the while(infile.eof()) loop after i input "numberinput"

Has it ever read the line? What is the content in your file when read? You may try to cout inside the loop to see what it is reading.

i mean as far as i know its reading the file. thats what my infile.fail() is for.

the plain text file looks like this
"11223344556677889900"

just so i can test it to see if its working.

but like i said i have a mirror of this case where it searches for letters instead of numbers (char's instead of int's) and it works flawlessly. with the same exact code with different variables. thats why this is driving me crazy.

I just want to know what the content each time the infile reads from your file. Could you add cout << number; right below infile >> number; and compile it again? You should see whether it has ever been inside the loop or it is really stuck inside the loop (and could know where).

i added it before it asks for the number input (because after that it wont let anything output or input) and it outputed: "73076".

so obviously there is something wrong with my infile.open or suffin.

Hmm... I believe that the infile reads as string, not an integer. You may have to put the read in data in a string, and then convert it to int. After that, you can use it to compare.

//priming read
infile >> letter;
			
//looping count
while (!infile.eof()) 
{	
	if (letter == letterinput) 				
        {	
	count++;
	infile >> letter;
	}
		else 
		{
		infile >> letter; //reset read
		}	
}

this is the mirror code i was talking about. works flawlessly. doesn't seem to be pulling it in as a string because im comaparing the char letter to the char letterinput.

excuse my horribly similar variables lol

No no, what I mean is that if the input read by infile is a string, you would have problem to do

if (number == numberinput) { count++; }

because number is not an integer. That's the only think I can think of right now. I am going to bed :(( Tired from work. :(

i added it before it asks for the number input (because after that it wont let anything output or input) and it outputed: "73076".

so obviously there is something wrong with my infile.open or suffin.

Adding it before the input is worthless. We need to find out what the value is after the input.

If you think there's something wrong with the open, then test for an error after you try to open. Did it work? Find out.

after some comparison i found out that the cause of it stopping after numberinput was the while (infile.eof()) for some reason or another.

changed it to while(infile) and it fixed itself

Wait... You use while (infile.eof()) instead of while (!infile.eof())???

well no it was !infile.eof() but i replaced that with infile.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.