c++ - I want to input character and integers by scanf into a structure -
ok, i'd input one-letter character , 3 numbers structure using scanf, , want print 4 of them using function prints it. everytime run errors saying can't run it, or prints right except character part, go blank.. possibly wrong this??
#include <stdio.h> struct score { char a; float x, y, z; }; void main(void) { void avg(char *a, float x, float y, float z); char a1 = 'b'; float x1 = 0, y1 = 0, z1 = 0; printf("enter alphaber\n"); fflush(stdin); scanf_s("%c", &a1); printf("enter 3 numbers (ex:1,2,3)\n"); fflush(stdin); scanf_s("%f,%f,%f", &x1, &y1, &z1); struct score s1 = { a1, x1, y1, z1 }; avg(s1.a, s1.x, s1.y, s1.z); } void avg(char *a, float x, float y, float z) { printf("%c (%f,%f,%f) \n", a, x, y, z); }
the signature of avg()
wrong. first argument should not char*
char
.
because hate msvc-specific code, code should this. note should check whether readings successful.
#include <stdio.h> struct score { char a; float x, y, z; }; int main(void) { /* declareing function inside function unusual, not bad */ void avg(char a, float x, float y, float z); char a1 = 'b'; float x1 = 0, y1 = 0, z1 = 0; printf("enter alphaber\n"); if (scanf("%c", &a1) != 1) { puts("read error"); return 1; } printf("enter 3 numbers (ex:1,2,3)\n"); if (scanf("%f,%f,%f", &x1, &y1, &z1) != 3) { puts("read error"); return 1; } struct score s1 = { a1, x1, y1, z1 }; avg(s1.a, s1.x, s1.y, s1.z); } void avg(char a, float x, float y, float z) { printf("%c (%f,%f,%f) \n", a, x, y, z); }
Comments
Post a Comment