Arrays with Filestream

This is what I am trying to accomplish

C++ HW - Files and Arrays
Create two 10-element arrays (1 is empty for now 1 is what the next line is)
Using a random number generator, fill one array with integers having a value between 1 and 100.
Print the contents of the array
Open a file for writing
Write the contents of the array to the file (the 1st rand array)
Open your file for reading (with the 1st rand array written to it)
As you read the file, write the double of each number to your second array (so basically 2x what the random numbers generated)
Print the contents of the array

I don't know how to get the 10 element array I generated with random to double.

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
#include <iostream>
#include <ctime>
#include <fstream>

using namespace std;
int main()
{
	ofstream outputFile;  
	ifstream inputFile;
	int Arrayinloop, array1, arrayout; 

	srand(time(NULL));

	const unsigned int arrsize = 10;
	int numberArray[arrsize];
	int array2[arrsize];

	for (int i = 0; i < arrsize; i++)

	{
		numberArray[i] = rand() % 100;
		cin >> numberArray[i] >> Arrayinloop;
		array1 += Arrayinloop;
		cout << numberArray[i] << "\t";
	}

	outputFile.open("ArrayOut.txt");
	outputFile << Arrayinloop << "\t" << endl; 
	outputFile.close();

	cout << "Array 1 written to ArrayOut.txt\n";

	inputFile.open("ArrayOut.txt");
	
	while (inputFile >> arrayout)
	{
		while (cout << arrayout << endl)
		{
			// I want to have the double of the first array printed here 
		}
	}
	

cin.get();
return 0;
}
Hello nickmcp11,

I have not fixed everything yet, but the comments should help for now:
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
55
56
#include <iostream>
#include <cstdlib>  // <--- For "rand()" and "srand()".
#include <ctime>

#include <fstream>

using namespace std;

int main()
{
    ofstream outputFile;
    ifstream inputFile;
    int Arrayinloop{}, array1{}, arrayout{};  // <--- ALWAYS initialize your variables.

    //srand(time(NULL));
    srand(static_cast<unsigned int>(time(nullptr)));

    const unsigned int arrsize = 10;  // <--- Name works better as all CAPS.
    int numberArray[arrsize]{};  // <--- ALWAYS initialize your variables.
    int array2[arrsize]{};       // <--- ALWAYS initialize your variables.

    for (int i = 0; i < arrsize; i++)
    {
        numberArray[i] = rand() % 100;  // <--- This crreates an "int" not a "double".

        cin >> numberArray[i] >> Arrayinloop;  // <--- Why are you inputing to the array that you just gave a random number to?

        array1 += Arrayinloop;

        cout << numberArray[i] << "\t";
    }

    outputFile.open("ArrayOut.txt");
    // <--- How do you know it is open.

    outputFile << Arrayinloop << "\t" << endl;
    outputFile.close();

    cout << "Array 1 written to ArrayOut.txt\n";

    inputFile.open("ArrayOut.txt");
    // <--- How do you know it is open.

    while (inputFile >> arrayout)
    {
        while (cout << arrayout << endl)  // <--- You will need to explain the while loop here. It is not needed.
        {
            // I want to have the double of the first array printed here

            // <--- You could type cast the output to a "double, but it would always print "something.0".
        }
    
    cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}

I will have some other suggestions shortly.

Andy
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
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>

int main()
{
	srand(static_cast<unsigned int>(std::time(nullptr)));

	const size_t arrsize {10};
	int numberArray[arrsize] {};
	std::ofstream outputFile("ArrayOut.txt");

	if (!outputFile)
		return (std::cout << "Cannot create output file\n"), 1;

	std::cout << "Array 1:\n";
	for (size_t i = 0; i < arrsize; ++i) {
		numberArray[i] = rand() % 100 + 1;
		std::cout << numberArray[i] << '\t';
		outputFile << numberArray[i] << ' ';
	}

	outputFile.close();

	std::ifstream inputFile("ArrayOut.txt");

	if (inputFile) {
		int array2[arrsize] {};

		for (int no = 0, i = 0; (i < arrsize) && (inputFile >> no); array2[i++] = no * 2);

		std::cout << "\nArray 2:\n";
		for (size_t i = 0; i < arrsize; ++i)
			std::cout << array2[i] << '\t';

		std::cout << '\n';
	} else
		std::cout << "Cannot open input file\n";
}

Hello nickmcp11,

Consider starting your program like this:
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
#include <iostream>
#include <iomanip>  // <--- Added.
#include <string>   // <--- Added.
#include <cstdlib>  // <--- For "rand()" and "srand()".
#include <ctime>

#include <fstream>

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

int main()
{
    std::string ioFileName{ "ArrayOut.txt" };  // <--- Put File name here.

    std::fstream ioFile(ioFileName);  // <--- Opens file for both input and output. Default settings.

    if (!ioFile)
    {
        std::cout << "\n File " << std::quoted(ioFileName) << " did not open" << std::endl;
        \\std::cout << "\n File \"" << ioFileName << "\" did not open." << std::endl;

        return 1;
    }

    const unsigned int ARRSIZE{ 10 };

    int Arrayinloop{}, array1{}, arrayout{};  // <--- ALWAYS initialize your variables.
    int numberArray[ARRSIZE]{};
    double array2[ARRSIZE]{};

    srand(static_cast<unsigned int>(time(nullptr)));

    std::cout << std::fixed << std::setprecision(2);

For what you are doing you do not need to create 2 file streams. 1 will work just fine.

I did this, but you could combine the contents of both into just 1 for loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    for (int i = 0; i < ARRSIZE; i++)
    {
        numberArray[i] = rand() % 100 + 1;  // <--- This crreates an "int" not a "double".
    }

    std::cout << "\n ";

    for (int idx = 0; idx < ARRSIZE; idx++)
    {
        std::cout << (idx ? ", " : "") << numberArray[idx];

        ioFile << numberArray[idx] << " ";  // <--- A space is all you need.
    }

    cout << "\n\n    Array 1 written to ArrayOut.txt\n";


For the while loop I have:
1
2
3
4
5
6
7
8
    int idx{};

    ioFile.seekg(0);

    while (ioFile >> arrayout)
    {
        array2[idx++] = arrayout;
    }

But you could just write: while (ioFile >> array2[idx]) idx++;. Would do the same thing.

That just 1 last for loop to print "array2".

A sample output would be:

 100, 29, 8, 64, 21, 74, 45, 43, 23, 6

    Array 1 written to ArrayOut.txt


 Contents of array 2
 100.00, 29.00, 8.00, 64.00, 21.00, 74.00, 45.00, 43.00, 23.00, 6.00


 Press Enter to continue:



Andy

Edit:
Last edited on
There is no requirement for array2 to be of type double. It holds double (* 2) the contents of the first array.
@seeplus,

There nothing that I read that says it can not be. So, I have a different interpretation of the instructions. They could be clearer.

Andy
fill one array with integers having a value between 1 and 100.
write the double of each number to your second array (so basically 2x what the random numbers generated)


All values are integers.
It finally hit me.

Andy
I can confirm he wants every number multiplied by 2 not double vs int
Topic archived. No new replies allowed.