Thread: Need help on programming.

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

    Need help on programming.

    So I'm a new member here and i don't know how to make things coding for the amount of total claim for the question.
    Here is what i got so far
    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<stdlib.h>
    int main(int argc, char*argv)
        {
        char clas,response;
        int dista;
        float amount, rate;
        printf("Enter Class of Vehicle: ");
        scanf("%c",&clas);
        Label1:
        printf("Enter Distance Travelled(km):");
        scanf("%d",&dista);
        switch(clas)
        {
        case'A':
        case'a':rate = 0.70;break;
        case'B':
        case'b':rate = 0.60;break;
        case'C':
        case'c':rate = 0.50;break;
        case'D':
        case'd':rate = 0.45;break;
        default:printf("Wrong Input");rate=0;
        }
        amount = rate*dista;
        printf("\n Amount of Claim 1 (RM): %.2f",amount);
        printf("\n Next mileage Claim(EnterY/N):");
        if(response 'Y')
        {
            goto Label2;
        }
        else(response 'N')
        {
            printf("End");
        }
        }
    
        Label2:
        printf("\n Enter Distance Travelled(km): ");
        scanf("%d",&dista);
    
        switch(clas)
        {
        case'A':
        case'a':rate = 0.70;break;
        case'B':
        case'b':rate = 0.60;break;
        case'C':
        case'c':rate = 0.50;break;
        case'D':
        case'd':rate = 0.45;break;
        default:printf("Wrong Input");rate=0;
        }
        amount = Label1 + Label2;
        printf("Total Amount of Claim: %.2f",amount);
        return(0);
    What should i do to make it work? I've been trying to solve this for a few weeks now

    Or I tried this one out but still stuck. Which method is better for having total amount of claim?

    Code:
    #include<stdio.h>
    int main()
        {
        char clas, next, response, Y, N;
        int i, dista;
        float amount1,amount2, rate, grandtotal;    
        printf("Enter Class of Vehicle: ");
        scanf("%c", &clas);
        printf("Enter Distance Travelled(km):");
        scanf("%d", &dista);
        
        switch(clas)
        {
        case'A':
        case'a':rate = 0.70;break;
        case'B':
        case'b':rate = 0.60;break;
        case'C':
        case'c':rate = 0.50;break;
        case'D':
        case'd':rate = 0.45;break;
        default:printf("Wrong Input");rate=0;
        }
        amount1 = rate*dista;
        printf("\n Amount of Claim 1 (RM): %.2f",amount1);
        i++;
        printf("\n Next Mileage Claim (Enter Y/N)");
        scanf("%c",&next);
        if(response == Y);
        {
        printf("\n Enter Distance Travelled(km):");
        scanf("%d", &dista);
        }
        if else(response == N);
        {
            printf("End");    
        }
        amount2 = rate*dista;
        printf("\n Amount of Claim 2 (RM): %.2f",amount2);
            
        //This is the total amount i'm trying to make
        grandtotal = amount1 + amount2;
        printf("Total Amount of Claim = %.2f",grandtotal);
        
        return(0);
        }
    Last edited by BlazKaz; 04-09-2019 at 07:40 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Be cautious using goto - It can make your program very difficult to debug and reduce the readability.

    It is usually advised that you don't use 'goto' at all, unless you need to exit from a deeply nested loop.


    Break your problem into section such as:
    * Intro message
    * Get Vehicle class and distance travelled
    * Get rate
    * Display result
    * Implement do/while loop to go back to the start if the user enters 'y' or 'Y' when prompted
    * Add grand total functionallity
    * ...

    Make a new programme. Start with the first section that you made before - Don't move on until it is 100% working.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,077
    Quote Originally Posted by Click_here View Post
    Be cautious using goto - It can make your program very difficult to debug and reduce the readability.

    It is usually advised that you don't use 'goto' at all, unless you need to exit from a deeply nested loop.


    Break your problem into section such as:
    * Intro message
    * Get Vehicle class and distance travelled
    * Get rate
    * Display result
    * Implement do/while loop to go back to the start if the user enters 'y' or 'Y' when prompted
    * Add grand total functionallity
    * ...

    Make a new programme. Start with the first section that you made before - Don't move on until it is 100% working.
    Dijkstra would be proud...

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    You can do yourself a favor and format your code correctly. This is not a nitpicky "because you're supposed to but no one really does" kind of thing. Your code is legitimately difficult to read. I can't be sure what the closing brace on line 37 does to. It looks like it closes the main function, which will cause the program to not compile.

    This is bad:
    Code:
    int main(int argc, char *argv[])
      {
      for(int x = 0; x < 10; x++)
      {
      for(int y = 0; y < 10; x++)
      {
      // Stuff
      }
      }
    It's not too bad here, but as the code gets longer, it gets worse and word. This is better:
    Code:
    int main(int argc, char *argv[])
    {
      for(int x = 0; x < 10; x++)
      {
        for(int y = 0; y < 10; x++)
        {
          // Stuff
        }
      }
    Form the indentation, it's very clear what the structure of the program is.

    Next, don't use goto. It's rarely appropriate in C and it's from an era where programming languages didn't have very good looping features. I would suggest not using it at all until you learn where it's appropriate. And using goto to things like "label1" and "label2" is even worse. Name your labels something meaningful.

    I was going to give your program a crack, but honestly I can't make heads or tails of it. Someone else suggested that you start over, start small and build the program iteratively and I think that's a great idea.

    Also, what book are you using to learn C? Because I really recommend the K. N. King book. If you really read that book and do the exercises (yes, all of them), you'll be having a much better time of this than struggling with this one program for weeks.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    3
    Quote Originally Posted by Click_here View Post
    Be cautious using goto - It can make your program very difficult to debug and reduce the readability.

    It is usually advised that you don't use 'goto' at all, unless you need to exit from a deeply nested loop.


    Break your problem into section such as:
    * Intro message
    * Get Vehicle class and distance travelled
    * Get rate
    * Display result
    * Implement do/while loop to go back to the start if the user enters 'y' or 'Y' when prompted
    * Add grand total functionallity
    * ...

    Make a new programme. Start with the first section that you made before - Don't move on until it is 100% working.
    got it, ill rebuild again soon

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    3
    Quote Originally Posted by gaxio View Post
    You can do yourself a favor and format your code correctly. This is not a nitpicky "because you're supposed to but no one really does" kind of thing. Your code is legitimately difficult to read. I can't be sure what the closing brace on line 37 does to. It looks like it closes the main function, which will cause the program to not compile.

    This is bad:
    Code:
    int main(int argc, char *argv[])
      {
      for(int x = 0; x < 10; x++)
      {
      for(int y = 0; y < 10; x++)
      {
      // Stuff
      }
      }
    It's not too bad here, but as the code gets longer, it gets worse and word. This is better:
    Code:
    int main(int argc, char *argv[])
    {
      for(int x = 0; x < 10; x++)
      {
        for(int y = 0; y < 10; x++)
        {
          // Stuff
        }
      }
    Form the indentation, it's very clear what the structure of the program is.

    Next, don't use goto. It's rarely appropriate in C and it's from an era where programming languages didn't have very good looping features. I would suggest not using it at all until you learn where it's appropriate. And using goto to things like "label1" and "label2" is even worse. Name your labels something meaningful.

    I was going to give your program a crack, but honestly I can't make heads or tails of it. Someone else suggested that you start over, start small and build the program iteratively and I think that's a great idea.

    Also, what book are you using to learn C? Because I really recommend the K. N. King book. If you really read that book and do the exercises (yes, all of them), you'll be having a much better time of this than struggling with this one program for weeks.
    Oh, i just started programming so im having a hard time with it on my own with just youtube and internet but thanks for helping me out. i learned the C programming on internet so there is no books here that relates to coding. but thanks for your suggestion on the book. ill look it up for now

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    I would get away from videos and random stuff you find on the internet for the time being. Some of it is very low quality, but the real issue is that it's inconsistent. You need a good book that introduces things in the correct (or as correct as can be) order, all explained well with plenty of exercises to get you to put it into practice.

    Trust me when I say that learning from bad sources is a big mistake. I learned C from a pocket reference guide and a text file someone had written. This was before I had the internet so I couldn't ask questions, either. I got so much stuff wrong and honestly it was just a waste of time. Get a good book however you can and work it from cover to cover, and ask questions when you get stuck. You have these two advantages I didn't have, use them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 02-01-2019, 12:27 PM
  2. Replies: 0
    Last Post: 02-01-2019, 12:22 PM
  3. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

Tags for this Thread