我正在尝试扫描单行输入并将其存储在结构中。我不确定是我存储错误还是打印错误。我不确定如何将 scanf
与 for
循环一起使用,因为我以前从未做过,更不用说 C 喜欢覆盖东西了。所以我不确定如何处理这个问题。
这是我放在一起的东西,但是当我打印时,我得到了垃圾号码。我打算也许使用指针,但我的教授不会让我们使用它。这就是我遇到麻烦的原因。
已编辑
#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);
}
}
最佳答案
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);
}
关于c - 使用 scanf 从一行中扫描多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5453650/