pushing/copying/placing a string into an integer array

Hi, I know there are a million different answers on how to do them and I've tried them all but they don't seem to be working. Basically, what I need help with is that I have a file with two lines and in both lines there are multiple integers with white spaces. I want to read one line into one array called arr[] and the second line into another array called arr2[]. I tried doing that directly but I don't know how to code it so that the loop knows to stop copying the file numbers in the array once it reaches the newline. As a result, I've managed to copy the first line into a string and want to now copy that into an integer array, or arr[]. Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//This is the h file:

#ifndef //
#define //
#include <string>
using namespace std;
typedef int Item;
class MyFunction {
private:
   static const int maxsize = 100;
    Item arr[maxsize];
    Item arr2[maxsize];

public:
    void sortArrays();
    void setArrays();
};

#endif //
//This is the .cpp file:
#include "MyFunction.h"
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
void MyFunction::sortArrays() {
    ifstream infile;
    string temp;
    infile.open("MyInput.txt");
    int file;
    int size = 50;
    int i = 0;
    arr[i] = {0};


   if(infile.is_open())
   {
       getline(infile,temp);
       cout << temp;
   }
    cout << endl;
    for(int i=0;i<temp.length();i++)
    {
       arr[i] = temp[i] - '0'; 

// this is where the copy/push should be happening from the temp string into the array but it's not working

       cout << arr[i];
    }
    infile.close();

}

  Put the code you need help with here.


As of now, the output I get is the string output which is all the correct numbers from my file and then the array output which is just a stream of random numbers.
Last edited on
line 42 you define an int i, but i already exists, you defined it on line 32.

line 44 arr[i] = temp[i] - '0'; if temp is a string, what are you trying to do when you substract the character '0' to a char? The decimal value for '0' is constant and is equal to 48. arr[i] is then equal to the decimal value of the char at the position i in your temp string, minus 48.

So your arr array is basically going to be an array of random numbers as you mentioned.

Can you post your full code so that we can see more of what's happening outside of this class?

Edit: Actually I'm not sure I understand what you're trying to do. Are you basically reading two strings from a file, extracting the ints from the strings and placing them into their respective arrays of ints?
Last edited on
To read the ints from one line, first read the line into a string then use an istringstream on the line to read the ints until eof.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

void read_ints(std::istream& in, std::vector<int>& v) {
    std::string line;
    std::getline(in, line);
    std::istringstream iss(line);
    int n;
    while (iss >> n)
       v.push_back(n);
}

int main() {
    std::vector<int> a, b;
    read_ints(std::cin, a);
    read_ints(std::cin, b);
    for (int n: a) std::cout << n << ' '; std::cout << '\n';
    for (int n: b) std::cout << n << ' '; std::cout << '\n';
}

Last edited on
Hello az1234,

Both H00G0 and dutch make good points.

When I tried to compile the code the first thing I noticed is:
1
2
#ifndef //
#define // 

Without something to follow the "#ifndef" all the code that follows is not part of the program. It is like putting a comment on each line.

I think you should find this better:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef MYFUNCTIONS_h
#define MYFUNCTIONS_h

typedef int Item;

class MyFunction
{
private:
	static const int MAXSIZE = 100;
	Item arr[MAXSIZE];
	Item arr2[MAXSIZE];

public:
	void sortArrays();
	void setArrays();
};

#endif // !end MYFUNCTIONS_H 

When you define a variable as a constant it is customary to use capital letters for the variable name.

Your header file includes the header file "string", but there is nothing in that file that needs the "string" header. By putting "#include <string>" in the header you could include this in a ".cpp" file that does not need it.

It has been said the the order of the "#include" files should make no difference, but sometimes it does.

For what you have I would do this in the "MyFunctions.cpp file:
1
2
3
4
5
6
7
8
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

#include "My Functions.h"

//using namespace std;  // <--- Best not to use. 

By keeping an order something like this I find it helps to remember what you need or what you have left out. And I have found that any include file that is in quotes is best put last and most of the time the include files that precede it will cover anything in that header file that might need them.

When it comes to "using namespace std;" you should not put this in a header file. That is asking for trouble. Give this a read http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Actually it is best not to use "using namespace std;" in any file. What you are missing out on now is learning what is in the standard name space slowly. Then someday you will have to learn everything at one time.

The function that you have says "sortArrays()", but the code is to read a file not sort. Consider changing the name of this function.

Your "MyFunctions.cpp" has "#include <sstream>", but you never use it.

dutch's code can easily be changed to use arrays, if that is what you need, instead of vectors. I would help to know what you can or have to use for this program.

As H00G0 said the rest of your code would be helpful. Also the input file that you are using would be a big help so that everyone is using the same information and do not have to guess at what needs to be done.

Hope that helps,

Andy
H00G0,

I fixed the i=0 part to only have in my code once, thanks for catching that. As for the rest of the code, I'm a bit unsure of what you mean; the only thing other than these two files is the main.cpp in which I call this function and the actual input file. The input file is:

3  4  8  7  3 16 5 12 11 14  1  9  9  8  1 12
16  4  2  6 4 12  9 12 7  6 19  9 11 12  8  5 -4  -250

The subtracting 0 part I put in because in the other forums I looked at that had similar programs, they were using that and that seemed to work. I'm not sure how to copy the string into the array otherwise.

As for what I'm trying to do, I'm trying to put the first line from the input file into arr[] and the second line from the input file into another array, which I have not done so yet. So far, all I'm trying to do is put the first line from the input line into arr[], whether that be directly from the file or by putting it into a string first and then transferring it.

dutch,

How exactly do I do that with an array though? You can't push back into an array, right?

Handy Andy,

I've been erasing and rewriting this program throughout the day which is why there may be so many errors, sorry and thanks for catching those. I actually did have the correct headers in the #ifndef, #define, and #endif, I think I erased it when I was putting it onto this forum. As for the rest of the code:

main.cpp:

1
2
3
4
5
6
7
#include "SetFunctions.h"

int main() {
    SetFunctions call;
    call.sortArrays();
    return 0;
}


input file:

3  4  8  7  3 16 5 12 11 14  1  9  9  8  1 12
16  4  2  6 4 12  9 12 7  6 19  9 11 12  8  5 -4  -250
Last edited on
Hello az1234,

I agree with dutch a vector would be a better choice especially since each line of the input file is a different length, but you can still use an array. The size of the array will have to be the line with the most numbers.

Yes you are right you do not use "push_back" on an array.

My names may be a bit different, but this does work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <chrono>
#include <thread>

#include "My Functions.h"

//using namespace std;  // <--- Best not to use.
//constexpr int MAXSIZE{ 18 };

void MyFunction::ReadFile()
{
	std::ifstream inFile("MyInput.txt");
	std::string temp;
	int index{ 0 };

	if (!inFile)
	{
		std::cout << "\n File " << "MyInput.txt" << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		/*return 1;*/  exit(1);  // If not in "main". If did not open no need to continue.
	}

	getline(inFile, temp);
	std::istringstream ss(temp);

	while (ss >> arr[index++]);

	std::cout << temp << std::endl;

	index = 0;
	ss.clear();

	getline(inFile, temp);
	ss.str(temp);

	while (ss >> arr2[index++]);

	std::cout << temp << std::endl;

	inFile.close();
}

All I changed in the header file is the value for "MAXSIZE" to make it big enough to cover the input file.

Hope that helps,

Andy
@az1234, I was showing you the general idea. I assumed you would either switch to vectors or at least be able to figure out how to do it with arrays yourself. I guess I assumed too much.

@Andy, He should probably save the actual number of elements that he stored in the arrays. Otherwise he would have no way to determine it later. And it's good to protect from overflow, too.

1
2
3
4
5
int i = 0
for (int n; i < MaxSize && ss >> n; )
    arr1[i++] = n;
size1 = i;   // add size1 and size2 to the class
...

Last edited on
The subtracting 0 part I put in because in the other forums I looked at that had similar programs, they were using that and that seemed to work. I'm not sure how to copy the string into the array otherwise.

It's always interesting to be able to have a look at other people's codes on the same problem as you have. However I think it's a very bad practice and habit to get into, to simply copy and paste other's codes to make your program work. Sure it's handy but it can cost you a lot of time and energy, as you may have noticed.

It's ok to look at peoples codes, and I find it amazing sometimes to see how people can solve the same problem as you with a completely different approach. But for your own sake, be sure to understand fully what you're using, It may take some researching and digging, but in the long term I think it's a great investment.
Topic archived. No new replies allowed.