所以今天我决定用向量写一个简单的直方图,但是当我写这个程序并编译它时,输出显示一个永无止境的“*”。
#include <iostream>
#include <conio.h>
#include <ios>
#include <vector>
#include <algorithm>
using std::cout;
using std::vector;
using std::cin;
using std::endl;
int main()
{
int x;
double k;
cout<<"How many range would you like = ";
cin>>x;
vector<double> number; //All data was being stored here
cout<<"Input the number to be included in histogram = ";
while(cin>>k)
number.push_back(k);
x = x*10;
sort(number.begin(), number.end());
for(int i=0;i<x;i+=10) // Problem is on this loop statement, but I can't fix it.
{
vector<double>::size_type u = 0;
cout<<i<<"-"<<i+9<<" = "; //The range, Intended to put the "*" besides it.
while(u < number.size())
{
if( number[u]<=i+9 && number[u]>=i)
cout<<"*"; //For showing how many numbers on that range
else
u++;
}
cout<<endl;
}
getch();
return 0;
}
没有显示错误,只有第一个范围内的“”永无止境。(例如,当程序编译时,我输入数据并显示“0-9 =** * **** " 并且它只是继续重复而不转到下一行。有什么建议可以解决这个问题吗?编辑:我之前尝试过使用迭代器,但它与这个具有相同的输出。