strcmp()函数用的出问题了,查不出来呀!

jjmin 2007-10-31 04:57:13
#include <iostream.h>
#include <string.h>

int main()
{
char* buf1="aaaa";
char* buf2="bbb";
char* buf3="ccccc";
int ptr1,ptr2;

ptr1=strcmp(buf1,buf2);  
if(ptr1>0)
cout<<"Buffer 1 is greater than buffer 2"<<endl;
else
cout<<"Buffer 2 is less than buffer 1"<<endl;

ptr2=strcmp(buf2,buf3);
if(ptr2>0)
cout<<"Buffer 2 is greater than buffer3";
else
cout<<"Buffer 2 is less than buffer3";
return 0;

}
...全文
231 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
独孤过儿 2007-10-31
  • 打赏
  • 举报
回复
乖乖,仔细看一下我给你的回复好吗?
jjmin 2007-10-31
  • 打赏
  • 举报
回复
解决了,谢谢大家了。
0黄瓜0 2007-10-31
  • 打赏
  • 举报
回复
输出的叙述是矛盾的.就象我跟你打赌说:是1我赢,不是1你输.
jjmin 2007-10-31
  • 打赏
  • 举报
回复

呵呵
写错了,但是这部影响运行呀,我运行出错了。我改成下面也不可以呀。
#include <iostream.h >
#include <string.h >

int main()
{
char* buf1= "aaaa ";
char* buf2= "bbb ";
char* buf3= "ccccc ";
int ptr1,ptr2;

ptr1=strcmp(buf1,buf2);  
if(ptr1 >0) cout < < "Buffer 1 is greater than buffer 2 " < <endl;

ptr2=strcmp(buf2,buf3);
if(ptr2 >0) cout < < "Buffer 2 is greater than buffer3 ";

return 0;

}
RoseinTrap 2007-10-31
  • 打赏
  • 举报
回复
char* buf1= "bbbb";
char* buf2= "bbb";
char* buf3= "bb ";
int ptr1,ptr2;


ptr1=strcmp(buf1,buf2);
if(ptr1 <0)
cout << "Buffer 2 is greater than buffer 1 " <<endl;
else
cout << "Buffer 2 is less than buffer 1 " <<endl;

ptr2=strcmp(buf2,buf3);
if(ptr2 >0)
cout << "Buffer 2 is greater than buffer3 ";
else
cout << "Buffer 2 is less than buffer3 ";
return 0;
0黄瓜0 2007-10-31
  • 打赏
  • 举报
回复
编译楼主的代码,中间有几个宽字符空格.删除就好了.
buf3>buf2>buf1,因为第一个字符就是C>b>a
独孤过儿 2007-10-31
  • 打赏
  • 举报
回复
int strcmp ( const char * str1, const char * str2 ); <cstring> 

Compare two strings

Compares the C string str1 to the C string str2.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminanting null-character is reached.


Parameters
str1
C string to be compared.
str2
C string to be compared.

Return Value
Returns an integral value indicating the relationship between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

Example
/* strcmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
char szKey[] = "apple";
char szInput[80];
do {
printf ("Guess my favourite fruit? ");
gets (szInput);
} while (strcmp (szKey,szInput) != 0);
puts ("Correct answer!");
return 0;
}


Output:

Guess my favourite fruit? orangeGuess my favourite fruit? appleCorrect answer!


zhangyanli 2007-10-31
  • 打赏
  • 举报
回复
楼住的代码真有意思:buffer1永远比buffer2 大了.呵呵
if(ptr1 >0)
cout < < "Buffer 1 is greater than buffer 2 " < <endl;
else
cout < < "Buffer 2 is less than buffer 1 " < <endl;
BluntBlade 2007-10-31
  • 打赏
  • 举报
回复
strcmp可能返回-1、0和1。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧