使用scanf从一行扫描多个输入 [英] Scanning Multiple inputs from one line using scanf

查看:317
本文介绍了使用scanf从一行扫描多个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图扫描单行输入并将其存储在结构中.我不确定是否存储错误或打印错误.我不确定如何在for循环中使用scanf,因为我从来没有做过,更不用说C喜欢覆盖东西了.所以我不确定该如何处理.

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.

已编辑

#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.

这里的麻烦是scanf处理单个字符的方式与处理字符串和数字的方式有点不直观.它从标准输入中读取下一个字符,这可能是您输入完先前的输入后的换行符.然后,它继续尝试读取一个整数,但是相反,它找到了您打算由%c使用的字母.这会导致scanf失败并且不初始化stop_num.

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.

解决此问题的一种方法可能是改为读取字符串. scanf将开始在第一个非空白字符处读取字符串,并停止在第一个空白字符处读取字符串.然后,从读取字符串的缓冲区中取出第一个字符.

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从一行扫描多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆