mardi 29 décembre 2020

Asking the user if they want to run a program again in C

I'm new to coding so please forgive me if this is a stupid question. I am trying to create a program in C where the user can input the number of a month and then they are told how many days are in that month. My code works in a console, until I try to implement a way where the user is asked to begin the program again.

This is what I have tried to use so far in order to get it to work, but I cannot wrap my head around why it isn't. Any help would be greatly appreciated.

#include <stdio.h>

char choice;
do
{
    void printDays(int D) 
    { 
        if (D == 2) { 
            printf("This month has 28 days"); 
        } 

        else if (D == 4 || D == 6 
                || D == 9 || D == 11) { 
            printf("This month has 30 days"); 
        } 

        else if (D == 1 || D == 3 || D == 5 
            || D == 7 || D == 8 || D == 10 
            || D == 12) { 
            printf("This month has 31 days"); 
        } 
  
        else { 
            printf("Invalid month"); 
        } 
    } 

    int main() 
    { 
        int D = ; 
    
        printDays(D); 
    } 
    printf("Run the program again? (Y/N) ?"); 
    scanf("%c",&choice);
    if(choice=='n' || choice=='N') 
        break;
}while(1); 

Aucun commentaire:

Enregistrer un commentaire