2017-02-13 30 views
-2

嗨首先這是我的計劃,我想我的程序中所有的輸出隔離在在C運輸及工務局局長問題++

下面是我的程序的輸出現在我想使輸出爲全行在年齡和部分:

#include<iostream> 
#include <iomanip> 
using namespace std; 
struct student { 
string fullname[200]; 
string course[200]; 
int age[200]; 
}; 
student p1; 
main() 
{ 
    int input; 
    cout<<"How many Student do you like to Add:"; 
    cin>>input; 
    for(int i=0; i!=input; i++) 
    { 
     cout<<"----------Input #"<<i+1<<"----------"<<endl; 
     cout<<"Enter Name: "; 
     cin.ignore(); 
     getline (cin,p1.fullname[i]); 
     cout<<"Enter Age: "; 
     cin>>p1.age[i]; 
     cout<<"Enter Section: "; 
     cin>>p1.course[i]; 
    } 

    cout<<"\n\n----------------- List of Student that is 19 Above -----------------"<<endl; 
    cout<<"\nFull Name:"<<setw(20)<<"Age:"<<setw(20)<<"Section:"<<endl; 

    for(int i=0; i!=input; i++) 
    { 
     if(p1.age[i] >= 19) 
     { 
     cout<<p1.fullname[i]<< setfill(' ') <<setw(20) <<p1.age[i]<< setfill(' ') <<setw(20)<<p1.course[i]<<endl;  
     } 
    } 
    system("PAUSE"); 
} 
+1

歡迎。請看[問]。 –

回答

1

你需要,你想寫前場申請setfillsetw。並且您還需要將其應用於fullname

cout << setfill(' ') 
    << setw(20) << p1.fullname[i] 
    << setw(20) << p1.age[i] 
    << setw(20) << p1.course[i] 
    << endl; 
+0

如果填充字符不變,只需要應用一次'setfill' –

+0

@ M.M好點。在我的答案中,我只是複製粘貼了原始代碼,因此是雙setfill。我現在只是把它減少到了一個'setfill'。 –