Slavic_Donut
Slavic_Donut

Reputation: 51

Trouble parsing through C++ vector

I need to make a program that takes input of integers separated by whitespace, so for example:

4 4 5 8 8 9

The program then takes these numbers and computes the number of occurrences of each number, so the output for the above input would be:

The number 4 has 2 occurrence(s)
The number 5 has 1 occurrence(s)
The number 8 has 2 occurrence(s)
The number 9 has 1 occurrence(s)

I have this almost figured out, and it worked fine when I did it for input where numbers weren't separated by spaces(assuming they're 1 digit integers, not an assumption I'm making for the end version) but as soon as the input has spaces in-between the numbers it no longer works.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;

vector<int> parseString(string &s);
void parseVector(vector<int> &v);
int checkRepeats(vector<int> &v, int n);
void printVector(vector<int> &v);

int main()
{
vector<int> parsed;
vector<int> numbers;
string input;
bool keepGoing = true;
int nRepeats;                               // stores the number of times a number occurs, will constantly be overwritten

cout << "Enter some numbers: ";

while(true)
{   
    cin >> input;

    if(input == "stop" || input == "Stop")
    {
        break;
    }

    parsed = parseString(input);                                                    // parse input string to vector of ints
    parseVector(parsed);                                                            // send vector of ints to be checked for repeats
    //printVector(parsed);

    //cout << "\n";
}
}

void printVector(vector<int>&v)                                                     // not called right now, used for testing
{
    for(int i = 0; i < v.size(); i++)
    {
        cout << v.at(i) << " ";
    }
}

void parseVector(vector<int> &v)
{
    int x = 0;
int j = 0;
int nRepeats = 0;
int size = v.size();

for(int i = 0; i < size; i++)
{
    x = v.at(i);                                                                    // x equals the next element in vector 'v'
    nRepeats = checkRepeats(v, x);                                                  // count the number of times number 'x' occurs in vector 'v'
    //i += nRepeats - 1;
    cout << "The number: " << x << " has: " << nRepeats << " occurrence(s)\n";
}
}

int checkRepeats(vector<int> &v, int n)                                                 // counts the number of times a number is found in a given vector
{
    int nTimes = 0;
int size = v.size();

for(int i = 0; i < size; i++)
{

    if(v.at(i) == n)                                                                // match found, increment counter
    {
        nTimes++;
    }
}

return nTimes;
}

vector<int> parseString(string &s)
{
    vector<int> v;
int strLen = s.size();
int x;

for(int i = 0; i < strLen; i += 2)                                                  // increment by 2 to cut out white space from between the numbers
{
    x = s.at(i);
    x -= 48;                                                                        // subtract 48 from x, converts from ascii to int value
    v.push_back(x);
}

return v;
}

If you go to line 88 of that code, and change the increment of the loop counter from i += 2 to i++ it will work perfectly for input with no spaces, such as 445889 instead of 4 4 5 8 8 9

Does anyone know what I could try to fix this?

Upvotes: 0

Views: 76

Answers (2)

user657267
user657267

Reputation: 21000

You could try using map (super-naive version:)

#include <map>
#include <iostream>

int main()
{
  int i;
  std::map<int, int> ints;

  while (std::cin >> i)
    ++ints[i];

  for (auto const& num : ints)
    std::cout <<
      "The number " << 
      num.first <<  
      " has " <<  
      num.second << 
      " occurrence(s)\n";
}

Upvotes: 5

Jason Baker
Jason Baker

Reputation: 2481

cin reads from standard input until it hits a whitespace character, so you need to re-think how you're populating your vector. Other than that, the code should work fine.

Upvotes: 0

Related Questions