我正在嘗試做一個冒泡排序,它將根據字符串「desc」按升序移動幾個並行數組的位置。當我運行程序時,我只是得到一個空白屏幕,程序永遠不會結束。冒泡排序阻止我的程序執行
//Trace Levinson- Store sales
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int maxp = 50;
void swapem(string& a, string& b)
{
string temp;
temp = a;
a = b;
b = temp;
}
//These swapem functions are the functions used to move the array
//values when the sort function is called
void swapem(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void swapem(char& a, char& b)
{
char temp;
temp = a;
a = b;
b = temp;
}
void swapem(double& a, double& b)
{
double temp;
temp = a;
a = b;
b = temp;
}
void sortem(int id[], string desc[], int numsold[], double price[],
double dolars[], int nump)
{
int i, j;
for (j = 0; j < nump - 1; j++)
for (i = 0; i = nump - 1; i++) //I'm sorting a group of parallel arrays by the
if (desc[i] > desc[i+1])//string "desc", and the others are being moved based off of that
{
swapem(desc[i], desc[i + 1]);
swapem(id[i], id[i + 1]);
swapem(numsold[i], numsold[i + 1]);
swapem(price[i], price[i + 1]);
swapem(dolars[i], dolars[i + 1]);
}
}
int main()
{
int id[maxp], numsold[maxp], nump;
double price[maxp], dolars[maxp];
string desc[maxp];
ifstream inf;
ofstream outf;
inf.open("storesales.dat");
outf.open("storesales.ot");
outf.setf(ios::fixed);
outf.precision(2);
initem(desc, id, numsold, nump, price, dolars);
readem(id,numsold,nump,price,desc);
printem(id, desc, numsold,nump, price, outf);
getsales(numsold, price, dolars,nump);
sortem(id, desc, numsold, price, dolars, nump);
printem(desc, id, numsold, nump, price, dolars, outf);
system("pause");
}
我知道這些功能是問題,因爲我重新測試了它們,並將這些調用評論出來,程序完成。問題是,我不知道什麼是錯的。
請不要用'void main'發佈代碼。它不會與許多編譯器一起編譯,因爲它不是標準的。 'int main'是**更短**和標準。 –
只是一個愚蠢的問題,你爲什麼要'j'循環'nump-1'次?當嵌套循環中沒有交換髮生時,你應該停止......當排序完成時,意味着停止。否則,你的排序總是會在最壞的情況下運行。 – Spektre