Hi,
What should i add on so that user can enter the four weights from the keyboard. The program should print an error message is the weights are out of range?
Try something like:
int counter = 1 ;
while (counter <= 4 )
{
printf ("Enter the weight %d: ", counter) ;
scanf ("%d", &my_weight [counter - 1] ) ;
if ( some_condition_of_validity)
{
counter ++ ;
}
else
{
printf ("Incorrect choice!!!") ;
}
}
This should keep on asking the user the choice until he enters a correct number.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
Try something like:
int counter = 1 ;
while (counter <= 4 )
{
printf ("Enter the weight %d: ", counter) ;
scanf ("%d", &my_weight [counter - 1] ) ;
if ( some_condition_of_validity)
{
counter ++ ;
}
else
{
printf ("Incorrect choice!!!") ;
}
}
This should keep on asking the user the choice until he enters a correct number.
One mild problem from past experience, if the users enters a floating point number, this loop spins out of control. Give it a try, it's good exercise.
vegaseat
DaniWeb's Hypocrite
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
OH sorry Mr. Vegaseat my bad, thanks for pointing out the flaw to me. The OP required the user to input floating point values so i actually shouldnt have taken a decimal input in the first place.
Well here is a near foolproof Implementation of the input accepting phase for your program which accpets only valid floats.
int main (void)
{
int counter = 1 ;
float my_weight = 0;
char buffer [BUFSIZ] = {'\0'} ;
while (counter <= 4 )
{
printf ("Enter the weight %d: ", counter) ;
fgets (buffer, BUFSIZ, stdin) ;
if ( ! isdigit (buffer [0] ) )
{
printf ("Illegal input") ;
exit (1) ;
}
if ( buffer [strlen (buffer) - 1] == '\n' )
buffer [strlen (buffer) - 1] = '\0' ;
my_weight = atof (buffer) ;
if ( my_weight > 0 && my_weight < 10)
{
counter ++ ;
}
else
{
printf ("Incorrect choice!!!\n") ;
}
}
return 0 ;
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
Now that is sweet, except I wouldn't use exit (1), but would rather stay in the input loop.
vegaseat
DaniWeb's Hypocrite
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
Now that is sweet, except I wouldn't use exit (1), but would rather stay in the input loop.
Thanks for the appreciation.
Hmm i guess you are right, no use being too harsh :)
But that just involves adjusting the "if" stmt a bit and voila one would get what he wants.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
Looks like you have somewhat incomplete code:
#include<stdio.h>
float x[4] = {1.2,2.4,3.6,4.8};
float f[4] = {0.1,0.2,0.3,0.4};
float xave = 0.0;
float ftotal = 0.0;
int main()
{
int i;
float weights[4] = {0};
char buf[80];
for(i = 0; i < 4; ++i)
{
printf("Enter weight #%d", i+1);
fgets(buf,sizeof(buf),stdin);
weights[i] = atof(buf);
}
//???????????? missing something here!!!
{
int i;
for (i=0; 1<4;i++) ftotal + = f[i];
if(ftotal!=1.0)
{
printf("error\n");
exit();
}
for(i=0;1<4;i++) xave+=f *x;
printf("\nThe weights are %f"&f);
printf("\nThe average is %f\n",xave);
// blahblah...
return 0;
}
There are quite a number of obvious errors too!
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
I didnt change the logic of the program but just made the syntax errors which bugged you to go away. I would very much recommend you to format your code properly otherwise i wont be much of a help in the near future coz its really difficult to make out the problem with the code formatted in a bad way.
Also in your code in place of "i" variable you have written the number "1" which caused your program to crash. If you are coding in Notepad i would very much recommend you to switch to a syntax highlighter and indenter IDE which would make your as well as our task easy.
int main(void)
{
double x[4] = {1.2,2.4,3.6,4.8};
double f[4] = {0.1,0.2,0.3,0.4};
double xave = 0.0;
double ftotal = 0.0;
int i;
double weights[4] = {0};
char buf[BUFSIZ];
for(i = 0; i < 4; ++i)
{
printf ("Enter weight #%d ", i+1);
fgets (buf, BUFSIZ, stdin);
if ( buf [strlen(buf) - 1] == '\n' )
buf [strlen(buf) - 1] = '\0' ;
weights[i] = atof(buf);
}
for (i=0; i <4; i++)
ftotal += f[i];
if ( (int) ftotal != 1 )
{
printf("error\n");
exit (1);
}
for(i=0; i<4;i++)
{
xave += f[i]*x[i] ;
printf("\nThe weights are %f",f [i]);
}
printf("\nThe average is %f\n",xave);
}
Hope it helped, bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
hmmm. This is getting out of hand. Is it just me or are more and more people expecting us to do their work?
What should i add on so that user can enter the four weights from the keyboard. The program should print an error message is the weights are out of range?But is there amuch easily way of writing that part as it's seem pretty tough for me to understand.Please advise. Thanks. But seem to hv compilng errors...can anyone please help me out where should i amend correctly yo make it works? Can kindly advise what's wrong and what should i input into the program to make it complete? Please advise. Please advise what should i change or input inorder for the program to work It seem like my program doesn't work. Could you kindly advise what part should i amend or input for this program to work based on your professional skill in c programming. Please kindly advise.
All these are just asking someone else to do your work for you. All the code and error messages are next to crap. Do your own work, and ask proper questions with proper error messages next time. Use code tags also. Post crap and I delete.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115