Very simple C program ( running total)

  • Thread starter Thread starter liquidicy
  • Start date Start date
  • Tags Tags
    Program Running
Click For Summary
SUMMARY

The forum discussion focuses on a simple C program designed to maintain a running total based on user input. The initial solution incorrectly increments the variable 'amount' without reading user input, leading to a misunderstanding of the assignment requirements. The correct approach involves initializing 'total' to 0 and using the 'scanf' function to read three values into 'amount', adding each to 'total' sequentially. The final solution correctly implements these requirements, ensuring that user input is captured during program execution.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with variable declaration and initialization in C
  • Knowledge of input functions, specifically 'scanf' in C
  • Basic understanding of loops, particularly 'do-while' loops
NEXT STEPS
  • Study the use of 'scanf' for user input in C programming
  • Learn about variable scope and lifetime in C
  • Explore error handling techniques for user input in C
  • Investigate best practices for code formatting and readability in C
USEFUL FOR

Students learning C programming, educators teaching programming fundamentals, and developers seeking to improve their understanding of user input handling in C.

liquidicy
Messages
3
Reaction score
0
Very simple C program ( running total) [solved]

Homework Statement


Question is:

Given that two int variables, total and amount , have been declared, write a sequence of statements that:
initializes total to 0
reads three values into amount , one at a time.

After each value is read into amount , it is added to the value in total (that is, total is incremented by the value in amount ).


Homework Equations





The Attempt at a Solution



My answer is this:

total = 0;
amount = 0;
do{
++amount;
total += amount;
}while (amount < 3);

Their response is:
Remarks:
⇒ Did you read in all three values?
Remarks:
⇒ At Execution

What did I do wrong?
 
Last edited:
Physics news on Phys.org
Apparently I was just supposed to copy paste it three times with scanf("%i",&amount) instead of amount++
 


liquidicy said:

Homework Statement


Question is:

Given that two int variables, total and amount , have been declared, write a sequence of statements that:
initializes total to 0
reads three values into amount , one at a time.

After each value is read into amount , it is added to the value in total (that is, total is incremented by the value in amount ).


Homework Equations





The Attempt at a Solution



My answer is this:

total = 0;
amount = 0;
do{
++amount;
total += amount;
}while (amount < 3);

Their response is:
Remarks:
⇒ Did you read in all three values?
Remarks:
⇒ At Execution

What did I do wrong?
It's a good idea to put [ code] and [ /code] tags (without leading spaces) around your code. I've done it below, and indented your code to make it more readable.
Code:
total = 0;
amount = 0;
do
{
   ++amount;
   total  +=  amount;
} while (amount < 3);

In answer to the questions above, no, you didn't read in any of the three values. The values should have been entered at program run time.

Yes, you should use some input function, such as scanf, to do input.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K