开发者

C++ Program help: Structs, arrays, loops, input files, loop not working?

I am trying to write a program which will declare an array of 5 structs from information read from a file. Then I use a loop to the print the information of every element in the array.

The code I have written only seems to read one line from the txt. file. Any tips or advice would be appreciated.

#include <istream>
#include <iostream>
#include <ostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;


int main ()
{
    struct Bankinfo{
        string name;
        int accountnum;
        float checking;
        float savings;
        string phone;
    } bankinfo[5];


    int i;
    i=0;
    cout<<"This is a test program"<<endl;
    char x;
    x=0;
    for (i=0;i<=6;i++)
    {
        ifstream infile;
        char testinfo [10001];
        infile.open("testinfo.txt");
        cin.get(testinfo,10001);
        cout<<testinfo<<endl;

        infile>>bankinfo [i].name>>bankinfo [i].accountnum>>bankinfo [i].checking>>bankinfo [i].savings>>bankinfo [i].phone;


        cout<<setw(10) << (bankinfo[i].name);
        cout<<setw(10) <<(bankinfo [i].accountnum);
        cout<<setw(10) <<(bankinfo [i].checking);
        cout<<setw(10) <<setprecision (2)<<fixed<<(bankinfo [i].savings);
 开发者_运维百科       cout<<setw(15) <<(bankinfo [i].phone);

    }
    cout<<"                                             "<<endl;
    cout<<"Thanks for using the program"<<endl;
    return (0);
}


You're opening the file in each iteration of the loop in i. Try to get out of the loop the infile.open(...). Now it will read more lines. I don't see the purpose of that cin.get(...) either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜