【问题标题】:Problems with cin.getline() [closed]cin.getline() 的问题 [关闭]
【发布时间】:2015-07-16 15:39:23
【问题描述】:

有一个名为 URI 的网站有很多问题需要你解决,我正在解决这个问题,但显然发生了一些事情,我的程序输入错误,我不知道为什么。这是 URI 问题:

Maria has just started as graduate student in a medical school and she's needing
your help to organize a laboratory experiment which she is responsible about.
She wants to know, at the end of the year, how many animals were used in this
laboratory and the percentage of each type of animal is used at all.

This laboratory uses in particular three types of animals: frogs, rats and
rabbits. To obtain this information, it knows exactly the number of experiments
that were performed, the type and quantity of each animal is used in each
experiment.

Input

The first line of input contains an integer N indicating the number of test
cases that follows. Each test case contains an integer Amount (1 ≤ Amount ≤ 15)
which represents the amount of animal used and a character Type ('C', 'R' or
'S'), indicating the type of animal:
- C: Coelho (rabbit in portuguese)
- R: Rato (rat  in portuguese)
- S: Sapo (frog in portuguese)

Output

Print the total of animals used, the total of each type of animal and the
percentual of each one in relation of the total of animals used. The percentual
must be printed with 2 digits after the decimal point.

Input example:
10
10 C
6 R
15 S
5 C
14 R
9 C
6 R
8 S
5 C
14 R

Output example:
Total: 92 cobaias
Total de coelhos: 29
Total de ratos: 40
Total de sapos: 23
Percentual de coelhos: 31.52 %
Percentual de ratos: 43.48 %
Percentual de sapos: 25.00 %

这是我的代码:

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <iomanip>
#include <stdio.h>

using namespace std;

int main() {

    /**
     * Escreva a sua solução aqui
     * Code your solution here
     * Escriba su solución aquí
     */
     char input[5],num_char[2];
     int coelho = 0, rato = 0, sapo = 0,op, num, index;
     cin >> op;//Get how many time the loop will happen

     for(int i=0;i<op;i++){
        index = 0;
        fflush(stdin);
         cin.getline(input,4);
         //Copy the number part of  the input to the num_char[]  and  converts it to int
         while(input[index] != ' '){
            num_char[index] = input[index];
            index++;
         }
         num = atoi(num_char);
         //Get what animal is being tested
         if(input[index+1] == 'R') rato +=num;
         else if(input[index+1] == 'C') coelho +=num;
         else if(input[index+1] == 'S') sapo +=num;
     }
    //Print output
     cout << fixed << setprecision(2);
     cout << "Total: " << rato+sapo+coelho << " cobaias" << endl;
     cout << "Total de coelhos: " << coelho << endl;
     cout << "Total de ratos: " << rato << endl;
     cout << "Total de sapos: " << sapo << endl;
     cout << "Percentual de coelhos: " << coelho*100.0/(rato+sapo+coelho) << "%" << endl;
     cout << "Percentual de ratos: " << rato*100.0/(rato+sapo+coelho) << "%" << endl;
     cout << "Percentual de sapos: " << sapo*100.0/(rato+sapo+coelho) << "%" << endl;

    return 0;
}

这是问题的网页: https://www.urionlinejudge.com.br/judge/en/problems/view/1094

【问题讨论】:

  • 首先,不要在stdin 上调用fflush,这在技术上是未定义的行为。其次,您实际上并不需要getline(既不是成员函数也不是独立函数),因为所有数据行只包含一个整数和一个字符,您可以使用普通输入运算符&gt;&gt;
  • 不是 cin >> 字符串容易受到缓冲区溢出的影响吗?我被教导要尽可能避免使用 >> 运算符。而且 fflush(stdin) 消除了内存垃圾可能遇到的许多问题。至少我大学第一年他们就是这么教的
  • 为什么要读取字符串?输入包含 整数字符(但在读取字符时要小心,不要读取数字和字母之间的空格)。此外,如果读入std::string,它将根据需要增加其大小。

标签: c++ input cin


【解决方案1】:

你的问题是atoi 函数需要一个 C 风格的字符串,并且你应该知道所有 C 风格的字符串都有一个特殊的终止符,我看不到你有这会导致未定义的行为,因为atoi 越界尝试寻找终止符。

解决方案?对于您当前的解决方案,您需要添加字符串终止符。更好的解决方案?正如我在评论中所说,使用输入运算符&gt;&gt;,您将无需使用字符串或atoi

【讨论】:

  • 我试过了,还是不行
  • @JoãoAreias 您能否更新您的问题以表明您尝试了他的建议,也许我们可以弄清楚为什么它仍然不起作用
  • @JoãoAreias 嗯,这是您显示代码中最明显的错误,而且由于您没有提供更多细节,因此很难说其他任何内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 2021-11-06
  • 2012-06-21
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多