Thread: tolower and toupper arent showing

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

    tolower and toupper arent showing

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #define LINE_MAXLENGTH 80
    
    
    
    
    int main()
    {
        float num1, num2;
        char op, c;
    
    
        FILE* cmd = fopen("CommandsProj2.dat", "r");
    
    
        if (cmd == NULL)
        {
            printf("Error Code (404)");
            return EXIT_FAILURE;
        }
            printf("Help Bar +, -, *, /, H, Q\n");
    
    
        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 (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;
    
    
                case 'c':
                    result = printf("%c", toupper(c));
                    break;
    
    
                case 'C':
                    result = printf("%c", tolower(c));
                    break;
    
    
    
    
                default:
                    continue;
                }
    
    
            printf("%c Operator: %.2f %c %.2f = %.2f \n", op, num1, op, num2, result, c);
        }
    }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2014
    Location
    Miami, Florida, United States
    Posts
    19
    The program only prints the add, sub, multiply, and division and not the character case change

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Dejavu???

    I think everyone give you the suggestion that fgets (like the most other string functions) works with char arrays.
    Code:
    […]
        int line[LINE_MAXLENGTH + 1];
    
        while (fgets(line, LINE_MAXLENGTH + 1, cmd))
    
    […]
    This will not work!
    String functions expect arrays of characters, not arrays of intergers.
    An character is only 1 byte in size, an integer can have more and it depends on the architecture.
    The string functions work byte by byte, but this is broken if you use intergers.
    So i suggest (like other before me):
    Code:
    […]
        char line[LINE_MAXLENGTH + 1];
    
        while (fgets(line, LINE_MAXLENGTH + 1, cmd))
    
    […]
    Other have classes, we are class

  4. #4
    Registered User
    Join Date
    Dec 2014
    Location
    Miami, Florida, United States
    Posts
    19
    Quote Originally Posted by WoodSTokk View Post
    Dejavu???

    I think everyone give you the suggestion that fgets (like the most other string functions) works with char arrays.
    Code:
    […]
        int line[LINE_MAXLENGTH + 1];
    
        while (fgets(line, LINE_MAXLENGTH + 1, cmd))
    
    […]
    This will not work!
    String functions expect arrays of characters, not arrays of intergers.
    An character is only 1 byte in size, an integer can have more and it depends on the architecture.
    The string functions work byte by byte, but this is broken if you use intergers.
    So i suggest (like other before me):
    Code:
    […]
        char line[LINE_MAXLENGTH + 1];
    
        while (fgets(line, LINE_MAXLENGTH + 1, cmd))
    
    […]
    yes I have it too lol because when i put char line its the same result just the add etc...

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    There's a very important concept in programming - giving your variables meaningful names. While this means nothing to the compiler, it's very important to anyone reading your code - most especially you. Giving your variables meaningful names makes your code more self-documenting, so it's easier to understand what's going on at a glance.

    Now, with that being said, what is the variable "c" supposed to represent?

    Now ask yourself - when is the variable "c" given a value?

    If your answer is along the lines of, "Wait, it isn't!" then you're on the right track.

    Maybe you're confusing a character variable that might hold the value 'c' or 'C' with a variable named "c"? Who knows - you didn't provide a thorough description of your program, nor a sample of the input file, nor the expected output. And you've shown many signs, throughout several threads, of applying guesswork rather than an attempt at methodical understanding.

  6. #6
    Registered User
    Join Date
    Dec 2014
    Location
    Miami, Florida, United States
    Posts
    19
    Quote Originally Posted by Matticus View Post
    There's a very important concept in programming - giving your variables meaningful names. While this means nothing to the compiler, it's very important to anyone reading your code - most especially you. Giving your variables meaningful names makes your code more self-documenting, so it's easier to understand what's going on at a glance.

    Now, with that being said, what is the variable "c" supposed to represent?

    Now ask yourself - when is the variable "c" given a value?

    If your answer is along the lines of, "Wait, it isn't!" then you're on the right track.

    Maybe you're confusing a character variable that might hold the value 'c' or 'C' with a variable named "c"? Who knows - you didn't provide a thorough description of your program, nor a sample of the input file, nor the expected output. And you've shown many signs, throughout several threads, of applying guesswork rather than an attempt at methodical understanding.
    num1 and num2 are for the switch statements so i can use the operations (op), c is for knowinig when to use it for any char inputs like for uppercase and lowercase i didnt want to put so much online but ppl stop responding. and i didnt want to put the dat file on too so ppl wouldnt think that they have to do all the work but the program is suppose to scan the text file and apply the operation for each line going down, the (c A) and (C b) are uppercase and lowercase
    P [Print k-th Digit ] Print out the k-th digit of integer
    R [Round Reals ] Round double value x to i significant decimal placesS [Separate ] Separate out the sign, integer part and fractional part of double value x


    D [Partition Integer ] 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% of i and truncate it you get j


    H [Help ]Print a short synopsis of all the available commands
    Q [Quit ] Quit

    the text file
    Code:
    DA
    H
    + 3 5
    - 10 5
    * 8 9
    / 92 3
    c A
    C b
    P 56234 3
    R 3.14195 4
    S -51.235
    D 50 10
    Q

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating Strings? (tolower, toupper, ucfirst)
    By Atolar in forum C Programming
    Replies: 4
    Last Post: 02-10-2010, 08:00 AM
  2. toupper() & tolower() problem?
    By saqib_ in forum C++ Programming
    Replies: 35
    Last Post: 12-04-2009, 11:45 AM
  3. Need help with toupper and tolower function
    By phoebus in forum C Programming
    Replies: 8
    Last Post: 04-27-2008, 10:18 PM
  4. toupper(); tolower(); ?
    By dune911 in forum C Programming
    Replies: 9
    Last Post: 01-07-2003, 06:40 PM
  5. DX SDK8 examples that arent set up right!
    By lightatdawn in forum Windows Programming
    Replies: 2
    Last Post: 03-13-2002, 05:46 PM

Tags for this Thread