0

I would like to read my text file and output it exactly like in the input but the thing is, they are repeated! and the number will be either 1 or 0

My c++ code

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int n, count, size = 10;
string name, ID, stamina, plusmode, type;

int main ()
{
    ifstream data;
    data.open("cards2.txt");
        
        while (data)
        {
            data >> n;
            getline (data, name, '\t');
            getline (data, ID, '\t');
            getline (data, stamina, '\t');
            getline (data, plusmode, '\t');
            getline (data, type, '\t');
            
       for (count = 1; count <= 10; count++)
            {
              cout << n << " " << name << " " << ID << " " << stamina << " " << plusmode << " " << type << endl;  
      }
        }
}

My txt file

1   Abyss Devolos   F0647       Balance NA  SpeedStorm
2   Ace Dragon      E7609       Attack  NA  HyperSphere
3   Anubion A2      E1057       Defense NA  Dual-Layer
4   Balar B4        E4726       Attack  NA  SlingShock
5   Crystal Dranzer F0217       Balance NA  Burst
6   Cyclone Belfyre F3965       Stamina Attack  QuadDrive
7   Dark-X Nepstrius    E4749       Defense NA  SlingShock
8   Diomedes D2     E1062       Attack  NA  Dual-Layer
9   Doomscizor      E1033       Attack  NA  SwitchStrike
10  Vatryek Wing Accel  B9492   Attack  NA  Burst

My output in terminal

1  Abyss Devolos F0647  Balance
1  Abyss Devolos F0647  Balance
1  Abyss Devolos F0647  Balance
1  Abyss Devolos F0647  Balance
1  Abyss Devolos F0647  Balance
1  Abyss Devolos F0647  Balance
1  Abyss Devolos F0647  Balance
1  Abyss Devolos F0647  Balance
1  Abyss Devolos F0647  Balance
1  Abyss Devolos F0647  Balance
0  Abyss Devolos F0647  Balance
0  Abyss Devolos F0647  Balance
0  Abyss Devolos F0647  Balance
0  Abyss Devolos F0647  Balance
0  Abyss Devolos F0647  Balance
0  Abyss Devolos F0647  Balance
0  Abyss Devolos F0647  Balance
0  Abyss Devolos F0647  Balance
0  Abyss Devolos F0647  Balance
0  Abyss Devolos F0647  Balance

Please help me. I dont know how to make it not repeated

  • 2
    `for (count = 1; count <= 10; count++)` is a loop that repeats the output of every line you read from the file 10 times – 463035818_is_not_an_ai Jun 20 '22 at 13:26
  • 1
    The code does not check for when the `data` input fails. The code mixes extraction `>>` and `getline`, which probably will go poorly unless carefully handled (alas, the code doesn't handle that). – Eljay Jun 20 '22 at 13:28
  • The issue is very similar to https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons. You are doing things in the wrong order: check the state of the stream, read from the stream, use the input. Instead you need to check if reading from the stream suceeded and you can only do that after reading not before – 463035818_is_not_an_ai Jun 20 '22 at 13:31
  • 1
    First thing to do is remove the for loop. You still have bugs but perhaps it will be a little bit clearer then. – john Jun 20 '22 at 13:32
  • Your posted "My txt file" does not contain any tab characters, so it becomes hard for us to test (or check for errors in your input (or maybe it is completely invalid and has no tab characters)). My next question is `type` actually terminated by a tab character (or maybe should that be new line). You don't validate that any of the reads work. When you read from a file always check that the read actually worked. – Martin York Jun 20 '22 at 16:25
  • @john this is my code: while (data) { data >> n; getline (data, name); getline (data, ID); getline (data, stamina); getline (data, plusmode); getline (data, type); cout << n << " " << name << " " << ID << " " << stamina << " " << plusmode << " " << type << endl; } then what should i do? because the output is not what i want –  Jun 21 '22 at 07:16
  • @Amanda That code will not work with the data you have posted above, `getline(data,name)` will read the whole line intp name. You have to match the code to your input, you have to understand **precisely** how the different input functions work, what they read and what they don't read. – john Jun 21 '22 at 07:40
  • @Amanda Give me a few minutes and I'll update the answer with some code that reads you data – john Jun 21 '22 at 07:41
  • @Amanda Your data is tab delimited, is that right? – john Jun 21 '22 at 07:44

1 Answers1

0

Here's some code that reads and prints your data file. I am assuming that your data is tab delimited, which is what your initial code implies, but you haven't explicitly stated anywhere.

int main()
{
    ...
    int n;
    string name, ID, stamina, plusmode, type;
    while (data >> n) // continue reading until no more numbers
    {
        data.ignore(); // skip the tab immediately after the number
        getline(data, name, '\t');
        getline(data, ID, '\t');
        getline(data, stamina, '\t');
        getline(data, plusmode, '\t');
        getline(data, type, '\n'); // last data item is terminated by \n
        cout << n << " " << name << " " << ID << " " << stamina << " " << plusmode << " " << type << endl;
    }
}

Your original code made two mistakes (which I've indicated with comments).

The first error was that after reading the number in the first column, you failed to realise that the next character is a tab. You want to ignore that tab, so that you can start reading the next column. That's what data.ignore() does, it ignores the next character in the input.

The second error is that your code didn't reflect that the type column is not terminated by a tab but by a newline, so getline(data, type, '\t'); is wrong, it should be getline(data, type, '\n');

Both these errors are examples of where you haven't thought carefully enough about what the input is, and what the code you have written does. You need to be precise in your thinking.

john
  • 85,011
  • 4
  • 57
  • 81