Thread: Why isnt my program showing the results and got some weird numbers for CMD

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Miami, Florida, United States
    Posts
    19

    Why isnt my program showing the results and got some weird numbers for CMD

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define LINE_MAXLENGTH 80
    void add(int num1, int num2,FILE * ofp)
    {
        printf("%double + %double = %double\n",num1,num2,num1+num2);
        fprintf(ofp,"%double + %double = %double\n",num1,num2,num1+num2);
    
    
    }
    void multiply(int num1, int num2,FILE *ofp)
    {
        printf("%double * %double = %double\n",num1,num2,num1 * num2);
        fprintf(ofp,"%double * %double = %double\n",num1,num2,num1 * num2);
    
    
    }
    void substract(int num1, int num2, FILE *ofp)
    {
    
    
        printf("%double - %double = %double\n",num1,num2,num1-num2);
        fprintf(ofp,"%double - %double = %double\n",num1,num2,num1-num2);
    
    
    }
    void divide(int num1,int num2, FILE * ofp)
    {
    
    
        fprintf(ofp,"%d / %d = %d\n",num1,num2,num1/num2);
        printf("%d / %d = %d\n",num1,num2,num1/num2);
    }
    void toUpperCase(char ch, FILE * ofp)
    {
        printf("%c ==> %c\n",ch,toupper(ch));
        fprintf(ofp,"%c ==> %c\n",ch,toupper(ch));
    }
    void toLowerCase(char ch,FILE * ofp)
    {
        printf("%c ==> %c\n",ch,tolower(ch));
        fprintf(ofp,"%c ==> %c\n",ch,tolower(ch));
    
    
    }
    void printKthDigit(int val, int k,FILE * ofp)
    {
        int i=1;
        int temp =val;
        while(temp >0)
        {
            if(i == k)
            {
                printf("%d (digit @ %d) ==> %d\n",val,k, temp%10);
                fprintf(ofp,"%d (digit @ %d) ==> %d\n",val,k, temp%10);
    
    
                break;
            };
            i++;
            temp = temp/10;
        }
    
    
    }
    void roundDecimal(double x, int n, FILE * ofp)
    {
        char temp[32]="";
    
    
        double  rounded = round(x);
    
    
        sprintf(temp,"%.*lf",n,rounded);
        printf("%lf (precision=%d) => %s\n",x,n,temp);
        fprintf(ofp,"%lf (precision=%d) => %s\n",x,n,temp);
    
    
    }
    
    
    void separte(double x , FILE * ofp)
    {
        char temp[32]="";
        char sign;
        int intPart;
        int fraction;
        sprintf(temp,"%lf",x);
    
    
        sscanf(temp,"%d.%d",&intPart,&fraction);
    
    
        if(intPart > 0)
            sign = '+';
        else
        {
            sign = '-';
            intPart = -1 * intPart;
        }
        printf("%lf ==>  sign=%c, integer=%d,fraction=%d\n",x, sign,intPart,fraction);
        fprintf(ofp,"%lf ==>  sign=%c, integer=%d,fraction=%d\n",x, sign,intPart,fraction);
    
    
    }
    void partitionInteger(int i, int x, FILE * ofp)
    {
        int j;
        int k;
    
    
        j =floor(x* i/100.0);
    
    
        k = i-j;
    
    
        printf("i=%d,x=%d ==>  j=%d, k=%d \n",i,x,j,k);
        fprintf(ofp,"i=%d,x=%d ==>  j=%d, k=%d \n",i,x,j,k);
    }
    void help( FILE * ofp)
    {
        printf("\n *** Help Menu! See below for Calculator Commands: ***\n");
        printf("\n + i j ---> Add integers i and j \n");
        printf("\n * i j ---> Multiply integers i and j \n");
        printf("\n + -i j ---> Subtract integer j from i \n");
        printf("\n /i j ---> Divide integer i by j \n");
        printf("\n C Ch ---> Change character Ch to uppercase \n");
        printf("\n c Ch ---> Change character Ch to lowercase \n");
        printf("\n P i k ---> Print out the k-th digit of integer i \n");
        printf("\n R x i ---> Round double value x to i decimal places \n");
        printf("\n S x ---> Separate out the sign, integer part and fractional part of double value x \n");
        printf("\n D i x ---> Given integers i and x, print out two integers j and k, where the sum of j and k equals i, and when you take x'percent' of i and truncate it you get j \n");
        printf("\n H ---> view all commands \n");
        printf("\n Q ---> Quit program\n");
    
    
    }
    
    
    
    
    int main(void)
    {
        float num1, num2, result;
        char op, ch;
        double firstDouble;
        int temp;
    
    
        FILE* cmd = fopen("CommandsProj2.dat", "r");
    
    
    
    
        if (cmd == NULL)
        {
            printf("File could not be opened");
            return EXIT_FAILURE;
        }
    
    
    
    
        char line[LINE_MAXLENGTH + 1];
        while (fgets(line, LINE_MAXLENGTH + 1, cmd))
        {
            float result;
            line[strcspn(line, "\n")] = '\0';
            if (strcmp(line, "DA") == 0)
            {
    
    
            }
            else if (strcmp(line, "Q") == 0)
            {
            }
            else if (strcmp(line, "H") == 0)
            {
                printf("\nHelp:operations are + - * / H Q\n");
                continue;
            }
    
    
            else if (sscanf(line, "%c %f %f", &op, &num1, &num2) == 3)
            {
                printf("CMD=%int : ",cmd);
                if(cmd == '+')
                {
                    sscanf(temp,"%f %f",&num1,&num2);
                    add(num1,num2,cmd);
    
    
                }
                else if (cmd == '*')
                {
                    sscanf(temp,"%f %f",&num1,&num2);
                    multiply(num1,num2,cmd);
                }
                else if (cmd == '-')
                {
                    sscanf(temp,"%f %f",&num1,&num2);
                    substract(num1,num2,cmd);
                }
                else if (cmd == '/')
                {
                    sscanf(temp,"%f %f",&num1,&num2);
                    divide(num1,num2,cmd);
                }
                else if (cmd == 'C')
                {
                    sscanf(temp,"%c",&ch);
                    toUpperCase(ch,cmd);
                }
                else if (cmd == 'c')
                {
                    sscanf(temp,"%c",&ch);
                    toLowerCase(ch,cmd);
                }
                else if (cmd == 'P')
                {
                    sscanf(temp,"%i %i",&num1, &num2);
                    printKthDigit(num1,num2,cmd);
                }
                else if (cmd == 'R')
                {
                    sscanf(temp, "%d %d",&firstDouble,&num2);
                    roundDecimal(firstDouble, num2,cmd);
                }
                else if (cmd == 'S')
                {
                    sscanf(temp,"%lf",&firstDouble);
                    separte(firstDouble,cmd);
                }
                else if (cmd == 'D')
                {
                    sscanf(temp,"%i %i",&num1,&num2);
                    partitionInteger(num1,num2,cmd);
                }
                else if (cmd == 'H')
                {
                    help(cmd);
                }
                else if (cmd == 'Q')
                {
                    break;
                }
    
    
            printf("\n%c Operator: %4.1f %c %4.1f = %4.1f \n", op, num1, op, num2, result);
            fflush (stdout);
        }
    }
    
    
        return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Scrap all that

    http://www.charstarp.com/2013_04_01_archive.html

    Don't copy other people's (crappy) code.
    Last edited by anduril462; 12-09-2014 at 04:58 PM.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Location
    Miami, Florida, United States
    Posts
    19
    Alright Scrapped so how to implement upper/lowercase rounding and partitioning

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #define LINE_MAXLENGTH 80
    
    
    int main()
    {
        float num1, num2;
        char op;
    
    
    
    
        FILE* cmd = fopen("CommandsProj2.dat", "r");
    
    
    
    
    
    
    
    
        if (cmd == NULL)
        {
            printf("Error Code (404)");
            return EXIT_FAILURE;
        }
    
    
    
    
        int line[LINE_MAXLENGTH + 1];
        while (fgets(line, LINE_MAXLENGTH + 1, cmd))
        {
            float result;
            line[strcspn(line, "\n")] = '\0';
            if (strcmp(line, "DA") == 0)
            {
    
    
    
    
            }
            else if (strcmp(line, "Q") == 0)
            {
    
    
    
    
    
    
    
    
            }
            else if (strcmp(line, "H") == 0)
            {
                printf("Help Bar +, -, *, /, H, Q\n");
                continue;
            }
    
    
    
    
            else if (sscanf(line, "%c %f %f", &op, &num1, &num2) == 3)
            {
                switch (op)
                {
                case '+':
                    result = num1 + num2;
                    break;
                case '-':
                    result = num1 - num2;
                    break;
                case '*':
                    result = num1 * num2;
                    break;
                case '/':
                    result = num1 / num2;
                    break;
                default:
                    continue;
                }
            printf("%c Operator: %.1f %c %.1f = %.1f \n", op, num1, op, num2, result);
        }
    }
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    For upper/lower case, toupper and tolower is the appropriate solution. EDIT: Note that these operators only take 1 operand, so your sscanf on line 64 wont work, you'll have to find a way to deal with this.

    For rounding, you can use the sprintf trick, or you can use round() and pow() funcitons (with some multiplicaiton and division). I like the round/pow method here since it shows a better understanding of the math behind the process, rather than a clever C-specific trick.

    For the partitioning: I don't quite understand what that is supposed to do. Perhaps if you could give a clearer explanation and maybe some examples, that would be good. My primary confusion is the use of 'percent'.

    Much of the code you copied worked, but two big complaints:
    1. You just copied and tried to change some crap without sufficient thought or caution -- you didn't take much time to understand what it is you were copying. This is one fundamental issue with copying: you don't learn to properly break down, solve and implement your problems/solutions.
    2. The code that is copied is often of poor quality. Even if it works, you're often "learning" poor technique, design, code organization, etc. It's not the worst, but the code you copied is far from the best example.

  5. #5
    Registered User
    Join Date
    Dec 2014
    Location
    Miami, Florida, United States
    Posts
    19
    yeah how do you add it in the switch loop because i tried

    [code]

    case'c';
    result = toupper(num2);
    break;

    (and not really im not gonna program in my life unless in i wanna learn on my own through android or something)

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    switch is not a loop. A loop is something that repeats. Switch is a type of conditional or branching statement.

    CommandsProj2.Dat
    Im getting one error and six warnings i think im overlooking something

    One problem, one forum, one thread.

  7. #7
    Registered User
    Join Date
    Dec 2014
    Location
    Miami, Florida, United States
    Posts
    19
    Sorry for saying that true, i just thought ppl werent responding lol but hey man the R operator works thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Prefix Programming giving weird results
    By tmac619619 in forum C Programming
    Replies: 1
    Last Post: 03-13-2014, 08:18 PM
  2. [OpenGL ]Dot3 bumpmapping weird results
    By W3D in forum Game Programming
    Replies: 2
    Last Post: 07-10-2012, 03:59 PM
  3. Getting weird results with isalpha
    By itsthemac in forum C Programming
    Replies: 7
    Last Post: 04-20-2011, 09:17 PM
  4. Dot3 bumpmapping: weird results
    By psychopath in forum Game Programming
    Replies: 12
    Last Post: 02-09-2006, 01:40 PM
  5. Showing Line Numbers in MSVC++
    By drdroid in forum Windows Programming
    Replies: 1
    Last Post: 01-09-2003, 03:23 PM

Tags for this Thread