Minh
Minh

Reputation: 61

Program does not exit when expected

I'm new to C and I am trying to write a program that will let you enter up to 100 people's age and salary. The program at first will print some sentences to introduce (the display function) and then ask you whether you want to continue or not (yes_no function). After you enter a person's information, the program will also ask if you would like to continue to enter the next person's information or not. If you would like to continue, you will need to enter 1 and 0 for no/exit. When I compile and run the code, I found a problem and i could not figure out why!

The problem is that if you choose 0 (which means no/exit) after entering one's information, the program does not exit. It instead just asks the next person's information. But when I choose 0 right from the beginning, it just exits like usual. Why?

#include<stdio.h>

#define max 100
#define yes 1
#define no 0

int display(void);
int yes_no(void);
void get_data(void);

int date[max],month[max],year[max]; 
int cont;
int salary;

int main(){
    cont=display();
  if (cont==yes){
        get_data();
    }
return 0;
}

int display(void){
    printf("This program will let you enter ");
    printf("the age and salary of up to 100 people ");

    cont=yes_no();
    return cont;
}

int yes_no(void){
    int i=0;
    printf("\nDo you want to continue? Enter 1 for Yes and 0 for No\n");
    scanf("%d", &i);
    while(i<0 || i>1){
        printf("Invalid value.Please enter again\n");
        scanf("%d", &i);
    }
    if(i==1){
        return (yes);
    }else return (no);
}

void get_data(void){
    int i=0;
    for(i=0;i<max;i++){
        printf("Enter information for people %d\n", i+1);
        printf("Enter birthday\n");
        do{
            printf("Enter date\n");
            scanf("%d", &date[i]);
        }while( 0>date[i] || 31<date[i] );
        do{
             printf("Enter month\n");
             scanf("%d", &month[i]);
        }while( 0>month[i] || 12<month[i]);
        do{
             printf("Enter year\n");
             scanf("%d", &year[i]);
        }while( 1900>year[i] || 2016<year[i]);

             printf("Enter salary\n");
             scanf("%d", &salary);

    cont=yes_no();
    }
}

Upvotes: 3

Views: 109

Answers (1)

muXXmit2X
muXXmit2X

Reputation: 2765

void get_data(void){
    int i=0;
    for(i=0;i<max;i++){
        printf("Enter information for people %d\n", i+1);
        printf("Enter birthday\n");
        do{
            printf("Enter date\n");
            scanf("%d", &date[i]);
        }while( 0>date[i] || 31<date[i] );
        do{
             printf("Enter month\n");
             scanf("%d", &month[i]);
        }while( 0>month[i] || 12<month[i]);
        do{
             printf("Enter year\n");
             scanf("%d", &year[i]);
        }while( 1900>year[i] || 2016<year[i]);

             printf("Enter salary\n");
             scanf("%d", &salary);

    cont=yes_no(); // <== The Problem lies here
    }
}

You ask the user if wants to continue but you never check the return value of yes_no() Just add this after this line and it should work like a charm:

if (cont == no)
    return;

As others have mentioned, there are still some things you can do to "improve" your code.

defines should be capitalized so #define YES 1 would stick to this convention.

And you shouldn't use global variables. These are bad programming style. Simply pass the things you need in other functions as a parameter and if you need the manipulated value later on past them as a pointer.

Also the formatting could be improved (however this is a mostly opinion based topic ;) ) In C you usually have an extra line for each curly bracket.

void get_data(void)
{
...
}

//instead of
void get_data(void){
...
}

Also the blanks after the do-while-loop should look more like so:

do
{
...
} while(1900 > year[i]); //here the curly bracket is ok that way 

And operators should have a blank on both sides:

printf("Enter information for people %d\n", i + 1);
// instead of this
printf("Enter information for people %d\n", i+1);

This is all I've seen up to now.

Upvotes: 4

Related Questions