| | |
Regarding previous programming q 2
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2006
Posts: 33
Reputation:
Solved Threads: 0
Hi,
Anyone here can advise me correction of my c programming?
It doesn't seem to be working.
#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;
main()
{
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);
}
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?
Anyone here can advise me correction of my c programming?
It doesn't seem to be working.
#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;
main()
{
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);
}
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?
•
•
•
•
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?
C Syntax (Toggle Plain Text)
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.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
•
•
Try something like:
C Syntax (Toggle Plain Text)
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.
May 'the Google' be with you!
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.
Well here is a near foolproof Implementation of the input accepting phase for your program which accpets only valid floats.
C Syntax (Toggle Plain Text)
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 ; }
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
•
•
Now that is sweet, except I wouldn't use exit (1), but would rather stay in the input loop.
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.
Last edited by ~s.o.s~; Oct 2nd, 2006 at 2:54 pm.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
Join Date: Oct 2006
Posts: 33
Reputation:
Solved Threads: 0
Hi,
Can this be done?
#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;
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);
}
{
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);
}
But seem to hv compilng errors...can anyone please help me out where should i amend correctly yo make it works?
0001 #include<stdio.h>0002 float x[4] = {1.2,2.4,3.6,4.8};0003 float f[4] = {0.1,0.2,0.3,0.4};0004 float xave = 0.0;0005 float ftotal = 0.0;0006 main()0007 {0008 int i;0009 float weights[4] = {0};0010 char buf[80];0011 for(i = 0; i < 4; ++i)0012 {0013 printf("Enter weight #%d", i+1);0014 fgets(buf,sizeof(buf),stdin);0015 weights[i] = atof(buf);0016 }0017 {0018 int i;0019 for (i=0; 1<4;i++) ftotal + = f[i]; parse error before `='
0020 if(ftotal!=1.0)0021 {0022 printf("error\n");0023 exit(); at this point in file0024 }0025 for(i=0;1<4;i++) xave+=f *x; invalid operands `float[4]' and `float[4]' to binary `operator *'0026 printf("\nThe weights are %f"&f); invalid operands `const char[20]' and `float[4]' to binary `operator &'0027 printf("\nThe average is %f\n",xave);0028 }0029 0030 0031 parse error at end of input confused by earlier errors, bailing out


Syntax Errors found in /usr/include/stdlib.h:
Can this be done?
#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;
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);
}
{
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);
}
But seem to hv compilng errors...can anyone please help me out where should i amend correctly yo make it works?
0001 #include<stdio.h>0002 float x[4] = {1.2,2.4,3.6,4.8};0003 float f[4] = {0.1,0.2,0.3,0.4};0004 float xave = 0.0;0005 float ftotal = 0.0;0006 main()0007 {0008 int i;0009 float weights[4] = {0};0010 char buf[80];0011 for(i = 0; i < 4; ++i)0012 {0013 printf("Enter weight #%d", i+1);0014 fgets(buf,sizeof(buf),stdin);0015 weights[i] = atof(buf);0016 }0017 {0018 int i;0019 for (i=0; 1<4;i++) ftotal + = f[i]; parse error before `='
0020 if(ftotal!=1.0)0021 {0022 printf("error\n");0023 exit(); at this point in file0024 }0025 for(i=0;1<4;i++) xave+=f *x; invalid operands `float[4]' and `float[4]' to binary `operator *'0026 printf("\nThe weights are %f"&f); invalid operands `const char[20]' and `float[4]' to binary `operator &'0027 printf("\nThe average is %f\n",xave);0028 }0029 0030 0031 parse error at end of input confused by earlier errors, bailing out

Syntax Errors found in /usr/include/stdlib.h:
•
•
Join Date: Oct 2006
Posts: 33
Reputation:
Solved Threads: 0
0001 #include<stdio.h>0002 float x[4] = {1.2,2.4,3.6,4.8};0003 float f[4] = {0.1,0.2,0.3,0.4};0004 float xave = 0.0;0005 float ftotal = 0.0;0006 main()0007 {0008 int i;0009 float weights[4] = {0};0010 char buf[80];0011 for(i = 0; i < 4; ++i)0012 {0013 printf("Enter weight #%d", i+1);0014 fgets(buf,sizeof(buf),stdin);0015 weights[i] = atof(buf);0016 }0017 {0018 int i;0019 for (i=0; 1<4;i++) ftotal + = f[i]; parse error before `='
0020 if(ftotal!=1.0)0021 {0022 printf("error\n");0023 exit(); at this point in file0024 }0025 for(i=0;1<4;i++) xave+=f *x; invalid operands `float[4]' and `float[4]' to binary `operator *'0026 printf("\nThe weights are %f"&f); invalid operands `const char[20]' and `float[4]' to binary `operator &'0027 printf("\nThe average is %f\n",xave);0028 }0029 0030 0031 parse error at end of input confused by earlier errors, bailing out


Syntax Errors found in /usr/include/stdlib.h:
0020 if(ftotal!=1.0)0021 {0022 printf("error\n");0023 exit(); at this point in file0024 }0025 for(i=0;1<4;i++) xave+=f *x; invalid operands `float[4]' and `float[4]' to binary `operator *'0026 printf("\nThe weights are %f"&f); invalid operands `const char[20]' and `float[4]' to binary `operator &'0027 printf("\nThe average is %f\n",xave);0028 }0029 0030 0031 parse error at end of input confused by earlier errors, bailing out

Syntax Errors found in /usr/include/stdlib.h:
![]() |
Similar Threads
- Screening MySQL/PHP Programmers (MySQL)
- Need java tutor? (Java)
- Regarding my previous programming part 3 (C)
- MySQL and VB6 (Visual Basic 4 / 5 / 6)
- Java1 or Java2? (Java)
- Problems launching java source code (serious newbie, sorry! :( ) (Java)
Other Threads in the C Forum
- Previous Thread: pthreads scheduling problem?
- Next Thread: a simple question
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework inches include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






