简体   繁体   中英

Struggling to write a c program the runs the way this assignment is describing

so the assignment is like this:

"write one C program that asks the user to input the number of coins that the user has and then output the total amount of money the user has. Assume that the user has dimes, nickels, pennies and quarters only. After the user enters the number of coins for each denomination, the program should output the total amount of money. The program should work with any number of coins entered. How many pennies do you have? 10

You entered 10

How many nickels do you have? 5

You entered 5

How many dimes do you have? 1

You entered 1

How many quarters do you have? 1

You entered 1

You have $0.70

Thank you."

The code i wrote looks like this:

#include<stdio.h>
#include<stdlib.h>
main() {
    int pennyTotal, nickleTotal, dimeTotal, quarterTotal, result;
    printf("How many pennies do you have?\n");
    scanf_s("%i", &pennyTotal);
    printf("How many nickles do you have?\n");
    scanf_s("%i", &nickleTotal);
    printf("How many dimes do you have?\n");
    scanf_s("%i", &dimeTotal);
    printf("How many quarters do you have?\n");
    scanf_s("%i", &quarterTotal);
    printf("You have\n");
    result = pennyTotal + nickleTotal + dimeTotal + quarterTotal;
    printf(result);
    printf("Thank you.\n");
    system("pause");

so far I got the "how many..." part right. It's just the part where I set the values and get the output to be how much money the user has when they enter a random number. I'm so lost but i feel like I'm really close.

Yes, you are close. The main problem you have is that printf(result); is wrong. You need printf("%d", result);

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