求大神幫幫忙,int Information::setdown()
{
int n; //輸入一次性要錄入的人數,
int b; //臨時把考號賦給變數b,用于計算考號是否是8位數
cout << "請輸入錄入學生人數:" << endl;
cin >> n;
if (_access("D:\\information.txt", 0) != 0) //判斷檔案是否存在,如果存在,就寫入下面的內容
{
ofstream outf("D:\\information.txt", ios::app); //打開檔案,沒有時創造
if (!outf) //判斷檔案是否打開
{
cout << "Cannot open the file\n" << endl;
return 0;
}
outf << '\t' << '\t' << '\t' << "第一批成績匯總表" << endl; //保存資訊
outf << '\t' << "考號" << '\t' << '\t' << "姓名" << '\t' << "語文" << '\t' << "數學" << '\t' << "英語" << '\t' << "總分" << endl;
outf << "(由八位數字組成)" << "(中文)" << endl;
outf.close(); //關閉檔案
}
for (int i = 0; i < n; i++)
{
for (;;) //目前暫用死回圈,如果格式錯誤就不能執行下一步
{
int a = 0; //每一次都要把a這個值歸零,從新計算考號位數
cout << "請輸入學生的考號(8位數)、姓名、語文、數學、英語各科成績:" << endl;
cin >> num >> name >> chinese >> math >> english;
b = num;
while (b) //輾轉除10法計算考號幾位數
{
a++;
b = b / 10;
}
if (a == 8) //判斷是否是等于8位數,是就執行下一步
{
break;
}
else
{
cout << "格式有誤,請重新輸入!\n" << endl;
}
}
sum = chinese + math + english; //計算總分
ofstream outf; //打開檔案,以追加的形式,每一次錄入資訊就保存一次
outf.open("D:\\information.txt", ios::app);
outf << '\t' << num << '\t' << name << '\t' << chinese << '\t' << math << '\t' << english << '\t' << sum << endl; //保存資訊
outf.close(); //關閉檔案
}
}在這里面加個給我看看
uj5u.com熱心網友回復:
求大神啊,怎么論壇都死沉沉的,沒有人回答的嗎?uj5u.com熱心網友回復:
輸入發負值之類的自己判斷一下就行了uj5u.com熱心網友回復:
cin.getline(temp1, num-1,0);輸入的時候要按Enter然后輸入檔案結束符(Ctrl+Z),接著再按Enter能停止輸入。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/118594.html
標籤:基礎類
上一篇:C++builder制作日歷
