-1

What is wrong with this. Everything is working except the first element if the first element contains any zero. e.g. input -> 100 output -> 10 input->6030 output->630.

Question:

  • Input Format

    The first and only line consists of n integers separated by commas.

  • Output Format

    Print the integers after parsing it.

Code:

#include <sstream>
#include <vector>
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

vector<int> parseInts(string str) {
    // Complete this function
    stringstream ss(str);
    char ch;
    vector<int> v;
    int s;
    int i = 0;
    ss >> ch;
    cout << ch;
    while(ss >> i){
        v.push_back(i);
        ss >> ch;
    }    
    return v;
}

int main() {
    string str;
    cin >> str;
    vector<int> integers = parseInts(str);
    for(int i = 0; i < integers.size(); i++) {
        cout << integers[i] << "\n";
    }
    
    return 0;
}
Community
  • 1
  • 1
HarshM
  • 21
  • 6
  • 2
    Because it'll bite you eventually: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) and [Why should I not #include ?](https://stackoverflow.com/q/31816095/364696) should be high on your reading list. – ShadowRanger Jan 08 '20 at 06:21

2 Answers2

1
ss >> ch;
cout << ch;

The problem is here where you extract 1 char from the input stream before going into the loop and reading the rest. Also the way you have written the code is that a number is made of only a single digit because you can only read in 1 digit into a char.

If the input is a comma separated list of integers on a single line it is better to read in the whole line using std::getline() and parse the line. You can specify the delimiter as the third parameter, and convert each string read to an integer using std::stoi(). This will also allow you read in numbers containing more than one digit.

So try changing parseInts() as follows:

std::vector<int> parseInts(string str) {
    // Complete this function
    std::stringstream ss(str);
    std::vector<int> v;
    std::string number;

    while (getline(ss, number, ','))
    {
        v.push_back(std::stoi(number));
    }

    return v;
}

One thing to note is the use of >>operator vs getline. The former will read until a space or newline while the latter will read a whole line. You are only able to read in the whole line using >> if it doesn't contain any spaces.

jignatius
  • 6,304
  • 2
  • 15
  • 30
0

What is wrong with this

You're reading from the string stream in a wrong manner.

To understand why you got 630 output for 6030 input, dry run your code. You're reading a character ch from stream so ch is assigned the value '6'. Now you're again reading from stream in while loop using variable i but i is an integer and so "030" is converted to 30(int) and you finally get 630 as output.

Note that for cases like 678, 5, 10, etc. you'll get the correct output but not for all test cases.

Here's one correct way to do it:

#include <sstream>
#include <vector>
#include <iostream>

std::vector<int> parseInts(std::string str) {
    std::stringstream whole_str(str);
    std::string integer_str;
    int integer;
    std::vector<int> v;
    while(whole_str >> integer_str){ // Read 1 integer(string) at a time
        std::stringstream one_int(integer_str);
        one_int >> integer; // Read and convert string to int
        v.push_back(integer);
    }
    return v;
}

int main() {
    std::string str{"100 200 5060 2040"};
    std::vector<int> integers = parseInts(str);
    for(int i = 0; i < integers.size(); i++) {
        std::cout << integers[i] << "\n";
    }
    return 0;
}

Output

100
200
5060
2040
Ajay Dabas
  • 1,404
  • 1
  • 5
  • 15