I am a beginner

I have to write a code in straight "C"

the code has to read from a file from c:\temp
c:\temp\numbers.txt

inside that txt file are the numbers
2 4 6 8 -3 20 30 40 60 80 100 200

It reads the txt in my code perfect

It is supposed to multipy by 6 each number
that is a
positive
and less than 100

and show the output


I get a calculation and output of all numbers found

I cannot get my if statement correct to check the criteria

if (i > 0 || i < 100);
I also tried
if (i > 0, i < 100);

also of criteria is not met:
print an error message to console stating the problem

Bad part is we have to use Miracle C to compile and build

I am sure this is simple to all of you. But I am needing assistance with my
if statement at least,....error message would be great also too

Thank you in advance for at least reading this

#include <stdio.h>;


int IntegerArray[25];
int i;

void main()

{
FILE*f; // informing to use a file

  f=fopen("c:\\temp\\numbers.txt","r"); //the file to open with the data
  
 
  while (fscanf(f,"%d",&i)!=EOF) //scanning the file and data to include numbers
     {
     
  if (i > 0 || i < 100);  //<<<------supposed to ensure # is positive,     less than 100 I ...
                         // ........cant figure out if input fails criteria...
                         //to print error message to console stating the problem with the bad numbers
       printf(" %d\n",i*6); //if i = criteria  times them by 6 and show output to console
     }
    
  fclose(f);

  getch();
}

Recommended Answers

All 21 Replies

I am sorry if I wrapped the code incorrect

> It is supposed to multipy by 6 each number
that is a
positive
and less than 100

Think about that if statement.

We want something where the value of i is positive AND the value of i is less than 100.

&& means AND and || means OR. Therefore, when you tested i > 0 || i < 100 you were saying "either i is positive OR i is less than 100". You want to do i > 0 && i < 100 in order to say "i is positive AND i is less than 100."

&&

Ahhhhhhh !!!
but am I placing my if statement in the correct area?

It might be placed correctly, but the syntax is definitely wrong. You said if (whatever); <----- the semicolon should not be there. An example if statement:

if (i > 0 && i <100){
printf(whatever);
}

You need brackets.

commented: Ah yes, the end of line semicolon "trap", always a perennial favourite :) +35

trying that now .
brb

I tried that ... I am goofing somewhere after I added that

error is
(null) Parse Error, expecting `'}''
''
aborting compile


I took out the comments on my IF

it looked goofy in here

#include <stdio.h>;


int IntegerArray[25];
int i;

void main()

{
FILE*f; // informing to use a file

  f=fopen("c:\\temp\\numbers.txt","r"); //the file to open with the data
  
 
  while (fscanf(f,"%d",&i)!=EOF) //scanning the file and data to include numbers
     {
     
  if (i > 0 && i < 100 ){  //ERROR with "SEP"
                        
       printf(" %d\n",i*6); //if i = criteria  times them by 6 and show output 
     }
    
  fclose(f);

  getch();
}

I think I just fixed it ....going to check my math
here is the code ...with //removing non working items

lets see

#include <stdio.h>;


int IntegerArray[25];
int i;

void main()

{
FILE*f; // informing to use a file

  f=fopen("c:\\temp\\numbers.txt","r"); //the file to open with the data
  
 
  while (fscanf(f,"%d",&i)!=EOF) //scanning the file and data to include numbers
     {
     
  if (i > 0 && i < 100 )//{  //ERROR with "SEP"
                        
       printf(" %d\n",i*6); //if i = criteria  times them by 6 and show output 
     }
    
  fclose(f);

  getch();
}

be back to let you know ....

if this is fixed ...I just have to figure out how to make an error message stating whay what numbers were not used...as an output

BRB

Yes...very cool ....my calculations and if statement works perfect
and the calculations output to console ..is perect math
now

what is best to do this
if a number in the input doe not meet the criteria the program should print an error to the console stating the problem

basiccaly from the numbers in the txt file .....being -3 , 100 , 200
were not included in the calculations.

perhaps you can try something like this.

if (critera)
{
    // do whatever
}
else
{
    // do something different
}

would I add that
IF
after the
printf //calculations output

ohh probably before it..

I am newbie ....sorry...willing to learn

#include <stdio.h>;


int IntegerArray[25];
int i;

void main()

{
FILE*f; // informing to use a file

  f=fopen("c:\\temp\\numbers.txt","r"); //the file to open with the data
  
 
  while (fscanf(f,"%d",&i)!=EOF) //scanning the file and data to include numbers
     {
     
  if (i > 0 && i < 100 )//{  //ERROR with "SEP" took it out....also dont use || this means OR use &&
                        
       printf(" %d\n",i*6); //if i = criteria  times them by 6 and show output 
     }
    
  fclose(f);

  getch();
}

but,.......how would I make the code read and state the numbers from the txt file that were not used?

without hard coding it

> Bad part is we have to use Miracle C to compile and build
http://www.daniweb.com/forums/thread193907.html
Like I said before, you need to slap your tutor about the head with an educational 2x4 until they learn that the compiler they recommend is a piece of CRAP.

Getting even trivial programs, which every other compiler would have no trouble with, is an exercise in frustration. Perhaps the name of the compiler derives from "OMG, it finally compiles, it's a fucking miracle". I used to think it was a case of "OMG, somebody actually paid me money for this PoS, it's a fucking miracle".

Who knows, maybe the "tutor" (I use the term advisedly) has got a kickback deal from the crapware author for recommending it. Either way, you're stuck with the suck.

Just forget the course and learn by yourself online. You'll get a hell of a lot further, and you'll enjoy learning it as well.

At least install something like http://www.codeblocks.org so that you can parallel compile your code with a real compiler. You'll then be able to see that what you tried with mira-crap was actually a real program which could be run successfully.

If you give your tutor a handful of examples of trivially correct programs which compile and run OK with CodeBlocks, but which elicit some kind of barf from miracrap, then they might start to listen.

commented: \/\/34D!!!1 +11
commented: Lmao :D +11

salem is right about Miracle C. its even worse than Borland/Turbo. Seriously, do try getting a real compiler.

CodeBlocks (with MinGW)
Bloodshed Dev-C++
Microsoft's Visual C++ Express

are all free. I reccomend CodeBlocks. its a full featured IDE graphical environment, and with MinGW's version of GCC you can't go wrong.

....

back to your problem at hand:

if (i > 0 && i < 100 )             
{
    printf(" %d\n",i*6); 
}
else
{
    // do your error handling routine here
}

that's all i was trying to say...

>>how would I make the code read and state the numbers from the txt file that were not used?

the same way you made the code read and state the numbers from the test file that *were* used.... with "printf()" and the "%d" format specifier.

.

if (i > 0 && i < 100 )             
{
    printf(" %d\n",i*6); 
}
else
{
    // do your error handling routine here
}

that's all i was trying to say...

how would I make the code read and state the numbers from the txt file that were not used?

the same way you made the code read and state the numbers from the test file that were used.... with "printf()" and the "%d" format specifier.

I agree with the its a miracle it works ..when it finally compiles.

When I tried these
else
it lists all the multiplication numbers and the full contents of the txt file
what I am attempting to do is ...once it shows the multipied calculations...
then have it say which numbers in the file were ommited only on a new printf line

here is my original working code again

#include <stdio.h>;


int IntegerArray[25];
int i;

void main()

{
FILE*f; // informing to use a file

      printf("\nThis code will look for numbers inside the file of of c drive temp numbers.txt\n\n");
      printf("\n");
      //printf(" %d\n",i);
      printf("\nPress ENTER to continue");
      
     getch();
     printf("\n");
     printf("\n");
     printf("\nPlease NOTE that these criteria factors are needed");
     printf("\n");
     printf("\nNumber must be a positive");
     printf("\n");
     printf("\nNumber must be less that 100");
     printf("\n");
     printf("\nIf the criteria is met within the txt file we will Multiply each number by 6");
     printf("\n");
     printf("\nPress ENTER to continue\n");
     printf("\n");
     getch();
     
  f=fopen("c:\\temp\\numbers.txt","r"); //the file to open with the data

 
  while (fscanf(f,"%d",&i)!=EOF) //scanning the file and data to include numbers
     {
  
  if (i > 0 && i < 100 )//{  //ERROR with "SEP" took it out....also dont use || this means OR use &&
               
       printf(" %d\n",i*6); //if i = criteria  times them by 6 and show output
  if (i < 0 && i > 100 ) ////trying
       printf(" %d\n",i);    //////////trying   

     }
    
  fclose(f);
printf("\nPress ENTER to CLOSE\n");
  getch();
}

my original issue was solved

the &&
fixed that

The point Salem and others are making about the compiler seems to be two fold. One, that it is a piece of crap and coding with it is probably extremely frustrating. But secondly, and more importantly, by giving errors, warnings, or not compiling where a better compiler would compile, it is teaching you not to use legitimate coding habits, which can't make anyone a better programmer

the nice thing is .... for this codeing in C course
that is my week 5 of 5 assignment

I went ahead and sent it in without showing what numbers were not used.

I just want to be done....
So far I have an A in the class

So far I have an A in the class

if that's all that's important to you, well, then congratulations.

of course, with an instructor who doesnt know a C compiler from a pile of horseshit, it's not anything to get real excited about.

but hey, whatever works for you.

> with an instructor who doesnt know a C compiler from a pile of horseshit
Perhaps it's grade "hay" rather than grade "A" :icon_cheesygrin:

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.