我想通过scanf将字符和整数输入到结构中
I want to input character and integers by scanf into a structure
好的,所以我想使用 scanf 在结构中输入一个字母字符和三个数字,我想使用打印它的函数打印所有四个字符。但是每次我运行它时,我都会收到错误,说我无法运行它,或者有时它会正确打印除字符部分之外的所有内容,它只会变为空白。这可能有什么问题??
#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 an alphabern");
fflush(stdin);
scanf_s("%c", &a1);
printf("enter three 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);
}
avg()
的签名是错误的。第一个参数不应该是char*
而是char
。
因为我讨厌特定于 MSVC 的代码,所以您的代码应该是这样的。请注意,您应该检查读取是否成功。
#include <stdio.h>
struct Score
{
char a;
float x, y, z;
};
int main(void)
{
/* declareing function inside function is unusual, but not bad */
void avg(char a, float x, float y, float z);
char a1 = 'b';
float x1 = 0, y1 = 0, z1 = 0;
printf("enter an alphabern");
if (scanf("%c", &a1) != 1) {
puts("read error");
return 1;
}
printf("enter three 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);
}
相关文章:
- 如何打印不带空格的输入整数?
- 编写一个程序,输入整数 n 并打印该数字的字符数(符号除外)
- 如何在不中断剩余代码的情况下仅C++中输入整数
- 如何在输入整数时中断 for 循环
- C++ 在 CIN 双精度时挂起,但如果输入整数,则继续
- 我如何使用CIN输入整数,每个数字都被逗号分隔
- 如何输入整数并将其从最高到最低排序
- 如何创建一个程序,用户输入整数并输出90度数字金字塔
- 使用对输入整数的文件输入
- 使用户输入整数字符串,然后输出不同的字符串
- 从 fstream 到 2d 矢量的输入整数<矢量<int>> C++
- 用户输入整数后跟垃圾
- 输入整数直到 C/C++ 中的换行符
- 输入整数的最快方法
- C++循环 - 输入整数,直到用户退出
- 尝试输入整数行并在以矢量作为存储的函数的插入排序类型中排序
- 如何使C++程序在用户输入整数时抛出错误,而需要字符串
- 当要求输入整数并使用isdigit检查时,将char转换为int
- 如何通过指针输入整数数组并打印出数组和数组的大小(C++)
- 如果打印后输入整数,则C++分段故障,但其他情况下可以