Thread: Issue on coding

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    3

    Post Issue on coding

    Hey everyone. I have an issue on the following code.

    Code:
    #include <stdio.h>
    
    // Indicating the 'main' function
    int main()
    // Starting of the 'main' function
    {
        // Declaring the variables
        int GarmentType, TestType, MachineReading;
        char GT[9], TT[20], QC[30];
        
        // Reading variables
        printf("Enter Garment Type : \n1 - Trousers\n2 - Shorts\n3 - T-Shirts\n4 - Jackets\n---> ");
        scanf("%d", &GarmentType);
    
    
        if (GarmentType == 1)
        {
            GT[9] = "Trousers";
        }
        else if (GarmentType == 2)
        {
            GT[9] = "Shorts";
        }
        else if (GarmentType == 3)
        {
            GT[9] = "T-Shirts";
        }
        else if (GarmentType == 4)
        {
            GT[9] = "Jackets";
        }
        else
        {
            printf("Error\n");
            return 0;
        }
    
    
    
    
        printf("Enter Test Type : \n1 - After Wash Testing\n2 - Seam Strength Testing\n---> ");
        scanf("%d", &TestType);
    
    
        if (TestType == 1)
        {
            TT[20] = "After Wash Testing";
        }
        else if (TestType == 2)
        {
            TT[20] = "Seam Strength Testing";
        }
        else
        {
            printf("Error\n");
            return 0;
        }
    
    
        printf("Enter Machine Reading ---> ");
        scanf("%d", &MachineReading);
    
    
        if (TestType == 1)
        {
            if ((MachineReading >= 55) && (MachineReading <= 80))
            {
                QC[30] = "Export Quality";
            }
            else if ((MachineReading >= 30) && (MachineReading <= 54))
            {
                QC[30] = "Marginal and Rejected";
            }
            else if ((MachineReading >= 0) && (MachineReading <= 29))
            {
                QC[30] = "Rejected";
            }
            else
            {
                printf("Error\n");
                return 0;
            }
    
    
        }
        else if (TestType == 2)
        {
            if ((MachineReading >= 80) && (MachineReading <= 120))
            {
                QC[30] = "Export Quality";
            }
            else if ((MachineReading >= 40) && (MachineReading <= 79))
            {
                QC[30] = "Marginal and Rejected";
            }
            else if ((MachineReading >= 0) && (MachineReading <= 39))
            {
                QC[30] = "Rejected";
            }
            else
            {
                printf("Error\n");
                return 0;
            }
        }
    
    
    
    
        printf("Garment type : %s\n", GT[9]);
        printf("Test type : %s\n", TT[20]);
        printf("Machine read : %d\n", MachineReading);
        printf("Quality : %s\n", QC[30]);
    
    
    
    
        return 0;
    
    }


    The question was to create a simple garment application and this is the base code. However, the code is not running properly and there are no syntax error. Although, there are many warnings displayed. Can someone help on it please?
    Last edited by TexRider; 04-01-2020 at 10:16 AM. Reason: Had a slight fix on the post. Had two codes displayed

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    However, the code is not running properly and there are no syntax error. Although, there are many warnings displayed.
    Really? Do you know that you should treat warnings as errors and fix the problems?

    By the way here is what my compiler says about your code:

    ||=== Build: Debug in chomework (compiler: gcc-8) ===|
    main.c||In function ‘main’:|
    main.c|18|error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]|
    main.c|22|error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]|
    main.c|26|error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]|
    main.c|30|error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]|
    main.c|47|error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]|
    main.c|51|error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]|
    main.c|68|error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]|
    main.c|72|error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]|
    main.c|76|error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]|
    main.c|90|error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]|
    main.c|94|error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]|
    main.c|98|error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]|
    main.c|110|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]|
    main.c|111|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]|
    main.c|113|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]|
    ||=== Build failed: 12 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
    Perhaps you need to up your compiler's warning level.

    Edit:
    All those "warnings" are really errors, using the wrong format specifier in either printf() or scanf() produces undefined behaviour, which means that anything can happen.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    You need to use strcpy to copy characters from one string to another. You can't just use assignment.

    Assignment can be used if you are just copying pointers to strings without actually copying the characters themselves. Your program could be written to work that way instead since you don't really need separate copies of the characters (you aren't modifying them, for example).

    You don't need to keep repeating the size of the array all over the place. In fact, indexing the array with it's size is, due to zero-indexing, one-past-the-end, and is therefore out-of-bounds.
    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main()
    {
        int GarmentType = 0;
        char GT[100];
     
        printf("Enter Garment Type:\n"
               "1 - Trousers\n"
               "2 - Shorts\n"
               "3 - T-Shirts\n"
               "4 - Jackets\n"
               "---> ");
        scanf("%d", &GarmentType);
     
        const char *s = NULL;
     
        switch (GarmentType)
        {
        case 1: s = "Trousers"; break;
        case 2: s = "Shorts";   break;
        case 3: s = "T-Shirts"; break;
        case 4: s = "Jackets";  break;
        default:
            printf("Error\n");
            return 0;
        }
        strcpy(GT, s);
     
     
        int TestType = 0;
        char TT[100];
     
        printf("Enter Test Type:\n"
               "1 - After Wash Testing\n"
               "2 - Seam Strength Testing\n"
               "---> ");
        scanf("%d", &TestType);
     
        switch (TestType)
        {
        case 1: s = "After Wash Testing";    break;
        case 2: s = "Seam Strength Testing"; break;
        default:
            printf("Error\n");
            return 0;
        }
        strcpy(TT, s);
     
     
        int MachineReading = 0;
        char QC[100];
     
        printf("Enter Machine Reading ---> ");
        scanf("%d", &MachineReading);
     
        switch (TestType)
        {
        case 1:
            if (MachineReading < 0 || MachineReading > 80)
            {
                printf("Error\n");
                return 0;
            }
            if (MachineReading >= 55)
                s = "Export Quality";
            else if (MachineReading >= 30)
                s = "Marginal and Rejected";
            else
                s = "Rejected";
            break;
        case 2:
            if (MachineReading < 0 || MachineReading > 120)
            {
                printf("Error\n");
                return 0;
            }
            if (MachineReading >= 80)
                s = "Export Quality";
            else if (MachineReading >= 40)
                s = "Marginal and Rejected";
            else
                s = "Rejected";
            break;
        }
     
        strcpy(QC, s);
     
     
        printf("Garment type : %s\n", GT);
        printf("Test type : %s\n", TT);
        printf("Machine read : %d\n", MachineReading);
        printf("Quality : %s\n", QC);
     
        return 0;
    }
    Last edited by john.c; 04-01-2020 at 11:40 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Apr 2020
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    Really? Do you know that you should treat warnings as errors and fix the problems?

    By the way here is what my compiler says about your code:



    Perhaps you need to up your compiler's warning level.

    Edit:
    All those "warnings" are really errors, using the wrong format specifier in either printf() or scanf() produces undefined behaviour, which means that anything can happen.
    Appreciate for pointing them out. In fact I bothered about the warnings as well. I suppose that was the reason why the program didn't run and crashed midway through.
    Thanks

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    3
    Quote Originally Posted by john.c View Post
    You need to use strcpy to copy characters from one string to another. You can't just use assignment.

    Assignment can be used if you are just copying pointers to strings without actually copying the characters themselves. Your program could be written to work that way instead since you don't really need separate copies of the characters (you aren't modifying them, for example).

    You don't need to keep repeating the size of the array all over the place. In fact, indexing the array with it's size is, due to zero-indexing, one-past-the-end, and is therefore out-of-bounds.
    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main()
    {
        int GarmentType = 0;
        char GT[100];
     
        printf("Enter Garment Type:\n"
               "1 - Trousers\n"
               "2 - Shorts\n"
               "3 - T-Shirts\n"
               "4 - Jackets\n"
               "---> ");
        scanf("%d", &GarmentType);
     
        const char *s = NULL;
     
        switch (GarmentType)
        {
        case 1: s = "Trousers"; break;
        case 2: s = "Shorts";   break;
        case 3: s = "T-Shirts"; break;
        case 4: s = "Jackets";  break;
        default:
            printf("Error\n");
            return 0;
        }
        strcpy(GT, s);
     
     
        int TestType = 0;
        char TT[100];
     
        printf("Enter Test Type:\n"
               "1 - After Wash Testing\n"
               "2 - Seam Strength Testing\n"
               "---> ");
        scanf("%d", &TestType);
     
        switch (TestType)
        {
        case 1: s = "After Wash Testing";    break;
        case 2: s = "Seam Strength Testing"; break;
        default:
            printf("Error\n");
            return 0;
        }
        strcpy(TT, s);
     
     
        int MachineReading = 0;
        char QC[100];
     
        printf("Enter Machine Reading ---> ");
        scanf("%d", &MachineReading);
     
        switch (TestType)
        {
        case 1:
            if (MachineReading < 0 || MachineReading > 80)
            {
                printf("Error\n");
                return 0;
            }
            if (MachineReading >= 55)
                s = "Export Quality";
            else if (MachineReading >= 30)
                s = "Marginal and Rejected";
            else
                s = "Rejected";
            break;
        case 2:
            if (MachineReading < 0 || MachineReading > 120)
            {
                printf("Error\n");
                return 0;
            }
            if (MachineReading >= 80)
                s = "Export Quality";
            else if (MachineReading >= 40)
                s = "Marginal and Rejected";
            else
                s = "Rejected";
            break;
        }
     
        strcpy(QC, s);
     
     
        printf("Garment type : %s\n", GT);
        printf("Test type : %s\n", TT);
        printf("Machine read : %d\n", MachineReading);
        printf("Quality : %s\n", QC);
     
        return 0;
    }
    Your code is working. Yet to be honest I couldn't understand the concept yet, since I haven't covered that section. But I am working on it.
    Appreciated.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    TBH, you copy/pasted your way to 100 extra lines, only to discover what you did was wrong.

    Code:
    #include <stdio.h>
     
    int main()
    {
        // Declaring the variables
        int GarmentType;
        char GT[9];
         
        // Reading variables
        printf("Enter Garment Type : \n1 - Trousers\n2 - Shorts\n3 - T-Shirts\n4 - Jackets\n---> ");
        scanf("%d", &GarmentType);
     
     
        if (GarmentType == 1)
        {
            GT[9] = "Trousers";
        }
    
        printf("Garment type : %s\n", GT[9]);
        return 0;
    }
    1. Start small
    Never write more code than you're prepared for the compiler just throwing it right back at you as being a pile of rubbish.
    Lots of small incremental changes built on successes is a much better approach than a single big bang that doesn't work.

    2. Compile often
    Enable as many warnings as you can.
    c++ - Flags to enable thorough and verbose g++ warnings - Stack Overflow
    Fix everything the compiler complains about, or just ask if you don't understand why your 'x code' causes 'y warnings'.

    3. Test regularly.
    It's very easy (yes, really) to write code that compiles perfectly.
    Writing code that does exactly what you want first time is much much harder.
    So after you've got some compilation success, make sure that translates into functional success as well.


    On a positive note, you have good code layout skills 👍
    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. Coding for Nintendo 64: Memory issue
    By fulumbler in forum C Programming
    Replies: 5
    Last Post: 03-10-2017, 10:19 AM
  2. sequence issue in coding...
    By Jenny Lay in forum C Programming
    Replies: 2
    Last Post: 12-29-2015, 09:40 PM
  3. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  4. Need some help with coding
    By Alphawaves in forum C Programming
    Replies: 27
    Last Post: 03-28-2007, 06:08 PM
  5. coding with dev-cpp
    By gamer in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2003, 02:09 AM

Tags for this Thread