0

Was hoping to print out the values of test1. I just would like to ask how come the values of test1 are not printed out and only prints out "PRINT START" and "PRINT END". Any help or idea is highly appreciate.

#include <vector>
#include <iostream>

using namespace std;

void print_out(vector<int> numbers)
{

    cout << "------------- PRINT START -------------" << endl;

    for( auto i: numbers )
        cout << i << endl;

    cout << "------------- PRINT END -------------" << endl;
}

vector<int> beautify(vector<string> numbers)
{

    vector<int> result;
    return result;
}

int main()
{

    vector<string> test1;
    test1.push_back("3167389213");
    test1.push_back("32989741893");
    test1.push_back("2138");

    print_out(beautify(test1));
    return 0;
}

Update

Thank you, So I've applied the codes inside beautify though it still cant output the test1 values.

vector<int> beautify(vector<string> numbers)
{
    vector<int> result;
    for (auto & i : numbers)
        result.push_back(std::stoi(i));
    return result;
}
Quentin
  • 62,093
  • 7
  • 131
  • 191
Cj Medina
  • 469
  • 2
  • 6
  • 10
  • 1
    What is the problem and where is your question? Also, please take a look at the [tour](http://stackoverflow.com/tour) and [how to ask](http://stackoverflow.com/help/how-to-ask). – moffeltje Jul 01 '15 at 08:08
  • Why convert to `int` just to print, why not print directly – Shreevardhan Jul 01 '15 at 08:12
  • `beautify` returns empty vector which is passed to `print_out` so how can you expect values of `test1` as output. – Gaurav Sehgal Jul 01 '15 at 08:14
  • Please do not make edits that invalidate existing answers, add updates to your question instead. If you encounter a different problem, please ask a separate question. – Quentin Jul 01 '15 at 08:56
  • @Quentin thanks for the reminder I'll keep that in mind. – Cj Medina Jul 01 '15 at 09:41

2 Answers2

1

Ok, this is the flow of your program:

  • you create an empty vector
  • you fill it with strings that contains digits
  • you call the print_out function with the argument beautify(test1) so we need to look what beautify() returns.
  • beautify() returns an empty vector (int)
  • print_out() prints all elements from the empty vector, so none.

This code is equivalent and maybe clarifies something:

int main()
{

    vector<string> test1;
    test1.push_back("3167389213");
    test1.push_back("32989741893");
    test1.push_back("2138");

    vector<int> newVector = beautify(test1);
    print_out(newVector); //here newVector is empty
    return 0;
}

What you probably want to do is in your beautify() function, convert the string vector to an int vector. See How do I convert vector of strings into vector of integers in C++?

Community
  • 1
  • 1
moffeltje
  • 4,521
  • 4
  • 33
  • 57
0

Correct your beautify function

vector<int> beautify(vector<string> numbers)
{
    vector<int> result;
    for (auto & i : numbers)
        result.push_back(std::stoi(i));
    // If results are to be sorted
    std::sort(result.begin(), result.end());
    return result;
}

Also, the integers that you push in form of strings are out of range.

test1.push_back("3167389213");    // outside the range of int
test1.push_back("32989741893");

See http://ideone.com/vOeMHi demo

Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
  • @ Shreevardhan May I know what does "for (auto & i : numbers) result.push_back(std::stoi(i));" do? – Cj Medina Jul 01 '15 at 09:38
  • @CjMedina convert each element of vector to integer and push it in the result vector. – Shreevardhan Jul 01 '15 at 09:41
  • @CjMedina Yes, the result vector is then passed to `print_out` function. Your code's now working, right ? – Shreevardhan Jul 01 '15 at 09:48
  • @ Shreevardhan another question if I may, how can I return the result vector values in ascending order? – Cj Medina Jul 01 '15 at 09:59
  • @CjMedina add this `std::sort(result.begin(), result.end());` – Shreevardhan Jul 01 '15 at 10:00
  • yes there was no error though it did not print out the values passed by result I'm not sure if it was passed in printout() for it suppose to print out all the int vectors – Cj Medina Jul 01 '15 at 10:02
  • @ Shreevardhan where should I put std::sort(result.begin(), result.end()); exactly? – Cj Medina Jul 01 '15 at 10:03
  • @CjMedina it will print once you push integers in range of `int` – Shreevardhan Jul 01 '15 at 10:07
  • void print_out(vector numbers) { for( auto i: numbers ) cout << i << endl; } vector beautify(vector numbers) { vector result; for (auto & i : numbers){ result.push_back(std::stoi(i));} std::sort(result.begin(), result.end()); return result; } – Cj Medina Jul 01 '15 at 10:20