guys..I would like to ask for some help. Im trying to read 10 integers in a text file and store it in a array. the problem is when I display it the numbers are treated one by one instead of putting them all together. For example when I display array[0] which is suppose to be 100 the outputs are:

a[0] =1
a[1] =0
a[2] =0

get my point?
I would like to ask for some advice on how to do it correctly. heres a piece of my code:

char ch;
int size=10;
int myArray[size];
ifstream myfile;
myfile.open("numbers.txt");
while(!myfile.eof())
                            {
                                 myfile.get(ch);
                                 for(int i=0; i<10; i++)
                                                    {
                                                        myArray[i]=ch;
                                                        cout<<myArray[i]<<" ";


                                                              }
                             }

the output should be:
100 0 -3 77 55 2 -10 4 67 34
but the output I have is:
1 0 0 0 -3 7 7 5 5 and so on.....

thanks guys

Recommended Answers

All 8 Replies

Member Avatar for iamthwee

I would read in the file as ints as opposed to chars.

I would read in the file as ints as opposed to chars.

it also doesn't work. I've tried it but my outputs are some strange symbols nothing else..

it also doesn't work. I've tried it but my outputs are some strange symbols nothing else..

Ok this is just theory but--

Why not read every value (separated by spaces) as a string then sstream them into ints and store that sstream'd value into an indice of the array?

it also doesn't work. I've tried it but my outputs are some strange symbols nothing else..

Show us your file. How are the numbers separated? I would use the >> operator, not the get() method.

Also, on further review, I seem some weird things in your loop. First, you read a char from the file (using the code in the OP). Then, you enter a while loop that assigns elements 1-10 of your array to that char, so if the first char you read was '1', the entire array would be filled with 1's. Then, you take the next char in your file, and assign that char to every element in your array, and so on until the file finishes. During all of this, you'll be outputting each char in the file 10 times.

This is a bit weird though, since the output you gave isn't consistent with this prediction. Am I not seeing something, or is something else wrong?

Ok this is just theory but--

Why not read every value (separated by spaces) as a string then sstream them into ints and store that sstream'd value into an indice of the array?

could i ask some idea on how to do that please..?;) im just new to C++ and not very much familiar with the language

Show us your file. How are the numbers separated? I would use the >> operator, not the get() method.

Also, on further review, I seem some weird things in your loop. First, you read a char from the file (using the code in the OP). Then, you enter a while loop that assigns elements 1-10 of your array to that char, so if the first char you read was '1', the entire array would be filled with 1's. Then, you take the next char in your file, and assign that char to every element in your array, and so on until the file finishes. During all of this, you'll be outputting each char in the file 10 times.

This is a bit weird though, since the output you gave isn't consistent with this prediction. Am I not seeing something, or is something else wrong?

well..honestly I really don't know if my code is correct..thats why I'm asking for some advice on how to do it correctly.

the numbers in the text file are separated by a space and thats how it is supposed to look when I execute the program.

Combine iamthwee's and CoolGamer48's suggestions. Read in integers from the file and use the >> operator to do it. I think you have to use the >> operator anyway to read in data as an integer (can't think of any other way) so I guess that's redundant advice. That should work and that's the easiest way. If it doesn't work, post the program and the input file.

well..honestly I really don't know if my code is correct..thats why I'm asking for some advice on how to do it correctly.

the numbers in the text file are separated by a space and thats how it is supposed to look when I execute the program.

I would condense/change the program to this:

int myArray[10];
ifstream myfile("numbers.txt");
for(int i = 0; i < 10;i++)
{
    myfile >> myArray[i];
    cout << myArray[i];
}

This program assumes that there are 10 integers in the file separated by spaces or newlines (or some form of whitespace), which, from what you said, there are.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.