Like Tree2Likes
  • 1 Post By Matticus
  • 1 Post By laserlight

How come my maths not working?

This is a discussion on How come my maths not working? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main (int argc, const char * argv[]) { int location, vehicle, weekday, peak, km, days; float ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    21

    How come my maths not working?

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[])
    {
    
    
        int location, vehicle, weekday, peak, km, days;
        float price, insurance;
        
        printf("Please choose a location(1: Toronto 2: Missisauga 3: Milton):\n");
        scanf("%d", &location);  
        
        switch (location) {
            case 1:
                printf("Which type of car(1: Pickup Truck 2: Cargo Truck 3: 10' Truck 4: 14' Truck 5: 20' truck):\n");
                scanf("%d", &vehicle);
                
                if (vehicle==5){
                    printf("not avail.\n");
                    break;}   
    
                printf("Enter weekday (1-Mon, 2-Tues 3-Weds,4-Thurs, 5-Fri, 6-Sat, 7-Sun):\n");
                scanf("%d", &weekday);
                if (weekday > 4){
                    peak = 1;}
                else peak == 0;
                
                printf("Enter number of KMs driven:\n");
                scanf("%d", &km);
                
                printf("Would you like insurance:(1-Yes/0-No)\n");
                scanf("%d", &insurance);
                
                if (insurance==1){
                    printf("How many days will you have the car for?\n");
                    scanf("%d", &days);
                    insurance=30*days;}
                else {insurance==0;}
                
                if (vehicle==1 && peak==1){
                    price=0.50*km+15;}
                
                if (vehicle==1 && peak==0){
                    price=0.50*km+12.50;}
                
                printf("Total is: %lf Insurance: %lf\n",price,insurance);
        }
        
        /*
         break;
         case 2:
         printf("you chose green color\n");
         break;
         case 3:
         printf("you chose blue color\n");
         break;
         default:
         printf("you did not choose any color\n");
         
         */
        
    
                
    }
    Compiling fine, just the the maths not making sense.

    IGNORE THE COMMENT AT BOTTOM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    The problem with the math lies here:

    Code:
    if (vehicle==1 && peak==1){
                    price=0.50*km+15;}
                
                if (vehicle==1 && peak==0){
                    price=0.50*km+12.50;}
                
                printf("Total is: %lf Insurance: %lf\n",price,insurance);
    If you enter anything except for '1' for "vehicle," then no calculations are performed and the variables contain junk. It is good practice to initialize your variables when they are declared; then you'd at least get predictable results.

    --

    Some other errors in your code:

    Code:
    else peak == 0;
    // ...
    else {insurance==0;}
    You're confusing the assignment operator (=) and the equality operator (==).

    ---

    Also, you're (correctly) declaring "main()" as returning an integer, but you aren't returning anything in your code.

    Code:
    return 0;
    ---

    You also do not seem to be using the "switch()" statement the way it is intended.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    Also, you should be checking to see if you have warnings when you compile. This will help uncover some of the problems I've mentioned.

    Code:
    ||In function 'main':|
    |26|warning: statement with no effect|
    |32|warning: format '%d' expects type 'int *', but argument 2 has type 'float *'|
    |38|warning: statement with no effect|
    |64|warning: control reaches end of non-void function|
    ||=== Build finished: 0 errors, 4 warnings ===|
    Salem likes this.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    21
    thanks fix those problems. can you please tell me why this is not computing correctly?

    Code:
    printf("Enter number of KMs driven:\n");
                scanf("%d", &km);
                if (km>100){
                    extra_km=(km-100)*0.50;    
                }
                else extra_km=0;

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    What makes you think it is not computing correctly?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    21
    This is what I'm getting:

    ~/c_programming> ./km
    Enter number of KMs driven:
    1000
    Extra KM: -0.00

    ---

    Basically I want it so if its over 100 KM that it gets charged 0.50cent/KM minus the base 100

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Works for me:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int km;
        double extra_km;
    
        printf("Enter number of KMs driven:\n");
        scanf("%d", &km);
        if (km > 100) {
            extra_km = (km - 100) * 0.50;   
        }
        else
            extra_km = 0;
    
        printf("Extra KM: %f\n", extra_km);
    
        return 0;
    }
    Output:
    Code:
    Extra KM: 450.000000
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    21
    Hmm yeah that fixed it? I guess it was the spacing? Grammar? o_O

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    I suspect it was the type of extra_km.
    Matticus likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. maths and c++
    By mkeisu in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2008, 04:59 PM
  2. Maths
    By AcerN30 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-03-2008, 02:13 PM
  3. Maths Help
    By bumfluff in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-16-2007, 10:53 AM
  4. Help with maths
    By Coritani in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 09:36 AM
  5. More Maths :(
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-20-2002, 10:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21