newbie needs advice!

This is a discussion on newbie needs advice! within the C Programming forums, part of the General Programming Boards category; Hello all! Just started taking an evening course in C (brand new to programming)and am enjoying it immensely. I am ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Smile newbie needs advice!

    Hello all! Just started taking an evening course in C (brand new to programming)and am enjoying it immensely. I am however having a couple of problems with understanding what I know are pretty basic situations. Can someone tell me why my program compiles but then when I try to execute it it gives a windows error and crash's. It is supposed to emulate a simple calculator reading from a disk file and writing to another with errror messages if you try to divide by 0 or use an unknown operator. Any advice is appreciated and the input file is in the directory with the .c and .in files.

    #include <stdio.h>

    int main()
    {
    FILE* infile;
    FILE* outfile;
    int x, y, answer;
    char op;

    infile = fopen("calc4.in", "r");
    outfile = fopen("calc4.out", "w");

    do
    {
    // Read x op y from infile
    fscanf(infile, "%d %c %d", &x, &op, &y);
    answer = (x + y) || (x - y) || (x * y) || (x / y);
    if (op != ('+' || '-' || '*' || '/'))
    fprintf(outfile,"\n Unknown Operator!");
    else
    if (y == 0)
    fprintf(outfile,"\n Division by Zero is not allowed!");
    else
    fprintf(outfile,"\n%d %c %d = %d", x, op, y,answer);





    }while (!((x == 0) && (y == 0)));

    return 0;
    }

  2. #2
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    answer = (x + y) || (x - y) || (x * y) || (x / y);
    if (op != ('+' || '-' || '*' || '/'))

    these lines don't accomplish very much

    the logical || operator doesn't work as you think..

    for the answer line, it will simply test if each expression evaluates to zero. then, if any are equal to 0, answer will be equal to 0 (logical true), else if will become logical false (1?)

    the op line doesn't work either. you need something like:

    Code:
    switch (op) {
     case '+':
          answer = x + y;
          break;
    
      case '-':
           answer = x - y;
           break;
    
       /* for a bad operator */
       default:
            /* no operator selected ! */
    };
    that can replace your answer = line and your if line
    .sect signature

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Smile

    Thanks for the really quick response but while we have covered the break and continue commands in class I am not familiar with switch and case so I had better stay with what is being taught. It's just my third (one night weekly) continuing ed class at a local university.

    It is supposed to be a "do while loop" exercise with nested "if else" statements. I really just want to understand why it does not work.

    Thanks again ............... canada Rules!

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Smile

    I've changed the code but still get windows error messages and then the file crashs. Any more suggestions about why? Thanks all in advance !!

    #include <stdio.h>

    int main()
    {
    FILE* infile;
    FILE* outfile;
    int x, y, answer;
    char op;

    infile = fopen("calc4.in", "r");
    outfile = fopen("calc4.out", "w");

    do
    {

    fscanf(infile, "%d %c %d", &x, &op, &y);


    if (op == '+') {
    answer = x + y;
    } else
    if (op == '-') {
    answer = x - y;
    } else
    if (op == '*') {
    answer = x * y;
    } else
    if (op == '/') {
    answer = x / y;
    }
    fprintf(outfile,"\n%d %c %d = %d", x, op, y,answer);

    else
    if (y == '0')
    fprintf(outfile,"\n Division by Zero is not allowed!");
    else
    fprintf(outfile,"\n Unknown Operator!");


    }while (!((x == 0) && (y == 0)));




    return 0;
    }

  5. #5
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    When using files, is it not best to check the return value of fopen against NULL so that you can be sure that the file opened OK?
    Code:
    if ((infile = fopen("calc4.in", "r")) == NULL);
    {
        fprintf(stderr,"Error in opening input file!!\n");
        exit(1);
    }
    Also, is FILE* infile right, or should it be FILE *infile?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by VegasSte
    When using files, is it not best to check the return value of fopen against NULL so that you can be sure that the file opened OK?
    Also, is FILE* infile right, or should it be FILE *infile?
    1) Yes. You always want to check the return value of your fopen. It's always good to know for sure your functions work as you expect.

    2) The second one is just a matter of preference. Here, C ignores white space. Thus, all of this is the exact same thing:

    FILE *infile;
    FILE * infile;
    FILE* infile;
    FILE*infile;

    All of those are the exact same thing.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167

    Thanks quzah

    Thus, all of this is the exact same thing:

    FILE *infile;
    FILE * infile;
    FILE* infile;
    FILE*infile;
    Thanks quzah!!

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    bluenoser: Read this.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Smile

    Thanks fellas,
    the prog still doesn't work without a windows error and a crash so I guess there is more wrong than I thought. The .in file is with the other files all in the same directory. I guess the good news is that it is forcing me to read and understand more in the process.

    Didn't know about the code tags but will use them in the future.

  10. #10
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    Like Salem says, the files might be the problem!

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Smile

    Greetings!
    I wish that it was the infile because I could solve that, but when I take out the if else statements it reads the file OK. I don't however get the results that I want. I am using the LCCwin32 compiler because that is what the class is using but I also doubt it's related to that. I will just keep trying but I am loosing hair over this.

  12. #12
    Champion
    Guest

    Suggestions

    I have a few suggestions, though they may not solve your problems:

    1 - check for y is not zero before the if else statements. If y is zero and operator is '/', you should not proceed at all as
    answer = x / 0

    will crash your program

    2 - the line

    fprintf(outfile,"\n%d %c %d = %d", x, op, y,answer);

    could be causing problems as the compiler assumes you have finished with the if - else if statement but then you have else if again

    3 - you do not check for if (y == '0') but check for if (y == 0) as y is read in as an int (%d).

    fscanf(infile, "%d %c %d", &x, &op, &y);

    So it will be stored as 0 instead of '0'.

    Hope this helps.

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    23
    Thanks for the advice but I had just realized myself about the mistake with the (y == '0') line. I've rewritten it and while I think I understand the do while statement I must be missing something. Perhaps someone could tell me whats wrong with the code as it is written now. It still brings up an 'Encountered an error must close' dialog as it executes. ......thks

    Code:
    int main()
    {
       FILE* infile;
       FILE* outfile;
       int x, y, answer;
       char op;
    
       infile = fopen("calc4.in", "r");
       outfile = fopen("calc4.out", "w");
    
    
       do
       {
    
          fscanf(infile, "%d %c %d", &x, &op, &y);
    
    
    	  if (op == '+')
    		  answer = x + y;
    	   else
    	  if (op == '-')
    		  answer = x - y;
    	   else
    	  if (op == '*')
    		  answer = x * y;
    	   else
    	  if (op == '/')
    	      answer = x / y;
           else
    		  fprintf(outfile, "\nIllegal Operator.");
          if (y == 0)
    		  fprintf(outfile, "\nDivision by zero not allowed!");
    	   else
    	  fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);
    
    
    
      }
          while (!((x == 0) && (y == 0)));
    
    
    
    
    
    
    
        return 0;
    }
    Last edited by bluenoser; 10-15-2002 at 06:41 PM.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >infile = fopen("calc4.in", "r");
    >outfile = fopen("calc4.out", "w");
    You need to check that both of these worked before you start using the file streams.

    The control you have around your loop is not good. If you want to read all the data in the file, you'd do better by checking the return code from fscanf(), rather than testing x and y.

    You've also forgotten to fclose() both your files.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Champion
    Guest

    Again

    Hi,

    Please ignore if you have already considered this - as I suggested before, it will be better to put the y == 0 checking before the calculations, as if y = 0 and you run x / y, you will have problems.

    Also, you may need to check for EOF when you do the file scanning. I am not sure when you want to escape from the do while loop - do { } (while(!((x==0) && (y==0))) will end when both x and y are 0, but if the condition is not met and you keep looping and scan the end of the file, you may have a problem.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie need some advice
    By wiak in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2006, 05:56 PM
  2. Newbie variable advice
    By 182 in forum C++ Programming
    Replies: 12
    Last Post: 02-15-2006, 06:08 PM
  3. Advice for newbie...
    By CompiledMonkey in forum C Programming
    Replies: 2
    Last Post: 06-12-2003, 07:18 PM
  4. newbie needs some advice
    By werdy666 in forum C++ Programming
    Replies: 0
    Last Post: 09-15-2002, 11:56 PM
  5. Newbie: Seek advice
    By gogo in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2001, 10:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21