Arrays Printing Garbage Values

Jun 15, 2017 at 1:08am
Hi,
My program is supposed to print from the input file (My array size is 10). But it is printing some weird values. Can someone please explain what is going on with my code?
Thank you for your time!

Header File:
#include <iostream>
#include <fstream>
#include <string>

using std::ifstream;
using std::ofstream;
using std::istream;
using std::ostream;
using std::string;
using std::cout;
using std::cin;
using std::endl;

const int LAB=10;

void readI( string input, string output );

Main File:
# include "try.h"

int main()
{
readI( "input1.txt", "output1.txt");
}

Body:
#include "try.h"

void readI( string input, string output )
{
int Checking[LAB];
int count=0;
ifstream in(input);
ofstream out(output);

int numbers;
out<<"Index\t"<<"Value"<<endl;
in>>numbers;

while (!in.fail())
{
count++;
for (int i=0; i<10; i++)
{
out<<i<<"\t"<<Checking[i]<<endl;

}
in>>numbers;
}

}


My Input File:
4
-13
-12
-0
-7
3
-11
-11
-12
7
14
-0
-2
5
78
78
2
45
2
44
56
76
8
99
07
34
22
34
56
7
8
5
3
3
24
56
7
8
45
-7
87
89
67


The Output I am getting:

Index Value
0 1702232
1 9670912
2 9827828
3 10
4 1702276
5 1702264
6 9644761
7 9827828
8 -1761205977
9 1702276
0 1702232
1 9670912
2 9827828
3 10
4 1702276
5 1702264
6 9644761
7 9827828
8 -1761205977
9 1702276
0 1702232
1 9670912
2 9827828
3 10
4 1702276
5 1702264
6 9644761
7 9827828
8 -1761205977
9 1702276
0 1702232
1 9670912
2 9827828
3 10
4 1702276
5 1702264
6 9644761
7 9827828
8 -1761205977
9 1702276
0 1702232
1 9670912
2 9827828
3 10
4 1702276
5 1702264
6 9644761
7 9827828
8 -1761205977
9 1702276
0 1702232
1 9670912
2 9827828
3 10
4 1702276
5 1702264
6 9644761
7 9827828
8 -1761205977
9 1702276
0 1702232
1 9670912
2 9827828
3 10
4 1702276
5 1702264
6 9644761
7 9827828
8 -1761205977
9 1702276
0 1702232
1 9670912
2 9827828
3 10
4 1702276
5 1702264
Jun 15, 2017 at 3:15am
It looks like you're printing uninitialized variables.

Where do you ever assign any values to your array elements?

Please use code tags when posting code.

Jun 15, 2017 at 4:03am
Sorry, I will use the code tags. I am pretty new to this site.

I initialized Const int on my header file. Then I assigned numbers to the arrays. I could not find any other variables that need to be initialized. Could you please direct me to it? thanks!
Last edited on Jun 15, 2017 at 4:58am
Jun 15, 2017 at 8:15am
closed account (48T7M4Gy)
It might be a good idea to show us the question you have been set. If it is just a matter of copying the items in input file to the output file then you don't need arrays.
Jun 15, 2017 at 9:58am
Then I assigned numbers to the arrays.

Where did you assign values to your array?

Jun 15, 2017 at 2:40pm
@jib- I assigned it in my header file. const int LAB=10. Again, I'm new to programming. Please forgive me if I am not making any sense.

@Kemort- I need to use Arrays for this.
Jun 15, 2017 at 4:26pm
You use LAB to define the length of your array, so that it has 10 elements. But you don't actually assign any values to those elements.
Jun 16, 2017 at 5:44am
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/218025/
Jun 16, 2017 at 6:24am
It's pretty hard to understand what exactly is the problem but seems like you can get some help with it. Try to give some more information about the code, like, what exactly did you try to do?
If you need help detecting errors within you code, you can try using some programs, such as checkamrx or others in order to help you do that.
Good luck.
Ben.
Jun 16, 2017 at 9:51pm
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
#include <iostream>
#include <fstream>
#include <limits> 
#include <string>


constexpr int LAB = 10;
void readInput(std::string input, std::string output);
void waitForEnter();


int main()
{
    readInput("input1.txt", "output1.txt");
    waitForEnter();
    return 0;
}


void readInput(std::string input, std::string output)
{
    std::ofstream out(output);
    out << "Index\tValue\n";
    std::ifstream in(input);
    int index = 0;
    while(in)
    {
        int number;
        in >> number;
        out << index++ << "\t" << number << '\n';
    }
    in.close();
    out.close();
}


void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


input1.txt:
4
-13
-12
-0
-7
3
-11
-11
-12
7
14
-0
-2
5
78
78
2
45
2
44
56
76
8
99
07
34
22
34
56
7
8
5
3
3
24
56
7
8
45
-7
87
89
67


output1.txt:
Index	Value
0	4
1	-13
2	-12
3	0
4	-7
5	3
6	-11
7	-11
8	-12
9	7
10	14
11	0
12	-2
13	5
14	78
15	78
16	2
17	45
18	2
19	44
20	56
21	76
22	8
23	99
24	7
25	34
26	22
27	34
28	56
29	7
30	8
31	5
32	3
33	3
34	24
35	56
36	7
37	8
38	45
39	-7
40	87
41	89
42	67
43	67

Last edited on Jun 16, 2017 at 9:52pm
Jun 17, 2017 at 9:26am
closed account (48T7M4Gy)
Yep, who knows what the relevance of an array with 10 elements is?
Jun 17, 2017 at 12:49pm
Here is your code, indented and in one file:
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 <fstream>
#include <string>

using std::ifstream;
using std::ofstream;
using std::istream;
using std::ostream;
using std::string;
using std::cout;
using std::cin;
using std::endl;

const int LAB = 10;

void readI(string input, string output);

int
main()
{
    readI("input1.txt", "output1.txt");
}

void
readI(string input, string output)
{
    int Checking[LAB];
    int count = 0;
    ifstream in(input);
    ofstream out(output);

    int numbers;
    out << "Index\t" << "Value" << endl;
    in >> numbers;

    while (!in.fail()) {
	count++;
	for (int i = 0; i < 10; i++) {
	    out << i << "\t" << Checking[i] << endl;

	}
	in >> numbers;
    }
}

At lines 34 & 42, you read a number into numbers, but never actually do anything with it. You don't store it anywhere for later printing. At lines 38-41 you print the Checking array, but you never actually store any values into the array.

The count variable tells you how many numbers you've stored. If you want to fill the Checking array, then you can read a number directly into Checking[count]:
1
2
3
4
5
6
7
8
9
10
    in >> Checking[count];

    while (!in.fail()) {
        count++;
        for (int i = 0; i < 10; i++) {
            out << i << "\t" << Checking[i] << endl;

        }
        in >> Checking[count];
    }

That's closer, but when I run it now I get:
$ ./foo
Segmentation fault (core dumped)

The problem now is that you've allocated room in Checking for 10 numbers, but input1.txt contains 85. As a result, the program happily stores numbers in whatever member comes after the Checking array, and it causes the program to crash. As an aside, this is one of the reasons why it's usually preferable to use C++ vectors instead of arrays. Back to the problem, never fear, we can just change LAB at line 14 to, say, 100 so it's big enough.
const int LAB = 100;
Now the output still isn't right, but it's closer. Below are the first few lines of output1.txt. The underlined entries are correct:
Index   Value
0       4
1       0
2       -2147196998
3       1
4       -2145400288
5       1
6       -13400
7       0
8       -2145631872
9       1
0       4
1       -13
2       -2147196998
3       1
4       -2145400288
5       1
6       -13400
7       0
8       -2145631872
9       1
0       4
1       -13
2       -12
3       1
4       -2145400288
5       1
6       -13400
7       0
8       -2145631872
9       1
0       4
1       -13
2       -12
3       0
4       -2145400288
5       1
6       -13400
7       0
8       -2145631872
9       1

Why is the output file so big? Because you're printing the Checking array inside the input. So your code basically does this:
1
2
3
4
5
6
read one number
print all 10 values in Checking
read another number
print all 10 values in Checking
read another number
...

You need to move the loop that prints the array outside of the input loop:
1
2
3
4
5
6
7
    while (!in.fail()) {
        count++;
        in >> Checking[count];
    }
    for (int i = 0; i < 10; i++) {
        out << i << "\t" << Checking[i] << endl;
    }


Now when I run it, output1.txt contains:
Index   Value
0       4
1       -13
2       -12
3       0
4       -7
5       3
6       -11
7       -11
8       -12
9       7

Dang.... Where are the other numbers?? Looking at the code, I see that you've hard-coded 10 at the ending limit when you print:
for (int i = 0; i < 10; i++) {
So how many numbers should you print? Not the whole array: you might not fill the whole array. You want to print only the numbers that are in it, and that is handily stored in count:
for (int i = 0; i < count; i++) {
Putting it all together, here is the code:
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
#include <iostream>
#include <fstream>
#include <string>

using std::ifstream;
using std::ofstream;
using std::istream;
using std::ostream;
using std::string;
using std::cout;
using std::cin;
using std::endl;

const int LAB = 100;

void readI(string input, string output);

int
main()
{
    readI("input1.txt", "output1.txt");
}

void
readI(string input, string output)
{
    int Checking[LAB];
    int count = 0;
    ifstream in(input);
    ofstream out(output);

    int numbers;
    out << "Index\t" << "Value" << endl;
    in >> Checking[count];

    while (!in.fail()) {
	count++;
	in >> Checking[count];
    }
    for (int i = 0; i < count; i++) {
	out << i << "\t" << Checking[i] << endl;
    }
}



Jun 17, 2017 at 5:23pm
@ Mikeboy- It was for one of my homework problems. This site is allowed for our class before you say something! I showed this post to my professor and he said the main target of this lab is to print the garbage or print the max size of the Array.So, I did not need to assign any values. Thanks!

@dhayden -- Thanks a lot. That was very helpful. Couldn't reply to the post because I was stuck doing my project.

@benhart- Yeah I did get some help with it. I had ask my professor and managed to find a solution. Thanks! We use powershell and it is really hard to find a solution sometimes.Thanks!

@Enoizat - Thanks a lot!. I was able to find a solution. Your post was very helpful.

@kemort- not sure. Sorry.

Jun 18, 2017 at 6:45am
closed account (48T7M4Gy)
@kemort- not sure. Sorry.

@OP That's OK despite the strange requirement. that's why seeing the question would have been useful. But that's your call.

Another alternative is to use dynamic arrays rather than hard coding the value for LAB. All you do is count the number of entries, declare the dynamic array then go on to read in the values and write them to the output file simultaneously or after the array has been filled. Again your call.
Topic archived. No new replies allowed.