Question:
C++ question: converting a string to array of integers?
Edik
2009-02-11 12:57:39 UTC
I'm a C++ newbie, so please be gentle... :-)

I have a text file which looks something like this:
0 4 10 3 7 9 2
1 7 9 11 4 0 3
etc etc etc (a series of integers on each line)

What I want to do is to take the file, and use ifstream to have my program read it, and subtract the numbers from each other so that the display is the difference between the 2nd and 1st numbers, 3rd and 2nd, 4th and 3rd, etc.
(the first line should end up as 4 6 -7 4 2 -7)

I assume that each line of my text file will be understood as a string when it is read, yes? So, I need to convert my string into a series of integers.

So, how do I convert a string (which is made up of integers separated by spaces) into a series of integers?

Thanks for any help!!
Three answers:
cja
2009-02-11 14:09:52 UTC
With a problem like this you have to be careful about memory management. In general, you won't know the size or contents of your input file beforehand. For this exercise, the length of each line in the file, and the number of numbers each line contains, are the key variables. Luckily, C++ provides some good features, string and vector in particular for this problem, to help you. Also, there is stringstream extraction to make converting text representation of numbers into integers. If you're not familiar with some of that stuff, you should look it up. I assure you that an implementation such as I've done (see below) will be much easier than using simple arrays. This is not to say that you shouldn't learn how to use arrays, and manage memory yourself. I'm just showing you what I think is a good way to do it using what C++ provides.



#include

#include

#include

#include



using namespace std;



int main(int argc, char *argv[]) {

    string file,line;

    int n;

    vector intVec;

    vector::iterator i;



    cout << "Enter name of file containing numbers: ";

    getline(cin,file);

    ifstream myfile(file.c_str());

    if (myfile.is_open()) {

        while (myfile) {

            getline(myfile,line);

            stringstream ss(line);

            while (ss.good()) {

                if (ss >> n) {

                    intVec.push_back(n);

                }

            }

            i = intVec.begin();

            while (i != intVec.end()) {

                n = *i;

                if (++i != intVec.end()) {

                    cout << *i - n << " ";

                }

            }

            if (intVec.size() > 1) cout << endl;

            intVec.clear();

        }

        myfile.close();

    } else {

        cout << "error opening " << file << endl;

    }

    return(0);

}



Example:



Enter name of file containing numbers: nums2.txt

4 6 -7 4 2 -7

6 2 2 -7 -4 3

6

11 45



Where nums2.txt contains :



$ cat nums2.txt

0 4 10 3 7 9 2

1 7 9 11 4 0 3



0

4 10

4      15       60
?
2016-10-17 05:12:31 UTC
The arithemetic way is to apply the residences of the location fee form device. b = one thousand*a million + one hundred*8 + 3*10 + 2 So, only use the dimensions of the array and 'opposite' the index, multiplying the kth place by using 10^ok permit int len = the dimensions of array a then int b = 0; for( int ok=a million; ok<=len; ok++ ) { ...b += a[len-ok]*pow(10,ok-a million); ...} +upload Joelkatz's technique is sensible yet, to me, sensible is way less significant than sparkling and ordinary to appreciate.
[ J ] a [ Y ]
2009-02-11 13:35:08 UTC
#include



using namespace std;



int main()

{

ifstream fin("myfile.txt");



int myarray[number of items];

int i = 0;



while(!fin.eof() )

{

fin >> myarray[i];

i++;

}



return 0;

}



that should work, but you might need to seperate your numbers by comma's instead of spaces.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...