Thread: How to check for minus??

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

    How to check for minus??

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <math.h>
    #include <ctype.h>
    
    #define MAX 11
    
    
    
    void main()
    {
        float Divide1,Divide2 = 0.0;
        void function(char,float,float);
    
        int Num1;
        int Num2,y,n=0,k=0,kl,x;
        char isdigit(char);
        char Str2[MAX];
        char Str1[MAX];
        char Operator[1],v;
    //do-while loop until Exit is Typed
        do
        {
        printf("\nPlease enter the required calculation\n");
    //Get first string
        scanf("%s", Str1);
    //Exit program if 'Exit' typed
        if (strcmp(Str1,"Exit") == 0)
             break;
    //Get second string
        scanf("%s", Str2);
        if (strcmp(Str2,"Exit") == 0)
             break;
    // if numbers entered are above MAX display error
        if (strlen(Str1) > MAX -1)
            printf("ERROR5");
    
        else if (strlen(Str2) > MAX -1)
            printf("ERROR5");
    
            Divide1= atof(Str1);
        Divide2= atof(Str2);
    
            //Calculate Squareroot if string2 is an 'S'
        if  ((*Str2) == 'S')
            printf("=%f\n",sqrt(Divide1) );
    //Calculate Reciprocal if string2 is an 'R'
        else if ((*Str2) == 'R')
            printf("=%f\n",1/Divide1 );
    
    // Check if strings are alphabetacal
       if ((*Str2 != 'S') & (*Str2 != 'R'))
        {
            y = strlen(Str1);
            for(x = 0; x < y; x++)
            {
            if( !isdigit(Str1[x]) )
            {
                k=1;
            }
            }
            y = strlen(Str2);
            for(x = 0; x < y; x++)
            {
             if( !isdigit(Str1[x]) )
            {
                k=1;
            }
            }
        if (k == 1)
        {
            printf("ERROR1");
        }
        else
        {
    
            scanf(" %s", &Operator);
            if (strlen(Operator) > 1)
            {
            n = 2;
            }
            if (n != 2)
            {
            v=Operator[0];
            Num1= atoi(Str1);
            Num2= atoi(Str2);
    //Switch statement to calculate Addition,Subtraction,Multiplication and Division
            switch(v)
            {
                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");
    
            }}
            if (n == 2)
            {
                printf("ERROR2");
            }
        }
        }
        n = 0;
        k = 0;
       fflush(stdin);
    
       }
       while(strcmp(Str1, "Exit") != 0);
       getch();
       }
    
    
        //function to display error if divide by '0' else division performed
    void function(char Str2, float Divide1, float Divide2)
        {
            if(Str2 == '0')
                printf("ERROR3");
            else
                printf("=%f",Divide1/Divide2);
    
        }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well uh, maybe like this, which is already in your code
    Code:
    case '-':
    Don't you go and just dump a whole lot of code on us, give almost zero exlpanation of what you're wanting to do and expect any kind of helpful response!

    How To Ask Questions The Smart Way
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > void main()
    > fflush(stdin);
    Read the FAQ, both of these are wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 05:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM