'weapon.txt'というファイルから行を読み取って、この行の長い構造に入力しようとしています。
struct weapon
{
char name[20]; //Edited
int strength;
}
読み取るファイルは次のようになります。
Excalibur
150
Throwing Stars
15
Rapier
200
Bow and Arrow
100
Axe
200
Crossbow
100
Scimitar
250
Rusted Sword
10
Soul Slayer
500
私が今持っているコードは
#include<fstream>
#include<iostream>
#include<cstring>
using namespace std;
struct WeaponInfo
{
char name[16];
int strength;
};
const int MaxWeap = 10;
void openfile(ifstream&); //Opening the file
void displayfile(ifstream&, WeaponInfo&);//Display file
int main ()
{
WeaponInfo weapon[MaxWeap];
ifstream fin;
openfile(fin);
displayfile(fin, weapon[MaxWeap]);
}
void openfile(ifstream& fin)
{
fin.open("weapon.txt");
}
void displayfile(ifstream& fin, WeaponInfo& weapon[MaxWeap])
{
char nm;
int str;
while (fin.eof() == 0)
{
for(int i = 0; i <= MaxWeap; i++);
{
fin.getline(nm);
fin.getline(str);
strcpy(weapon[i].name, nm);
strcpy(weapon[i].strength, str);
i++;
cout << weapon[i].name << "\n" << weapon[i].strength << endl;
}
}
fin.close();
}
編集:これは私がそれをやり直した後今持っているものです、私は次のコンパイルエラーを受け取ります:参照の配列としての「武器」の宣言; 関数'voiddisplayfile(...)'で、'fin'はこのスコープで宣言されていません。'weapon'はこのスコープでは宣言されていません。ma、e'i'のルックアップが'scoping[-fpermissive]のISO'に変更されました。