C语言 使用 scanf 从一行扫描多个输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5453650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scanning Multiple inputs from one line using scanf
提问by Thao Nguyen
I'm trying to scan in a single line input and storing it in a struct. I'm not sure if I am storing it wrong or I am printing it wrong. I'm not sure on how to use scanfwith forloops since I never done it before not to mention C likes to overwrite stuff. So I wasn't sure on how to approach this.
我正在尝试扫描单行输入并将其存储在结构中。我不确定是我存储错误还是打印错误。我不知道如何使用scanf与for循环,因为我从来没有这样做更何况ç喜欢的东西覆盖。所以我不确定如何解决这个问题。
This is something I put together but when I print I get junk numbers. I was intending to maybe use pointers but my professor won't let us use it. Which is why I'm having trouble.
这是我放在一起的东西,但是当我打印时,我得到了垃圾号码。我本来打算使用指针,但我的教授不让我们使用它。这就是我遇到麻烦的原因。
EDITED
已编辑
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 3
#define MAXTRIP 6
struct stop
{
float cost;
float time;
};
struct trip
{
char Dest_letter;
int stop_num;
struct stop leg[MAX];
};
int main(void)
{
int trip_num, index, i;
struct trip travel[MAXTRIP];
printf("Enter number of trips: ");
scanf("%d", &trip_num);
printf("Please enter destination letter/number of stops and cost/length of each stop:\n");
for (index = 0; index < trip_num; index++)
{
scanf("%c %d", &travel[index].Dest_letter, &travel[index].stop_num);
for ( i=0; i < travel[index].stop_num; i++)
scanf("%f %f", &travel[index].leg[i].cost, &travel[index].leg[i].time);
}
for (index =0; index < trip_num; index++)
{
printf("Trip:%d \nDestination Letter:%c", index+1, travel[index].Dest_letter);
for (i=0; i < travel[index].stop_num; i++)
printf("Cost:%.2f \nLength:%.2f", travel[index].leg[i].cost, travel[index].leg[i].time);
}
}
采纳答案by Null Set
You have your printing loop inside your reading loop. It is trying to print out all of the trip information after reading the first one in.
你的阅读循环中有你的打印循环。它试图在阅读第一个信息后打印出所有的旅行信息。
Edit: The trouble here is that the way scanfhandles single characters is kinda unintuitive next to the way it handles strings and numbers. It reads the very next character from standard in, which is probably a newline character from when you finished typing the previous input. Then it proceeds to try and read an integer, but instead it finds the letter you had intended to be consumed by the %c. This causes scanfto fail and not initialize stop_num.
编辑:这里的问题是scanf处理单个字符的方式与处理字符串和数字的方式相比有点不直观。它从标准输入中读取下一个字符,这可能是您完成前一个输入后的换行符。然后它继续尝试读取一个整数,但它找到了您打算由%c. 这会导致scanf失败而不是初始化stop_num。
One way around this may be to read in a string instead. scanfwill start reading the string at the first non-whitespace character and stop reading it at the first whitespace character. Then just take the first character from the buffer you read the string into.
解决此问题的一种方法可能是读入字符串。scanf将在第一个非空白字符处开始读取字符串,并在第一个空白字符处停止读取。然后从你读入字符串的缓冲区中取出第一个字符。
#include <stdio.h>
#define MAX 3
#define MAXTRIP 6
struct stop {
float cost;
float time;
};
struct trip {
char Dest_letter;
int stop_num;
struct stop leg[MAX];
};
int main(void)
{
int trip_num, index, i;
struct trip travel[MAXTRIP];
char buffer[10];
printf("Enter number of trips: ");
scanf("%d", &trip_num);
for (index = 0; index < trip_num; index++) {
printf("Please enter destination letter/number of stops:\n");
scanf("%s %d", buffer, &travel[index].stop_num);
travel[index].Dest_letter = buffer[0];
for (i = 0; i < travel[index].stop_num; i++){
printf("Please enter cost/length of stop %d:\n", i);
scanf("%f %f", &travel[index].leg[i].cost, &travel[index].leg[i].time);
}
}
printf("%d trips\n", trip_num);
for (index = 0; index < trip_num; index++) {
printf("Trip:%d \nDestination Letter:%c\n", index + 1, travel[index].Dest_letter);
for (i = 0; i < travel[index].stop_num; i++)
printf("Cost:%.2f \nLength:%.2f\n", travel[index].leg[i].cost, travel[index].leg[i].time);
}
}
回答by R.M.VIVEK ARNI
scanf()is used to get a value for runtime and used in control string
scanf()用于获取运行时的值并用于控制字符串
main()
{
//this R.M.VIVEK coding for Scaning Multiple inputs from one line using scanf
int r,m,v1,i,v,e,k;
char a,b,c;
float x,y,z;
clrscr();
printf("enter the Your five subject marks");
//%d is integer format ,
scanf("%d%d%d%d%d",&r,&m,&v,&i,&e);
//%c is char format and %s is a sting formar
printf("enter the any character values");
scanf("%c%c%c",a,b,c);
//%f is float format
printf("enter the Your height and wight");
scanf("%f%f",&x,&y);
}

