读书人

为什么小弟我的程序会出错各位大大帮

发布时间: 2012-02-22 19:36:56 作者: rapoo

为什么我的程序会出错,各位大大帮忙,谢谢
程序目的:将10个字符串排序

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string str[10];
string *p = NULL;
int i, j;

for ( i = 0; i < 10; i++ )
{
cout < < "Input str[ " < < i < < "]: ";
cin > > str[i];
}

for ( i = 0; i < 10; i++ )
{
for ( j = i + 1; j < 10; j++)
{
if ( str[i] > str[j] )
{
*p = str[i];
str[i] = str[j];
str[j] = *p;
}
}
}

cout < < "Now the strings are: " < < endl;

for ( i = 0; i < 10; i++ )
{
cout < < str[i] < < endl;
}

return 0;
}

[解决办法]
p初值为空,不能用他来接收。
可以
string pstr;
string *p=&pstr;
然后就对了。

读书人网 >C++

热点推荐