因此,今天我決定使用向量編寫一個簡單的直方圖,然而當我編寫這個程序並編譯它時,輸出顯示了一個永不結束的「*」。C++試圖創建直方圖
#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 = ** * ****「它只是繼續重複而不去下一行。任何建議來解決這個問題? 編輯:我試圖使用迭代器之前,但它有這個相同的輸出
'else u ++;'?刪除其他。 – user1810087
非常感謝你itwasntpete!我仍然需要對自己的邏輯進行更多的訓練。 – Kurangceret