Hi, I am trying to write a program that tells the user the ammount of digits that are in a number for multiple numbers. I have it to the point where it will tell me the digits of the first number but it wil just write it an infinite number of times and never go to the next number. Can somebody please help me i can not figure this out, here is what i have so far:

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
void main (void)
{
	double in;
	int x;
	ifstream inf("E:\\Homework8input.txt");
	ofstream outf("E:\\Homework8output.txt");
	inf>>in;
	while (in<2000000000 && in>-2000000000)
	{
		x = log10(in)+1;
		outf<<"digits = "<<x<<endl;
	}
}

thanks, dustin

Recommended Answers

All 6 Replies

>void main (void) int main() . No excuses.

>while (in<2000000000 && in>-2000000000)
What requirement brought this about? Anyway, you only read one number and proceed to work only on that number. You need to read another number inside the loop.

>void main (void) int main() . No excuses.

>while (in<2000000000 && in>-2000000000)
What requirement brought this about? Anyway, you only read one number and proceed to work only on that number. You need to read another number inside the loop.

hmm... thats the part im not understanding, how do you tell it to proceed to the next number. I would greatly appreciate any suggestions im really stuck, thank you.

>how do you tell it to proceed to the next number
The same way you told it to process the first number, just do it inside the loop rather than outside:

while ( inf>> in ) {
  x = log10 ( in ) + 1;
  outf<<"digits = "<< x <<'\n';
}

Because the >> operator returns the stream, and the stream has a conversion to something that can be tested for success or failure, you can use the entire operation as your loop condition.

>how do you tell it to proceed to the next number
The same way you told it to process the first number, just do it inside the loop rather than outside:

while ( inf>> in ) {
  x = log10 ( in ) + 1;
  outf<<"digits = "<< x <<'\n';
}

Because the >> operator returns the stream, and the stream has a conversion to something that can be tested for success or failure, you can use the entire operation as your loop condition.

Thank you Narue for the help so far. When i replaced my while loop with yours for some reason it is skipping the first number but proceding to read all the next numbers. Am i somehow telling it to do that or is that correct?? thanks for your time.

>Am i somehow telling it to do that or is that correct??
You're probably telling it to do that. Most likely you neglected to remove the input call outside of the loop, so your broken code would look something like this:

inf>> in;

while ( inf>> in ) {
  x = log10 ( in ) + 1;
  outf<<"digits = "<< x <<'\n';
}

Awesome! again thanks for the help!

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.