for loop 4 times

Hello.
I'm trying to do a program that can read the information from a .txt, and each 4 numbers can be stored into a array. So far I have 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
34
{
[#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{

	
	int i = 0, j = 0, k = 0, l = 0;
	ifstream myfile;
	myfile.open("quarterlysalesfigures.txt");
	while (!myfile.eof())
	{
		for (i = 0; i < 4; i++);
		{
			for (j = 4; j < 8; j++);
			{
				for (k = 8; k < 12; k++);
				{
					for (l = 12; l < 16; l++);
					myfile >> i, j, k, l;
					cout << i, j, k, l;

				}
			}
		}
	}
	myfile.close();
	system("pause");
	return 1;
}


It show up the numbers when it is executed, but all 16 numbers are stored in each of i,j,k,l.
Last edited on
Code format tags work like this: [code] {your code here} [/code]. Please edit your post and fix it.

Why are you doing so many nested loops if you just need to read from a file?

Simply do something like:
1
2
3
4
5
int a, b, c, d;
while (myfile >> a >> b >> c >> d) // loops as long as it is able to fill the 4x numbers with valid information
{
    cout << "contents: " << a << b << c << d << '\n';
}
Last edited on
and how can I separate them from each other? When I execute them each value from a,b,c,d has 4 numbers together.
You mean you just want to add spaces?
cout << "contents: " << a << " " << b << " " << c << " " << d << '\n';
Last edited on
no, for example if I only put "a" it gives me 1234, when I put "a" and "b" it gives me 1234 5678, but how can I separate them to have 1 2 3 4 ?
Oh you want to split the digits of an individual int?

This is a good learning experience, my hint is that % 10 (modulus) will extract the right-most digit, and / 10 (integer division) will remove the right-most digit.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Example program
#include <iostream>

int main()
{
    using namespace std;
    
    int number = 123;
    int m = number % 10;
    int d = number / 10; 
    
    cout << m << '\n'; // 3
    cout << d << '\n'; // 12
    
    cout << d % 10 << '\n'; // 2
}

I would work on writing a separate function that prints the separated digits of an input number. (It will need to use a loop).

e.g.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

void print_digits(int n)
{
    // fill in here
}

int main()
{
    print_digits(1234);
}
Last edited on
Hello kwaifu,

This would be a good time to show what the input file looks like, so nice Mr. Ganad can stop guessing at what you want.

It is possible that line 24 may need to be myfile >> i >> j >> k >> l;, but without the input file there is no way to know what there is to read.

Andy
Hello kwaifu,

In your OP you said
each 4 numbers can be stored into a array.
So where is your array?

Andy
I was prepared to keep guessing for multiple more iterations :)
(I didn't actually notice at first that OP was using the commas incorrectly)

My guess is that the nested for loops are unnecessary, but of course I can't prove that.
Last edited on
@Ganado,

Did you notice that the first line is ({) and the second line starts with([)?
Yes, that's from incorrect code tag artifacts.
@Ganado,

Good point. I have never seen that before.

Online 12 the variables "i", "j", "k" and "l" are defined. Then used as the for loop counters. Does that not mean when you read something into the variables it changes how the for loop works?

Andy
Indeed, it's overwriting the loop counters, which is almost certainly wrong.
Last edited on
Topic archived. No new replies allowed.