为什么小弟我的程序会出错,各位大大帮忙,多谢
为什么我的程序会出错,各位大大帮忙,谢谢
程序目的:将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;
然后就对了。
程序目的:将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;
然后就对了。