Thread: Having a problem using '-'

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Unhappy Having a problem using '-'

    Hi im having a problem whem attempting to calculate using negative
    with small numbers example==-2 1 + im getting the error message instead of the answer. PLZ HELP!!!

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <math.h>
    #include <ctype.h>
    
    
    #define ERROR5 "Input strings too long. Applies to numbers and operators."
    #define ERROR4 "Operation not possible."
    #define ERROR3 "Division by zero."
    #define ERROR2 "Unknown operation."
    #define ERROR1 "Number contains alphabetic characters."
    #define MAX 11
    
    void main()
    {
        float Divide1,Divide2 = 0.0;
        void function(char,float,float);
    
        int Num1, x=0;
        int Num2,y;
        char isdigit(char);
        char Str2[MAX];
        char Str1[MAX];
        char Operator;
    
        do
        {
        printf("\nPlease enter the required calculation\n");
        scanf("%s %s", &Str1, &Str2);
    
        if (strlen(Str1) > MAX-1)
            printf(ERROR5);
        else if (strlen(Str2) > MAX-1)
            printf(ERROR5);
    
        Divide1= atof(Str1);
        Divide2= atof(Str2);
    
        if  ((*Str2) == 'S')
            printf("%f\n",sqrt(Divide1) );
        else if ((*Str2) == 'R')
            printf("=%f\n",1/Divide1 );
    
        else for (y=0; y<MAX; y++)
        {
            if(isdigit (Str1[y] & Str2[y]))
                x = 6;
            else if (x != 6)
                x = 2;
        }
    
        if(x == 2)
            printf(ERROR1);
        else if (x == 6)
        {
            scanf(" %s", &Operator);
            Num1= atoi(Str1);
            Num2= atoi(Str2);
            switch(Operator)
            {
                case '+':
                    printf("= %d", Num1+Num2);
                    break;
                case '-':
                    printf("= %d \n", Num1-Num2);
                    break;
                case '*':
                    printf("=%d ", Num1*Num2);
                    break;
                case '/':
                    function(*Str2,Divide1,Divide2);
                    break;
                default:
                    printf(ERROR2);
    
            }
        }}
    while((*Str1) != "EXIT");
    
    getch();
    }
    
    void function(char Str2, float Divide1, float Divide2)
    {
    
    if(Str2 == '0')
        printf(ERROR3);
    else
       printf("=%f",Divide1/Divide2);
    
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What line is giving you the error and what is the error message?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And while we're waiting, take a look at this.
    http://cpwiki.sf.net/void_main
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Hi,

    I am new to c programming. While looking at ur code I came across a strange thing.

    The line


    void function(char,float,float);

    in main is a function prototype. My question is can function prototype delclare inside a function or it should be outside of all the functions????

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    yes the function can be declared inside main.any function can be declared inside main but should be called after the declaration.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I work it like this - if the function is only called by one other function, then I put it's prototype in that calling function.

    If the function is going to be called by more than one other function, then I put it before main(), making it known to all the functions in the program.

    I'm not as consistent about this as I'd prefer, but that's the plan.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    BEN10 and ADAK thanks for ur reply.....

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I always put them at top (or in a header). Easier to find, easier to locate, easier to change.
    And you don't have to move them in case you change your mind.
    We should also mention that this is a very poor way of making a prototype since it lacks the name of the parameters it accepts. It's pretty much impossible to figure out what it accepts by looking at the prototype because of that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Yes Elysia, I agree with you

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Lightbulb

    Ive fixed that problem but now I have another little problem when i input the first equation using +,-,*,/ the answer is correct but when input of the second equation using Squareroot or Reciprocal it seems to be waiting for the Operator. it shouldnt happen if the second string is R or S??
    If using Reciprocal or Squareroot in the first equation it works fine until i use +.-,*,/ then the following Reciprocal,Squareroot doesnt work

    HELP!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM