0

I have this code currently in which the user would enter a number for how many numbers they want in the array. followed by '12345' however about a second after writing it i realized this would only work if they entered number 0-9 anything in double figures or more wouldnt work.

int numberOfValues;
cout << "Please enter the amount of integers you want in the array" << endl;
cin >> numberOfValues;

int valuesArray[numberOfValues];
string valuesString;

cout << "Please Enter " << numberOfValues << " numbers" << endl;
cin>>valuesString;


for(int i = 0; i < numberOfValues; i++)
{
    valuesArray[i] = valuesString[i];
}
return valuesArray;

im thinking that the best way to do this would be for the user to enter numbers separated by a comma and to split them afterwards (iv done this same little porgram in java and trying to change it to C++ for my own personal learning) like in java i used string.split(",") i was wondering if there is anything similar in c++??

4
  • stackoverflow.com/questions/53849/…
    – Reunanen
    Commented Feb 26, 2013 at 12:41
  • I think boost::split would be the first thing to look at if you are willing to use a library. This question demonstrates that, but also many other solutions: stackoverflow.com/questions/236129/splitting-a-string-in-c
    – user1919238
    Commented Feb 26, 2013 at 12:41
  • thanks but dont want to use external libs it more for myself to learn how some core stuff is done instead of just using a lib to do it for me cheers anyway ill look at some of the other ways to do it, but im looking for a nice simple way to do it iv seen some of these are like 80 lines of code to just split a string that cant be right .. right??
    – AngryDuck
    Commented Feb 26, 2013 at 12:44
  • This answer uses just the C++ Standard Library: stackoverflow.com/a/236803/78845
    – johnsyweb
    Commented Feb 26, 2013 at 12:50

5 Answers 5

4

The simplest way I can think of would be to avoid reading to an intermediate string and let cin do the work for you:

std::vector<int> valuesArray;

int i = 0;
do {
    cin >> i;
    valuesArray.push_back(i);
} while (valuesArray.size() < numberOfValues && cin.get() == ',');

/* edit: You may need to consume a '\n', if you expect one, too: */
do {
    i = cin.get();
} while (i != '\n');
5
  • hey this kinda of worked i returned the array and then printed some values randomly, first couple of values were as expected by then after valuesArray[2] they all equaled 0? regardless of the number that should be there instead any ideas?
    – AngryDuck
    Commented Feb 26, 2013 at 13:22
  • valuesArray is declared with automatic storage duration. When the function returns, valuesArray is destroyed. Hence, the caller will be using an array that has been destroyed. Watch this space; I'll edit to use a vector.
    – autistic
    Commented Feb 26, 2013 at 13:29
  • cheers illigve it a try now
    – AngryDuck
    Commented Feb 26, 2013 at 13:42
  • at the moment this code is running inside a method int read(){} however if im returning a vector<int> how do i declare the method so that this vector<int> is returnable and useable in a main method?
    – AngryDuck
    Commented Feb 26, 2013 at 13:44
  • WOOT finally got it working thanks again ill give u top answer
    – AngryDuck
    Commented Feb 26, 2013 at 13:49
4

Use strtok. Documentation and example can be found Here

1

use combination of string::substr() and string::find(). Find the next comma charater and then find the substring from current location to next command character

1

It is not standard C++ string, but still, Qt's QString class provides a ready-to-use method QString::split(...) with support for stuff like regular expressions, options for split behavior, case sensitivity and whatnot...

0

I wrote a tokenizer time ago, hope it works for you:

std::vector<std::string> tokenize(const std::string &_line,const char *_delimeter)
{
    std::vector<std::string> Tokens;

    if(_line.empty()) return Tokens;

    std::string str;

    BOOST_FOREACH(char c,_line){
        if(c==*_delimeter)
        {
            Tokens.push_back(str);
            str.clear();
            continue;
        }
        str += c;
    }

    if(!str.empty())
        Tokens.push_back(str);

    return Tokens;
}

it is not efficient, but works for testing purpose.

Not the answer you're looking for? Browse other questions tagged or ask your own question.