Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
write a program that prompts the user for current and previous electricity meter value in kilowatts per hour and the cost per kilowatt per hour. The program should then calculate the cost of current consumption


What I have tried:

i am a beginner,so i havent quite come up with anything substantial
Posted
Updated 23-Mar-23 21:22pm
Comments
Dave Kreskowiak 22-Mar-23 14:09pm    
And your question would be? If you think you're going to get a tutorial on C, that's not going to happen in these little textboxes. If that's what you want, go Google for "C tutorial".
Mike Hankey 22-Mar-23 14:21pm    
What have you come up with?

I had a similar problem 50 years ago when I was learning to program. We didn't have google back then and nobody would do it for us. That's where I learned my debugging techniques and how do figure out problems.
If I got stuck I went to the instructor and asked for help, but if you asked a question like you have he would have thrown you out of his office. You had to show or explain to him what you did and where you were stuck. I found that if I did that the instructor was more than weilling to help.

Here is a place to start : cplusplus.com reference: fgets[^]. That function can be used to acquire input from a user. The page includes a sample of how to use it to prompt a user. I will leave the calculation part of this to you.

To accept input from the user pass stdin as the file pointer to fgets.

ETA: modified per merano99's excellent suggestion.
 
Share this answer
 
v3
Comments
merano99 22-Mar-23 19:02pm    
Since you can't specify the buffer size with gets() too long user input would quickly overwrite something, I would suggest fgets().
https://cplusplus.com/reference/cstdio/fgets/

In addition, the gets() function is no longer supported by current compilers for these reasons as well. [NOTE: This function is no longer available in C or C++ (as of C11 & C++14)]
Rick York 22-Mar-23 21:45pm    
Good idea.
merano99 23-Mar-23 2:06am    
5+; very nice, thanks for mentioning
 
Share this answer
 
Comments
merano99 23-Mar-23 13:45pm    
Are you sure you really want to recommend scanf("%s", stringVariable);?
CPallini 24-Mar-23 1:30am    
It is tricky, I admit. Anyway the OP needs only numbers.
Without time information, only with input of two meter readings and the price per kW/h, only the total price between the two meter readings can be calculated. Under the current consumption I would understand something else.

As CPallini has already mentioned, these are only numerical values. If still the date or text inputs should become necessary, the effort would be higher. The background is that one should secure programs in principle against incorrect user inputs. There are countless possibilities, what can go wrong and some things could lead to a crash of the program or to wrong results. The approach to read everything as text first, as Rick York suggests, to be able to check it then and also to be able to print a meaningful error message is a bit more complex but works for most of the input you want to get from a user. To turn a string into a number you could use strtol(). So it would be possible to read all numbers in one line. This is especially good if the numbers are in a text file.

Here is an example for strtol()
https://www.tutorialspoint.com/c_standard_library/c_function_strtol.htm

For other input formats there is a lot more, but that would lead too far here.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900