0

After giving a letter as input, my for loop skips the second user input and defaults it to 10. The user doesn't get a chance to type anything on the second iteration. As a result, the output is always x * 10 which is not ideal. How can I fix this?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void transport();

int main(void){
    transport();

    return 0;
}

void transport(){
    printf("Answer the following questions by typing a valid input!\n");

    char questions[2][500] = {"What kind of transportation do you use regularly?\nOptions:\n" 
    "e - electric car\n"
    "g - gas car\n"
    "m - motorcycle\n"
    "t - train\n"
    "b - bus\n"
    "w - walking or bike\n", 
    "How many hours a day do you travel by this transportation?\n"};
    int answers[2];

    char input;

    for(int i = 0; i < sizeof(questions) / sizeof(questions[0]); i++){
        printf("%s", questions[i]);
        scanf("%c", &input);

        switch(input)
        {
        case 'e':
            answers[i] = 1;
            break;
        case 'g':
            answers[i] = 2;
            break;
        case 'm':
            answers[i] = 3;
            break;
        case 't':
            answers[i] = 4;
            break;
        case 'b':
            answers[i] = 5;
            break;
        case 'w':
            answers[i] = 0;
            break;
        default:
            answers[i] = input;
            break;
        }
    }

    int ton = answers[0];
    int hour = answers[1];
    int ton_per_day = ton * hour;
    int ton_per_week = ton * hour * 7;
    int ton_per_year = ton * hour * 7 * 52;

    printf("Your CO2 emissions for your chosen transportation is %d ton per day, %d ton per week and %d ton per year!\n", ton_per_day, ton_per_week, ton_per_year);

    for(int n = 0; n < 2; n++){
        printf("%d\n", answers[n]);
    }
}

I'm open to all suggestions :)

0 Answers0