'cout' not working in reading function ??

Ok, here's reading function:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>

using namespace std;
 void skaitymas (int i, int n, int lytis[], int koja[], int dydis[]) {
    ifstream failas("a.txt");
    failas >> n;
cout << n;
    for (i=0; i<n; i++)
    failas >> lytis[i] >> koja[i] >> dydis[i];
cout<<1;
    failas.close();} 


I simply wanted to check the values, but for some reason cout<<n gave me nothing... Then I simply tried cout<<1 to make sure it was the problem with the file itself, and guess what, to my surprise that didn't work either... When such a obvious things aren't working it's just pain in the ... trying to figure it out by myself.

Thanks.
Last edited on
You're missing cout << endl;

You need to flush each line you write. Otherwise the data just stays in the cout buffer.



Tried with endl (cout << "1" << endl;), but it still can't give me output on the screen...

process returned 0

It's like compiler is completely ignoring it.
Last edited on
My first guess would be that this line is blocking:
failas >> lytis[i] >> koja[i] >> dydis[i];
Perhaps you could break this line up and print after you read each term to see how far into your file you're getting.
1
2
3
4
5
6
7
8
9
10
11
for (i=0; i<n; i++)
{
    failas >> lytis[i];
    cout << lytis[i] << endl;

    failas >> koja[i];
    cout << koja[i] << endl;

    failas >> dydis[i];
    cout << dydis[i] << endl;
}


EDIT: Oops! I skipped over the part where you mentioned this in your post.
process returned 0

Still, it may be helpful to put those prints in to track your program through the loop and see why you're not seeing a print on the other side.

EDIT 2: Also be sure you're not dealing with this: http://cplusplus.com/forum/beginner/1988/
Last edited on
Other codes with cout/cin are working just fine, so it can't be compiler problem...
Tried your code but for it's still ignoring cout...

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

using namespace std;
 void skaitymas (int i, int n, int lytis[], int koja[], int dydis[]);
int main()

{
void skaitymas (int i, int n, int lytis[], int koja[], int dydis[]);
return 0;
}

void skaitymas (int i, int n, int lytis[], int koja[], int dydis[]) {
    ifstream failas("a.txt");
    failas >> n;
    
cout << "1" << endl;

    for (i=0; i<n; i++)
{
    failas >> lytis[i];
    cout << lytis[i] << endl;

    failas >> koja[i];
    cout << koja[i] << endl;

    failas >> dydis[i];
    cout << dydis[i] << endl;
}
}
Last edited on
Try to call the function.


> Perhaps you could break this line up and print after you read each term
> to see how far into your file you're getting.
There are better ways. By instance, using a step-by-step run through a debugger.
There are better ways. By instance, using a step-by-step run through a debugger.

Sure. If (s)he has one and knows how to use it. I thought that was presumptuous given the nature of the problem we're discussing.

@SmOgER
What is meant by 'call the function' is fix line 10 of the program you posted. What you have is function declaration syntax. Notice all the types (ints and void) that you have there.
This:
void skaitymas (int i, int n, int lytis[], int koja[], int dydis[]);
should look more like this:
skaitymas ( i, n, lytis, koja, dydis);

The next problem you will run into is that none of those parameters have been declared yet. main() doesn't know what any of those are. You will have to declare those variables in main() in order to pass them in to your skaitymas function.
Last edited on
> Sure. If (s)he has one and knows how to use it.
┬┐why wouldn't have one?
Besides, it is not so hard to use, and certainly beneficial.


> should look more like this: skaitymas ( i, n, lytis[], koja[], dydis[]);
Nope, that's wrong. `lytis[]' is illegal.
Nope, that's wrong. `lytis[]' is illegal.

Good catch. I fixed this in my post.

why wouldn't have one?
Besides, it is not so hard to use, and certainly beneficial.

Sometimes people who are just beginning to write code learn to do so by typing into a text editor and invoking the compiler directly. You can assume that the poster has at least a text editor and compiler. I agree that learning to use a debugger is important and helpful, and if this poster has a debugger and the skill to use it, then (s)he should use it.
Last edited on

booradley60:

What is meant by 'call the function' is fix line 10 of the program you posted. What you have is function declaration syntax. Notice all the types (ints and void) that you have there.
This:
void skaitymas (int i, int n, int lytis[], int koja[], int dydis[]);
should look more like this:
skaitymas ( i, n, lytis, koja, dydis);


That's what I tried to do in the first place, but I'am kinda puzzled why I'am getting declaration errors with this code:
1
2
3
4
5
6
7
8
9
10
11
using namespace std;
 void skaitymas (int i, int n, int lytis[], int koja[], int dydis[]);
int main()

{
 skaitymas (i, n, lytis, koja, dydis);  // declaration errors, even though it was declared 
 return 0;
}

void skaitymas (int i, int n, int lytis[], int koja[], int dydis[]) {
    ifstream failas("a.txt"); 


Any ideas?
Last edited on
You didn't have variables i, n, etc. in your main function.
More to that: you are never using passed i and n in your function. Better to declare them as local inside function.
Try 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
#include <iostream>
#include <fstream>
using namespace std;

void skaitymas (int lytis[], int koja[], int dydis[]);
int main()
{
    const int count = 10;
    int lytis[count];
    int koja[count];
    int dydis[count];
    skaitymas (lytis,  koja, dydis);
    return 0;
}

void skaitymas (int lytis[], int koja[], int dydis[])
{
    ifstream failas("a.txt");
    int n;
    failas >> n;
    cout << n;
    for (int i=0; i<n; i++)
        failas >> lytis[i] >> koja[i] >> dydis[i];
    cout<<1;
    failas.close();
}

Thanks, it's working! sort of...

But I don't quite get it..
1
2
3
4
const int count = 10;
    int lytis[count];
    int koja[count];
    int dydis[count];


Why do I need to give variables const number of arrays??


EDIT:
It read n value well, But arrays are messed up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void skaitymas (int lytis[], int koja[], int dydis[]);
int main()
{
    const int count = 10;
    int lytis[count];
    int koja[count];
    int dydis[count];
    skaitymas (lytis,  koja, dydis);
    return 0;
}

void skaitymas (int lytis[], int koja[], int dydis[])
{
    ifstream failas("a.txt");
    int n;
    failas >> n;
    cout << n;
    for (int i=0; i<n; i++)
        failas >> lytis[i] >> koja[i] >> dydis[i];
    cout << lytis[i] << endl << koja [i] << endl << dydis[i] << endl; //  name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]|
    failas.close();
}



I think you gave me not the chunk of code which I'am looking for...
Last edited on
Why do I need to give variables const number of arrays??

Arrays in C++ have fixed capacity which should be known in compile-time. So you should either use const variable, or write capacity directly in definition: int lytis[10];

second error:
1
2
3
4
    for (int i=0; i<n; i++) {
        failas >> lytis[i] >> koja[i] >> dydis[i];
        cout << lytis[i] << endl << koja [i] << endl << dydis[i] << endl; /
    }
Aghhh...
Thanks a million!
Topic archived. No new replies allowed.