简体   繁体   中英

Unusual behaviour in first C program

I am writing a program to work out who should pay what in the household, based upon their income. The desired behaviour is the following:

  • Print welcome message and ask how many people are in the household.
  • If the number entered is 10 or below, ask for their names.
  • Ask for each persons income. <- this is where the issue is
  • Display total income.
  • Calculate and display result.

Obviously this program is not complete and I need to stop the user entering negative values, but the biggest problem is that when the user enters each persons income, upon hitting the return key it does not ask for any more user input and the totalEarnings comes out as 0.

I am used to programming in C++ so I'm hoping I've just missed a quirk of C.

main.c

#include <stdio.h>

int main(void){

    short numberOfPeople = 0;
    char* names[10] = {0,0,0,0,0,0,0,0,0,0};
    float earnings[10] = {0,0,0,0,0,0,0,0,0,0};
    float totalEarnings = 0;
    float bills = 0;

    printf("Welcome!\nThis program calculates who should pay what for the bills in a proportional manner, based upon each persons income.\nHow many people are in your household?\n\n");

    do {
        printf("You can enter up to 10: ");
        scanf("%d", &numberOfPeople);
    } while(numberOfPeople > 10);

    puts("");

    for(short j = 0; j < numberOfPeople; ++j){
        printf("What is person %d's name? ", j+1 );
        scanf(" %s", &names[j]);
    }

    puts("");   

    for(short i = 0; i < numberOfPeople; ++i){
        printf("How much did %s earn this month? ", &names[i]);
        scanf(" %.2f", &earnings[i]);       
        totalEarnings += earnings[i];
    }

    printf("\nTotal earnings are %.2f.\n\n", &totalEarnings);

    printf("How much are the shared bills in total? ");
    scanf(" %.2f", &bills);

    puts("");

    for(short k = 0; k < numberOfPeople; ++k){
        printf("%s should pay %.2f", &names[k], &bills); 
    }

    puts("");   

    return 0;
}

You have not allocated any memory to hold the names so are scanf-ing them into null.

All bets are off after this point.

As @LoztInSpace pointed out, names is not allocated.

And another mistake is you're using & operator with %s and %f specifiers in in your printf() calls. It won't give result as expected. Also, is your intention really a loop to ask for "number of peoples"?

You have problems with extra & characters in the printf calls, which others have noted.

The problem you are reporting is likely caused by the line:

scanf(" %.2f", &earnings[i]);

The problem is that . in scanf formats has no defined meaning (it may be being ignored, or it may be causing the scanf call to fail.) The 2 limits the input to 2 characters, so will fail if anyone has an income with more than 2 digits. So you need to get rid of those and what you REALLY need is to CHECK THE RETURN VALUE of scanf to see if it is failing and do something appropriate if it is. Something like:

while (scanf("%f", &earnings[i]) != 1) {
    scanf("%*[^\n]"); /* throw away the rest of the line */
    printf("That doesn't look like a number, what did they earn this month? ");
}

Something similar should be done with all the other scanf calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM