我的直方图程序有问题,我正在让它打印出我想要的确切内容,但是它有一些不寻常的间距问题,我想知道是否有人可以帮助我避免它。这就是我的输出的样子,你可以看到它在 90-100 括号之前的所有空格中都有错误的间距。
我希望它看起来像这样,所以数字和星号之间的底部没有空格:
*
*
* *
* *
* *
* *
* *
* *
* * *
* * * *
* * * *
0 1 2 3
也有人可以推荐一些东西来代替system("Clear")
orsystem("PAUSE")
吗?
代码:
#include <iostream>
#include <iomanip>
using namespace std;
void readExamMarks(int examMarks[], int sizeOfArray, int& counter1, int& counter2, int& counter3, int& counter4,int& counter5, int& counter6,int& counter7, int& counter8, int& counter9, int& counter10){
cout << "Please enter a set of exam marks to see a histogram for:" << endl;
int x = 0;
for( int idx = 0; idx < sizeOfArray; idx++){
cin >> x;
if((x >=0) && (x <= 100)){
x = x/10;
switch(x){
case 1:
counter1++;
break;
case 2:
counter2++;
break;
case 3:
counter3++;
break;
case 4:
counter4++;
break;
case 5:
counter5++;
break;
case 6:
counter6++;
break;
case 7:
counter7++;
break;
case 8:
counter8++;
break;
case 9:
counter9++;
break;
case 10:
counter9++;
break;
}
examMarks[idx] = x;
}
else{
cout << "ERROR: Value must be in range [0...100], please enter a valid value\n";
}
}
}
void printExamMarksDecade(){
cout << setw(5) << "10-20 " << "21-30 " << "31-40 " <<"41-50 " << "51-60 " << "61-70 " << "71-80 " << "81-90 " << "91-100 ";
}
void printHisto(int examMarks[], int sizeOfArray,int counter1, int counter2, int& counter3, int& counter4,int& counter5, int& counter6,int& counter7, int& counter8, int& counter9, int& counter10){
system("cls");
while(counter1 != 0 ){
cout << setw(3) << "*" << endl;
counter1--;
}
while(counter2 != 0 ){
cout << setw(10) << "*" << endl;
counter2--;
}
while(counter3 != 0 ){
cout << setw(17) << "*" << endl;
counter3--;
}
while(counter4 != 0 ){
cout << setw(24) << "*" << endl;
counter4--;
}
while(counter5 != 0 ){
cout << setw(31) << "*" << endl;
counter5--;
}
while(counter6 != 0 ){
cout << setw(38) << "*" << endl;
counter6--;
}
while(counter7 != 0 ){
cout << setw(45) << "*" << endl;
counter7--;
}
while(counter8 != 0 ){
cout << setw(52) << "*" << endl;
counter8--;
}
while(counter9 != 0 ){
cout << setw(59) << "*" << endl;
counter9--;
}
}
int main()
{
int examMarks[30];
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int counter6 = 0;
int counter7 = 0;
int counter8 = 0;
int counter9 = 0;
int counter10 = 0;
readExamMarks(examMarks, 30, counter1, counter2, counter3, counter4, counter5, counter6, counter7, counter8, counter9, counter10);
printHisto(examMarks, 30, counter1, counter2, counter3, counter4, counter5, counter6, counter7, counter8, counter9, counter10);
printExamMarksDecade();
system("PAUSE");
}