ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have played with this for a while and need a fresh set of eyes to help me if you will:
I have to write a program which asks for an integer number and stores as num. add num to total . Do this 6 times and calculate the average. Display total and average. Must reuse same variable.
This is what I have so far:
#include <stdio.h>
int main()
{
int num, avg, total;
total = 0;
printf("Enter an integer:");
scanf("%f", &num);
total = total+num;
printf("\n Enter an integer:");
scanf("%f", &num);
total = total + num;
printf("\n Enter an integer:");
scanf("f%", &num);
total = total+num;
printf("\n Enter an integer:");
scanf("%f:, &num);
total = total+num;
printf("\n Enter an integer:");
scanf("%f:, &num);
total = total+num;
printf("\n Enter an integer:");
scanf("%f:, &num);
total = total+num;
avg = total / 6.0;
printf("\n The total is %f", total);
printf("\n The average of the six numbers is %f", avg);
return 0;
}
I get the following errors when I run my compiler:
23: Unterminated string or character constant in function main()
24: Function call missing ) in function main()
These errors repeat on lines 27, 28, 31, & 32.
Any help would be appreciated. Explanations for what I am doing wrong will really be appreciated.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Before seeking help from others, you should first try to find by yourself.
The error message is very clear, you have an unterminated string in line 23, just look better here.
And instead of cut and paste, try using the for() loop.
this is (in my opinion) clearly a homework question, and as such not really what LQ.org is here for. you need to learn how to code yourself, not get answers from other people.
you do though have very obvious syntax errors on those lines, which aren't even the same as the ones which precede it.
I fixed the syntax errors. Thank you for pointing out the fact that the code lines weren't the same. I am new at this programming thing and if I don't know what something means and cannot find it in a book, how am I supposed to learn what to do. A friend of mine told me about this site and how helpful people were. That is why I asked this question. Thank you for your assistance.
you can read a number with %f then store it to an int variable.
scanf converts number to floating point representation then copy it to &x. you can read it as int.
casting is not performed in this case.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.