问题描述
我想在一个线路输入扫描并将其存储在一个结构。我不知道如果我将它存储错误或我打印错了。我不知道如何使用 scanf函数
与为
循环,因为我之前不提ç喜欢从未做过覆盖东西。所以,我不知道对如何处理这一点。
这是我的东西放在一起,但是当我打印,我得到的垃圾数量。我正打算也许使用指针,但我的教授不会让我们使用它。这就是为什么我有麻烦了。
EDITED
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;#定义MAX 3
#定义MAXTRIP 6
结构停
{
浮动费用;
时差;
};结构之旅
{
焦炭Dest_letter;
INT stop_num;
结构停腿[MAX];
};INT主要(无效)
{
INT trip_num,索引,I;
结构之旅旅游[MAXTRIP] 的printf(请输入车次号:);
scanf函数(%d个,&安培; trip_num);
的printf(请输入停止,每一站的成本/长度的目的字母/数字:\\ n);
对于(指数= 0;指数 - LT; trip_num;指数++)
{
scanf函数(%C%D,&安培;旅行[指数] .Dest_letter,&安培;旅行[指数] .stop_num);
对于(i = 0; I<旅行[指数] .stop_num;我++)
scanf函数(%F%F,&安培;旅行[指数] .leg [I]。成本,和放大器;旅行[指数] .leg [I]。时间);
}
对于(指数= 0;指数 - LT; trip_num;指数++)
{
的printf(跳闸数:%d \\ n目标函:%C,指数+ 1,游[指数] .Dest_letter);
对于(i = 0; I<旅行[指数] .stop_num;我++)
的printf(。费用:%2F \\ nLength:%2F,游[指数] .leg [I]。成本,出行[指数] .leg [I]。时间);
}
}
您有您的阅读循环内您的打印循环。它试图在阅读后的第一个打印出所有的行程信息。
编辑:这里的问题是,路 scanf函数
处理单个字符是有点儿直观旁边处理字符串和数字的方式。它从标准在第二天的性格,这可能是从当你打完了previous输入一个换行符。然后继续尝试读取一个整数,而是它发现你本来打算由%C消耗信
。这将导致 scanf函数
失败,无法初始化 stop_num
。
围绕这一点的一种方式可能是代替读取的字符串。 scanf函数
将开始读取的第一个非空白字符字符串,并停在第一个空格字符阅读。然后,只取你读串入缓冲区中的第一个字符。
的#include<&stdio.h中GT;#定义MAX 3
#定义MAXTRIP 6结构停止{
浮动费用;
时差;
};结构之旅{
焦炭Dest_letter;
INT stop_num;
结构停腿[MAX];
};INT主要(无效)
{
INT trip_num,索引,I;
结构之旅旅游[MAXTRIP]
炭缓冲液[10]; 的printf(请输入车次号:);
scanf函数(%d个,&安培; trip_num);
对于(指数= 0;指数 - LT; trip_num;指数++){
的printf(请输入站的目的地的字母/数字:\\ n);
scanf函数(%s%D,缓冲,和放大器;旅行[指数] .stop_num);
旅游[指数] .Dest_letter =缓冲区[0];
对于(i = 0; I<旅行[指数] .stop_num;我++){
的printf(请输入停止%d个成本/长度:\\ n,I);
scanf函数(%F%F,&安培;旅行[指数] .leg [I]。成本,和放大器;旅行[指数] .leg [I]。时间);
}
}
的printf(%d个车次\\ n,trip_num);
对于(指数= 0;指数 - LT; trip_num;指数++){
的printf(跳闸数:%d \\ n目标函:%C \\ N,指数+ 1,游[指数] .Dest_letter);
对于(i = 0; I<旅行[指数] .stop_num;我++)
的printf(成本:%2F \\ nLength:%2F \\ n。游[指数] .leg [I]。成本,出行[指数] .leg [I]。时间);
}
}
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 scanf
with for
loops since I never done it before not to mention C likes to overwrite stuff. So I wasn't sure on how to approach this.
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);
}
}
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 scanf
handles 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 scanf
to fail and not initialize stop_num
.
One way around this may be to read in a string instead. scanf
will 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.
#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);
}
}
这篇关于使用scanf函数一行Scaning多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!