cin.getline()的问题

时间:2015-07-16 15:39:23

标签: c++ input cin

有一个名为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

1 个答案:

答案 0 :(得分:3)

你的麻烦是atoi函数需要一个C风格的字符串,并且你知道所有C风格的字符串都有一个特殊的终结符,我看不到你有{em>未定义的行为导致atoi超出界限试图寻找终结者角色。

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