-1

In this code I dont know why but if (temp == '\n') is not working, therefore in output there is all zeros and that zeros in ith index doesnt update

while(fin.eof() != 1)
{
    if(temp ==  '\n' ) 
    {
        k = 0;
        j = 0;
        i++;
        cout << "call from 2nd if";     
    }
    if(temp == ',')
    {
        k = 0;
        j++;
        cout << "call from 1st if";
    }
    fin >> temp; 
    data[i][j][k] = temp;
    
    cout << "address " << i << j << k << " : " << data[i][j][k] << endl;
    k++;
    i,j;
}

OUTPUT:

   address at **0**31 : u
   address at **0**32 : i
   address at **0**33 : c
   address at **0**34 : e
   address at **0**35 : B
   .
   .
   .

basically it is 3dimesnional array where i th value is not updating ,what is solution to this

Christophe
  • 68,716
  • 7
  • 72
  • 138
daud nasir
  • 41
  • 4
  • 1
    You check `temp` *before* you read into it. Possibly `temp` is uninitialized at that point, which means it will have an *indeterminate* value, which leads to *undefined behavior*. – Some programmer dude Oct 17 '21 at 08:25
  • 1
    Can you make a [mcve]? `>>` ignores whitespace by default, and `'\n'` counts as whitespace. – HolyBlackCat Oct 17 '21 at 08:26
  • 1
    Also what is the statement `i,j;` supposed to do? – Some programmer dude Oct 17 '21 at 08:26
  • 2
    And 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 Oct 17 '21 at 08:27
  • `while(fin.eof() != 1)` get rid of those C habits. If you want to compare a `bool` to something, use `true` or `false` or just leave out the comparison: `while (!fin.eof())` – fabian Oct 17 '21 at 08:29
  • @HolyBlackCat Can you tell me how can i put '\n' into my temp variable so that i could detect it for my loop – daud nasir Oct 17 '21 at 08:32
  • Perhaps you should use `std::getline` to read full lines into a `std::string`? – Some programmer dude Oct 17 '21 at 08:37
  • actually im doing my assignment and i cant use getline – daud nasir Oct 17 '21 at 08:41
  • Please copy-paste the *full* and *complete* assignment into the question, including all requirements and limitations. – Some programmer dude Oct 17 '21 at 09:07
  • Get into a habit of declaring **and initialising** variables right before you need them. If you have a declaration `char temp;` somewhere, remove it. Put it right before the first use. Now `char temp; if(temp == '\n' )` do you see a problem here? – n. m. could be an AI Oct 17 '21 at 09:24

2 Answers2

0

if(temp == '\n' ) replace with if(isspace(temp)).

Jitu DeRaps
  • 144
  • 1
  • 9
0

It is a very bad idea to loop on eof in C++. If your file is empty, fin.eof() will be false until you try to read something from it.

So as a first corrective measure change to:

while(fin >> temp)
{
    ....
} 

We then assume that temp is defined as char, because you read char by char.

The trouble is that >> tends to swallow a lot of spaces, including ' ' and '\n' that you'll never get. If you do expect to get some white, you need to set std::noskipws:

while(fin >> noskipws >> temp)
{
    ....
} 

But if you're reading char by char, a better approach could be to read with fin.get()

Christophe
  • 68,716
  • 7
  • 72
  • 138