Calculation using input from text file

Good evening -
I am having some difficulty with a field that is being read from a text input file.   I need to determine if the value is an odd number.   However, when I try to calculate using the field I get sharing violations.   I've even moved it to another integer field, but I get lvalue errors.   Here's the code snippet below.   Am I using a pointer incorrectly here?

while ((fscanf(fpNumbr, "%d", &numbIn)) == 1)
{      
      oddno = fscanf(fpNumbr, "%d", numbIn);
      if (oddno % 2 == 0)
      printf("*");
      else
      printf("%d", oddno);
}
mojeauxBusiness UserAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony2000Commented:
The problem is that fscanf's return value is not the value you need to test against. Your scanned value will be in numbIn and not in oddno as written.

int oddno;
while ((fscanf(fpNumbr, "%d", &oddno)) == 1)
{      
      if (oddno % 2 == 0)
         printf("*");
      else
         printf("%d", oddno);
}
Anthony2000Commented:
I just removed the extra parenthesis.

int oddno;
while (fscanf(fpNumbr, "%d", &oddno) == 1)
{      
      if (oddno % 2 == 0)
         printf("*");
      else
         printf("%d", oddno);
}
Kent OlsenDBACommented:
Hi Mo,

The while() loop continues to grab consecutive integers from the input stream.  In fact, it can grab only one because the character that terminates the number isn't skipped.  When the fscanf() function is called again, it starts with the character that ended the previous conversion, so it reads no values.

You need to skip all of the non-digit items (space, newline, etc.) before calling fscanf again.


Kent
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Anthony2000Commented:
I didn't include all the original code, but here is part that reads and determines whether the number is odd/even.

I ran the modified code and here is what the output looked like:

code:

                  int oddno;
                  while (fscanf(fpNumbr, "%d", &oddno) == 1)
                  {      
                          printf("%d", oddno); // I added to see what came back from fscanf
                          if (oddno % 2 == 0)
                               printf("*\n");
                          else
                               printf("%d\n", oddno);
                  }


output:
.
.
.
Press any key to continue . . .
123456789123456789
12345678*
12345671234567
123456*
1234512345
1234*
123123
12*
11
Anthony2000Commented:
Hi Kent,

I thought fscanf worked as you stated, but it appears to be skipping the newlines?

Anthony
mojeauxBusiness UserAuthor Commented:
Hi Anthony -
Here's the full function... i've commented out some sections because I'm testing.  
void oddNumb(FILE *fpNumbr, int *numbIn)
{

/* LOCAL DECLARATIONS */

int oddno;

/* STATEMENTS */

while ((fscanf(fpNumbr, "%d", &numbIn)) == 1)
{      
      /*fscanf(fpNumbr, "%d", numbIn);
      if (oddno % 2 == 0)
      printf("*");
      else*/
      printf("%d", numbIn);
      printf("*");
}  

return;
}/*disyplays odd numbers from text file*/

Right now the ouptput displays the following:
123456789*12345678*1234567*123456*12345*1234*123*12*1

However, the display should be:
1*3*5*7*9 new line
1*3*5*7* new line
1*3*5*7new line
1*3*5* new line
1*3*5 new line
1*3* new line
1*3 new line
1* new line
1 new line

I'm reading from a text file that looks like
123456789
12345678
1234567
123456
12345
1234
123
12
1

Hi Kent - I read your comments and I'm not sure how to eliminate the new lines from being read by the fscanf statement.   Should I check for " "?  Is this what the program sees when it reads from the text file?

Thanks!!
Anthony2000Commented:
Ok, i see.

Here is what i think you need to understand. Fscanf is is reading the numbers not one character at a time but rather one number such as 123456789, 12345678, etc. Then it converts from ASCII to a integer representation of the numeruc characters. Do youunderstand?
phoffric\Commented:
>> I get sharing violations
>> oddno = fscanf(fpNumbr, "%d", numbIn);
>> Am I using a pointer incorrectly here?

No. You need &numbIn to make numbIn a pointer to an int.
Anthony2000Commented:
I think what you need to do is read a single character at a time, determine if it represent a number between 0-9, and then determine if it is odd or even. Be careful to skip over carriage returns and line feedcharacters.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mojeauxBusiness UserAuthor Commented:
Hi Anthony -  thanks for the clarification.   I understand how the fscanf is working now.  

Would it be easier if I wrote to the text file, placing a space between each number?  Would the fscan then recognize each interger?   Or would I then have to identify the spaces like Kent mentioned earlier?
mojeauxBusiness UserAuthor Commented:
I see your last post and it answered my question.  Thanks!
mojeauxBusiness UserAuthor Commented:
Appreciate the help tonight.   Hopefully I can get this wrapped up soon.   =)
Anthony2000Commented:
That sounds like an easy adjustment to try. I think adding a space will work.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.