好吧,我正在嘗試從文件中的值製作垂直條形圖。下面的代碼工作,到一個點,並水平打印,但每行一個星號,這意味着有空格(顯然)。不尋找勺子的答案,只是在正確的方向推動。C++中的垂直條形圖
using namespace std;
int main()
{
int counter;
cout<<"Please enter a number"<< "\n";
counter=0;
char *fname = "C:/Users/Jordan Moffat/Desktop/coursework/problem2.txt";
int x;
ifstream infile(fname);
while (infile >> x)
{
if (x==0 && x<=10){
cout<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\n";
}
else if (x>=10 && x<=20){
cout<<"\t"<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\n";
}
else if (x>=20 && x<=30){
cout<<"\t"<<"\t"<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\n";
}
else if (x>=30 && x<=40){
cout<<"\t"<<"\t"<<"\t"<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\n";
}
else if (x>= 40 && x<=50){
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\n";
}
else if (x>=50 && x<=60){
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\n";
}
else if (x>=60 && x<=70){
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"*"<<"\t"<<"\t"<<"\t"<<"\n";
}
else if (x>=70 && x<=80){
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"*"<<"\t"<<"\t"<<"\n";
}
else if (x>=80 && x<=90){
cout<<"*"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"*"<<"\t"<<"\n";
}
else if (x>=90 && x<=100){
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"*"<<"\n";
}
}
cout<<"====================================================================================="<< "\n";
cout<<"0-9"<<"10-19"<<"20-29"<<"30-39"<<"40-49"<<"50-59"<<"60-69"<<"70-79"<<"80-89"<<"90-100"<<"\n";
system("PAUSE");
}
您必須翻轉您的顯示器(或您的頭部)才能看到豎線爲豎線 – CapelliC 2012-04-03 06:40:10