[問題] 請教一個練習題
請教各位高手 我在做一練習題
題目如下:
產生一個陣列至多儲存10個學生的名字
以及另一個陣列儲存每個學生的成績(0~100)
用迴圈提示使用者輸入名字和成績
計算平均成績(利用迴圈計數)
顯示此平均成績並以表格顯示所有學生的名字和成績
我已初步達成題目要求
Question 1:可是為什麼顯示的時候 數字 名字 都無法對齊
是我setw使用錯誤嗎 還是另有原因
Question 2:輸入名字的時候 一定要以#結束嗎
一開始我的程式碼沒使用getline(cin,name[i],'#');
而是使用getline(cin,name[i]);
可是就是會有問題 不知道原因 可請懂的人稍微解釋一下嗎 謝謝~
煩請高手解答~謝謝
以下為程式碼:
#include<iostream>
#include<iomanip>
#include<cctype>
using namespace std;
int main()
{
const int student_number=10;
string name[student_number]={};
int grade[student_number]={0};
int count=0;
double average_grade=0.0;
char answer='y';
for(int i =0;( i<student_number && tolower(answer)=='y');i++)
{
cout<<"Please enter the student's name:"<<endl;
getline(cin,name[i],'#');
cout<<"Please enter the grade of student "<<name[i]<<"."<<endl;
cin>>grade[i];
count++;
cout<<"Do you still want to input any data ? "<<endl;
cin >> answer ;
}
int sum=0;
for(int i=0; i<count ; i++)
{
sum += grade[i];
}
average_grade = static_cast<float>(sum) / static_cast<float>(count);
cout<<"student name grade"<<endl;
for(int i =0;i< count;i++)
{
cout<<setw(12)<<name[i]<<setw(15)<<grade[i];
}
cout<<"\n average "<<average_grade<<endl;
return 0;
}
--
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.204.42.2
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1425153128.A.91F.html
推
03/01 04:01, , 1F
03/01 04:01, 1F
→
03/01 04:51, , 2F
03/01 04:51, 2F
→
03/01 04:51, , 3F
03/01 04:51, 3F
→
03/01 04:52, , 4F
03/01 04:52, 4F
推
03/01 07:42, , 5F
03/01 07:42, 5F
→
03/01 07:43, , 6F
03/01 07:43, 6F
→
03/01 07:44, , 7F
03/01 07:44, 7F
→
03/01 07:44, , 8F
03/01 07:44, 8F
→
03/01 07:45, , 9F
03/01 07:45, 9F
→
03/01 07:45, , 10F
03/01 07:45, 10F
→
03/01 07:46, , 11F
03/01 07:46, 11F
→
03/01 07:46, , 12F
03/01 07:46, 12F
→
03/01 07:47, , 13F
03/01 07:47, 13F
→
03/01 19:48, , 14F
03/01 19:48, 14F
→
03/01 19:48, , 15F
03/01 19:48, 15F
→
03/01 19:49, , 16F
03/01 19:49, 16F
→
03/01 19:49, , 17F
03/01 19:49, 17F
→
03/01 19:50, , 18F
03/01 19:50, 18F
→
03/01 22:34, , 19F
03/01 22:34, 19F
推
03/02 06:31, , 20F
03/02 06:31, 20F
討論串 (同標題文章)