Find the answer to your Linux question:
Results 1 to 5 of 5
Im trying to make a fairly simple calculator and am running into segmentation faults when I try to exit the program. It occurs precicely at the break and have no ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    8

    C help

    Im trying to make a fairly simple calculator and am running into segmentation faults when I try to exit the program. It occurs precicely at the break and have no idea of how to remedy this any help is greatly appreciated.

    #include<stdio.h>
    #include<string.h>

    int main()
    {
    float operand_1, operand_2, result;
    char operator;
    operand_1 = 0;
    operand_2 = 0;
    result = 0;

    /// ignore this just a sample set of instructions for the user not done yet****************
    printf("\n");
    printf("\n");
    printf("When program asks for an operand is means a number\n");
    printf("When the operator asks for an operator one of the follwoing options are allowed +,-,*,/\n");
    printf("If you want to exit anywhere in the program please enter X or x\n");
    printf("Type MS to store result in memory\n");
    printf("Type M+ to add result to memory\n");
    printf("Type MR to recall 1st or second operand\n");
    printf("Type MC to clear memory\n");
    printf("Type C to clear result\n");
    printf("\n");




    printf("Please enter an operand\n");
    scanf("%f",&operand_1);
    while (1)
    {
    printf("Please enter a operator\n");
    scanf("%s",&operator);

    printf("Please enter an operand\n");
    scanf("%f",&operand_2);

    if (operator == '+')
    {
    result = operand_1 + operand_2;
    printf("The answer is:\n");
    printf("%f+%f=%f\n", operand_1, operand_2, result);
    operand_1 = result;
    }
    else if (operator == '-')
    {
    result = operand_1 - operand_2;
    printf("The answer is:\n");
    printf("%f-%f=%f\n", operand_1, operand_2, result);
    operand_1 = result;
    }
    else if (operator == '/')
    {
    result = operand_1 / operand_2;
    printf("The answer is:\n");
    printf("%f/%f=%f\n", operand_1, operand_2, result);
    operand_1 = result;
    }
    else if (operator == '*')
    {
    result = operand_1 * operand_2;
    printf("The answer is:\n");
    printf("%f*%f=%f\n", operand_1, operand_2, result);
    operand_1 = result;
    }
    else if (operator == 'x' || operator == 'X')
    {
    printf("The program is now being terminated\n");
    break; // segmentation fault occurs here****************************************** look here:]
    }

    }
    }
    Last edited by Parnit; 06-16-2009 at 11:49 PM. Reason: mistake

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    I don't know if this is the problem but you have

    Code:
    scanf("%s",&operator);
    Where operator is defined as char....Gerard4143
    Make mine Arch Linux

  3. #3
    Just Joined!
    Join Date
    May 2009
    Posts
    8
    Do you know of a way to remedy this and also I discovered another problem, if I for example was to check to see if the operator say equalled to +MS it would only see that + sign due to the fact its a char and takes in only one character do you know how I can read everything until the user presses enter.

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Try looking up fgets() with info or man. It should provide you with functionality your looking for and change operator to a character array...Gerard4143
    Make mine Arch Linux

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by gerard4143 View Post
    I don't know if this is the problem but you have

    Code:
    scanf("%s",&operator);
    Where operator is defined as char....Gerard4143
    Exactly. You can use:
    Code:
    scanf("%c", &operator);
    Also, in some situations, the return data might be a 2 byte value, so you should use an int value for the operator. Then, mask the unused bits of the operator, and cast it to a char type. It is the overflow of the 1 byte char data that is causing your segfault. The stack has been munged, so when you break, it doesn't know where to go. And while it looks like it occurs at the break, it is actually at the function exit. It's just that the break is the last instruction it can find to give you an address for the segfault. At least, this has been my experience in the past.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...