題目:一個文本,第一行是捐款人數量,第二行是第一個捐款人姓名,第三行是第一個捐款人捐款額,然后是下一個人的姓名和捐款額,寫一個程式能從這個檔案里分別讀取資料,然后 將捐款大于等于10000的列入Grand Patrons串列里,否則列入Patrons,如果其中一組是空則列印none!;這個文本能成功打開,也能成功讀取第一行的數字,然后就不能繼續往下讀了,結果總是如下:
//
Enter the filename:donors.txt
Grand Patrons:
None!
Patrons:
0
0
0
0
Program ended with exit code: 0
//
以下是代碼,請問各路大神,問題出在哪兒???
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cstdlib>
using namespace std;
struct donor{
string name;
double money;
};
int main() {
int n;
char filename[20];
cout<<"Enter the filename:";
cin.getline(filename,20);
ifstream infile;
infile.open(filename);
if(!infile.is_open())
{
cout<<"Could not open "<<filename<<endl;
exit(EXIT_FAILURE);
}
(infile>>n).get();
vector<donor> ar(n);
for(int i=0;i<n;++i)//文本檔案能成功打開,但是這里為啥讀不到里面的資料????
{
getline(infile,ar[i].name);
(infile>>ar[i].money).get();
}
cout<<"Grand Patrons:"<<endl;
int m=0;
for(int i=0;i<n;++i)
if(ar[i].money>=10000)
{
cout<<ar[i].name<<' '<<ar[i].money<<endl;
++m;
}
if(m==0)
cout<<"None!"<<endl;
m=0;
cout<<"Patrons:"<<endl;
for(int i=0;i<n;++i)
if(ar[i].money<10000)
{
cout<<ar[i].name<<' '<<ar[i].money<<endl;
++m;
}
if(m==0)
cout<<"None!"<<endl;
infile.close();
return 0;
}
uj5u.com熱心網友回復:
首先宣告,這是我百度,沒有除錯過#include <iostream>
#include <fstream>
#include <string>
int main()
{
using namespace std;
struct Juan
{
string name;
double k;
};
int num=0,i=0;
ifstream ifs;
ifs.open("2.txt");
ifs>>num;
const int count=num;
Juan *member=new Juan[count];
while(num--)
{
ifs.get();
getline(ifs,member[i].name);
ifs>>member[i].k;
i++;
}
cout<<"Grand Patrons:"<<endl;
num=0;
for(i=0;i<count;i++)
if(member[i].k>10000)
{
cout<<member[i].name<<endl;
num++;
}
if(num==0)
cout<<"none"<<endl;
num=0;
cout<<"Patrons:"<<endl;
for(i=0;i<count;i++)
if(member[i].k<10000)
{
num++;
cout<<member[i].name<<endl;
}
if(num==0)
cout<<"none"<<endl;
return 0;
}
uj5u.com熱心網友回復:
不除錯,前面能打開檔案部分不管,展示一下你文本內容?緣由https://bbs.csdn.net/topics/396510443
int n;
(infile >> n).get();
vector<donor> ar(n);
for (int i = 0; i<n; ++i)//文本檔案能成功打開,但是這里為啥讀不到里面的資料????
{
getline(infile, ar[i].name);
(infile >> ar[i].money).get();
}
cout << "Grand Patrons:" << endl;
int m = 0;
for (int i = 0; i<n; ++i)
if (ar[i].money >= ............................................................)
{
cout << ar[i].name << ends << ar[i].money << endl;
++m;
}
if (m == 0)
cout << "None!" << endl;
m = 0;
cout << "Patrons:" << endl;
for (int i = 0; i<n; ++i)
if (ar[i].money<................................................................)
{
cout << ar[i].name << ends << ar[i].money << endl;
++m;
}
if (m == 0)
cout << "None!" << endl;
infile.close();
uj5u.com熱心網友回復:
那個管ends的這里又有ends了呀,真是很好的機會。uj5u.com熱心網友回復:
4Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
RIch Raptor
5000
uj5u.com熱心網友回復:
這個是txt文本內容uj5u.com熱心網友回復:
不除錯,前面能打開檔案部分不管,展示一下你文本內容?緣由https://bbs.csdn.net/topics/396510443
int n;
(infile >> n).get();
vector<donor> ar(n);
for (int i = 0; i<n; ++i)//文本檔案能成功打開,但是這里為啥讀不到里面的資料????
{
getline(infile, ar[i].name);
(infile >> ar[i].money).get();
}
cout << "Grand Patrons:" << endl;
int m = 0;
for (int i = 0; i<n; ++i)
if (ar[i].money >= ............................................................)
{
cout << ar[i].name << ends << ar[i].money << endl;
++m;
}
if (m == 0)
cout << "None!" << endl;
m = 0;
cout << "Patrons:" << endl;
for (int i = 0; i<n; ++i)
if (ar[i].money<................................................................)
{
cout << ar[i].name << ends << ar[i].money << endl;
++m;
}
if (m == 0)
cout << "None!" << endl;
infile.close();
uj5u.com熱心網友回復:
展示一下你除錯出現問題的截圖?uj5u.com熱心網友回復:
并且確定你已經找到并打開了文本檔案的截圖uj5u.com熱心網友回復:
展示一下你除錯出現問題的截圖?
uj5u.com熱心網友回復:
那測驗一下,只管讀取資料,其他不管
uj5u.com熱心網友回復:
那測驗一下,只管讀取資料,其他不管
First donor
Enter the name of donor:Freida Flass
Enter the money donoted:100500
Next donor:
Enter the name of donor:Sam Stone
Enter the money donoted:2000
Next donor:
Enter the name of donor:Tanmmy Tubbs
Enter the money donoted:5000
Next donor:
Enter the name of donor:Rich Raptor
Enter the money donoted:500
Grand Patrons:
FrFreida Flass 100500
Patrons:
Sam Stone 2000
Tanmmy Tubbs 5000
Rich Raptor 500
Program ended with exit code: 0
uj5u.com熱心網友回復:
那測驗一下,只管讀取資料,其他不管
#include <string>
#include <vector>
using namespace std;
struct donor{
string name;
double money;
};
int main() {
int n;
cout<<"Enter the number of contributors:";
(cin>>n).get();
cout<<"First donor"<<endl;
vector<donor> ar(n);
for(int i=0;i<n;++i)
{
cout<<"Enter the name of donor:";
getline(cin,ar[i].name);
cout<<"Enter the money donoted:";
(cin>>ar[i].money).get();
if(i!=n-1)
cout<<"Next donor:"<<endl;
}
cout<<"Grand Patrons:"<<endl;
int m=0;
for(int i=0;i<n;++i)
if(ar[i].money>=10000)
{
cout<<ar[i].name<<' '<<ar[i].money<<endl;
++m;
}
if(m==0)
cout<<"None!"<<endl;
m=0;
cout<<"Patrons:"<<endl;
for(int i=0;i<n;++i)
if(ar[i].money<10000)
{
cout<<ar[i].name<<' '<<ar[i].money<<endl;
++m;
}
if(m==0)
cout<<"None!"<<endl;
return 0;
}
uj5u.com熱心網友回復:
那測驗一下,只管讀取資料,其他不管
uj5u.com熱心網友回復:
去輸入?沒那功夫
uj5u.com熱心網友回復:
用你的代碼測驗沒發現蝦米問題。std::ifstream 讀檔案("..\\Debug\\回復專用\\捐款資料.txt");
uj5u.com熱心網友回復:
去輸入?沒那功夫
uj5u.com熱心網友回復:
至少已經幫助你驗證了大部分代碼了。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/77484.html
標籤:C++ 語言
上一篇:說我‘i’沒有定義求助
下一篇:QT獲取網頁上面展示的某一項資料
