c++ - Problems with cin.getline() -


there website called uri has lot of problems solve, solving 1 problem apparently happening program getting wrong input , have no idea why. uri problem:

maria has started graduate student in medical school , she's needing organize laboratory experiment responsible about. wants know, @ end of year, how many animals used in laboratory , percentage of each type of animal used @ all.  laboratory uses in particular 3 types of animals: frogs, rats , rabbits. obtain information, knows number of experiments performed, type , quantity of each animal used in each experiment.  input  first line of input contains integer n indicating number of test cases follows. each test case contains integer amount (1 ≤ amount ≤ 15) represents amount of animal used , character type ('c', 'r' or 's'), indicating type of animal: - c: coelho (rabbit in portuguese) - r: rato (rat  in portuguese) - s: sapo (frog in portuguese)  output  print total of animals used, total of each type of animal , percentual of each 1 in relation of total of animals used. percentual must printed 2 digits after 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 % 

and code:

#include <iostream> #include <string.h> #include <stdlib.h> #include <iomanip> #include <stdio.h>  using namespace std;  int main() {      /**      * escreva sua solução aqui      * code 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 loop happen       for(int i=0;i<op;i++){         index = 0;         fflush(stdin);          cin.getline(input,4);          //copy number part of  input num_char[]  ,  converts int          while(input[index] != ' '){             num_char[index] = input[index];             index++;          }          num = atoi(num_char);          //get animal 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; } 

and webpage of problem: https://www.urionlinejudge.com.br/judge/en/problems/view/1094

your trouble atoi function expects c-style string, , should know c-style strings have special terminator character, don't see having leads undefined behavior atoi goes out of bounds trying terminator character.

the solution? current solution need add string terminator. better solution? use input operators >> said in comment, , have no need use strings or atoi.


Comments