hi guys, i dont know why when i run the code, its shows my txt is not found, here i attached the txt.file and the coding :)
i just follow the coding in the example that lecturer given, but yeah as i mentioned before, the txt cannot be found :( i hope u guys can help me

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

//Prototype
void readCards();

int main()
{

    readCards();

    return 0;
}

void readCards(){

    const int CARDS = 10;
    int ID[CARDS];
    string cardName[CARDS] ;
    string pcode[CARDS];
    string type[CARDS];
    string plusMode[CARDS];
    string system[CARDS] ;
    int i=0;

    ifstream infile;
    infile.open("cards.txt");
    if (infile)
    {
        while (!infile.eof()){

            //Read from file
            infile >> ID[i] >> cardName[i] >> pcode[i] >> type[i] >> plusMode[i]>>system[i];
            i++;

        }
        infile.close();

    }
    else
    {
        cout << "File cannot be found.\n";
        exit(0);
    }
}

Recommended Answers

All 2 Replies

No mention of the compiler. For Visual C++ the text file has to be in a certain folder but without knowing which compiler you use and host OS I can't see if that's the problem.

What you want to do is handle the white spaces on your cardNames or change the formatting. It thinks that there are more than 6 variables.

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

//Prototype
void readCards();

int main()
{
    readCards();
    return 0;
}

void readCards() {

    const int CARDS = 10;
    int ID[CARDS]{};
    string cardName[CARDS];
    string pcode[CARDS];
    string type[CARDS];
    string plusMode[CARDS];
    string system[CARDS];
    int i = 0;

    ifstream infile;
    infile.open("cards.txt");

        while (i<CARDS)
        {
            while (infile.good())
            {
                while (infile >> ID[i] >> cardName[i] >> pcode[i] >> type[i] >> plusMode[i] >> system[i]) {
                    std::cout << ID[i] << " " << cardName[i] << " " << pcode[i] << " " << type[i] << " " << plusMode[i] << " " << system[i] << std::endl;
                    i++;
                }
            }    

        }   

        infile.close();
            exit(0);
        }

 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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.