Thread: How Can I test for characters in in char variable and multiply floats

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    1

    How Can I test for characters in in char variable and multiply floats

    Just a to let you know, I began learning C few days ago so I only know some syntax. Anyways, here is the problem. I dont know if, if(char var == 'character') is wrong or is there different way to multiply different floats? Thank you in advance

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define offroad 2.2
    #define motorway 1.3
    
    
    int main()
    {
    
    
        float way1;
        float mile1;
        float way2;
        float mile2;
        float way3;
        float mile3;
        char answer1 [2];
        char answer2 [2];
        char answer3 [2];
    
    
    //Ask options to calculate results
        printf("Please enter the information in order to obtain your results\n");
    
    
        printf("Will the road be a motorway(m) or offroad (o)?\n");
        scanf(" %c", answer1);
        printf("What distance in KM will you cover?\n");
        scanf(" %f", &way1);
    
    
        printf("Will the road be a motorway(m) or offroad (o)?\n");
        scanf(" %c", answer2);
        printf("What distance in KM will you cover?\n");
        scanf(" %f", &way2);
    
    
        printf("Will the road be a motorway(m) or offroad (o)?\n");
        scanf(" %c", answer3);
        printf("What distance in KM will you cover?\n");
        scanf(" %f", &way3);
    
    
        mile1 = way1;
        mile2 = way2;
        mile3 = way3;
    
    
    // Here is my problem way1 = way1 * motorway etc. doesnt work or the "if(answer1 == 'm')" statement is wrong.
    
        if(answer1 == 'm'){
            way1 *= motorway;
        }
        if(answer1 == 'o'){
            way1 *= offroad;
        }
        if(answer2 == 'm'){
            way2 *= motorway;
        }
        if(answer2 == 'o'){
            way2 *= offroad;
        }
        if(answer3 == 'm'){
            way3 *= motorway;
        }
        if(answer3 == 'o'){
            way3 *= offroad;
        }
    //Convert way variables into miles
        mile1 *= 1.6;
        mile2 *= 1.6;
        mile3 *= 1.6;
    
    
        printf("Trip 1 will be %.3f miles and use %.3f liters of fuel\n", mile1, way1);
        printf("Trip 2 will be %.3f miles and use %.3f liters of fuel\n", mile2, way2);
        printf("Trip 3 will be %.3f miles and use %.3f liters of fuel\n", mile3, way3);
    
    
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,736
    See, the way you've done it, answer1, 2 and 3 are char arrays, not char variables. Since you use "%c" in scanf, you don't need arrays. Change them to plain char variables and pass their addresses to scanf, like you do with way1, 2 and 3.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create floats from const char* ?
    By heatblazer in forum C Programming
    Replies: 2
    Last Post: 07-28-2015, 01:52 AM
  2. Replies: 7
    Last Post: 09-19-2011, 01:37 PM
  3. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  4. Replies: 2
    Last Post: 10-10-2009, 10:38 PM
  5. Help w/ complicated variable test
    By Jungle in forum C Programming
    Replies: 4
    Last Post: 03-01-2005, 04:48 PM

Tags for this Thread