-2

I was working on data structures with C++. Everything looks OK. This is a simple C++ file read. I think this code's output should be:

1
K
3
4
5

But I'm seeing:

1
2
3
4
5

How can I take data[4] in if?

This is file.txt

A(1#Jordan)
A(2#Kyrie)
A(3#Lebron)
A(4#Harden)
A(5#Doncic)

This is my code

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){

fstream file;
file.open("file.txt", ios::in);

if(file.is_open()){
    while(!file.eof())
    {
        char data[20];
        file >> data;
        
        if(2 == data[2])
            cout << data[4]<< endl; //**
        else 
            cout << data[2] << endl;
    }
}

file.close();
return 0;                                                                     
}
JaMiT
  • 14,422
  • 4
  • 15
  • 31
Sezkente
  • 3
  • 2
  • 2
    You read ***characters*** into the array `data`. I don't know any character-encoding where `2 == '2'`. – Some programmer dude Nov 06 '21 at 10:16
  • Also please read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Some programmer dude Nov 06 '21 at 10:18
  • To extend on Some programmer dude's comment - there are NO (zero, nix) standardised data sets for which `2 == '2'`. Even if there was such a character set, all C++ (and C) standards require an implementation to have the characters `'0'` to `'9'` as a contiguous set (i.e. `'1' == '0' + 1`, `'2' == '1' + 1`, etc) AND that `'0'` cannot have a numeric value of zero – Peter Nov 06 '21 at 10:39
  • Please review [ask]. Your question title is too vague. – JaMiT Nov 06 '21 at 13:18
  • Does this answer your question? [Do-while loop wont exit c++](https://stackoverflow.com/questions/48645315/do-while-loop-wont-exit-c) – JaMiT Nov 06 '21 at 13:22
  • This doesn't address the question, but get in the habit of initializing objects with meaningful values rather than default-initializing them and immediately overwriting the default values. In this case, that means changing `fstream file; file.open("file.txt", ios::in);` to `fstream file("file.txt", ios::in);`. Also, using `ifstream` instead of `fstream` makes things simpler; `ifstream file("file.txt");` is all you need, since `ifstream` is for input. And, finally, you don't need to call `file.close();`; the destructor will do that. – Pete Becker Nov 06 '21 at 14:00

1 Answers1

0

there is a small error in how you compare a char to an int; the correct comparison is using '2':

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){

fstream file;
file.open("file.txt", ios::in);



if(file.is_open()){
    while(!file.eof())
    {
        char data[20];
        file >> data;
        
        if('2' == data[2])
            cout << data[4]<< endl; //**
        else 
            cout << data[2] << endl;
    }
}

file.close();
return 0;                                                                     
}
cDc
  • 317
  • 3
  • 9