if 的一个问题。。为什么会这样?

hahanibc 2009-02-12 12:10:29
#include <iostream.h>
void main()
{
int a[11]={42,55,73,28,48,66,30,65,94,72};
int b;
int i=0;
cin >>b;
cout <<a[i]<<endl;
if (b=a[i])cout <<"a"<<endl;
else cout <<"b"<<endl;
}


我输入42的时候显示"a"随便输入一个1还显示"a"
晕啊 请指教
...全文
265 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
crospo 2009-02-14
  • 打赏
  • 举报
回复
==
王旺旺旺 2009-02-13
  • 打赏
  • 举报
回复
哈哈,被人鄙视了
十八道胡同 2009-02-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 hahanibc 的帖子:]
#include <iostream.h>
void main()
{
int a[11]={42,55,73,28,48,66,30,65,94,72};
int b;
int i=0;
cin >>b;
cout < <a[i] < <endl;
if (b=a[i])cout < <"a" < <endl; /*这里不应该是=,应该是==*/
else cout < <"b" < <endl;
}


[/Quote]
=是付值得,==是比较的
hahanibc 2009-02-12
  • 打赏
  • 举报
回复
感激不尽
hello101105 2009-02-12
  • 打赏
  • 举报
回复
#include <iostream.h>
void main()
{
int a[11]={42,55,73,28,48,66,30,65,94,72};
int b;
int i=0;
cin >>b;
cout < <a[i] < <endl;
if (b==a[i])cout < <"a" < <endl;
else cout < <"b" < <endl;
}

wzg112 2009-02-12
  • 打赏
  • 举报
回复
表达式错误!==
waizqfor 2009-02-12
  • 打赏
  • 举报
回复

if (b=a[i])cout < <"a" < <endl; //'='是赋值符号 ‘==’才是等于号 LZ注意啊

lspol 2009-02-12
  • 打赏
  • 举报
回复
= 是赋值肯定是真,==才是比较
独孤过儿 2009-02-12
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

int main()
{
int a[11] = {42, 55, 73, 28, 48, 66, 30, 65, 94, 72};
int b;
int i = 0;
cin >> b; // 输入的值存在b里面
cout << a[i] << endl; // 此处的i依然是0,所以打印出42
if (b == a[i]) // 这里应该是==,不是=的赋值
cout << "a" << endl;
else
cout << "b" << endl;

return 0;
}
wolflion 2009-02-12
  • 打赏
  • 举报
回复
表达式错误,=与==要区分
cppfaq 2009-02-12
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 xmrforever 的回复:]
引用 1 楼 fetag 的回复:
C/C++ code
#include <iostream>
using namespace std;

int main()
{
int a[11] = {42, 55, 73, 28, 48, 66, 30, 65, 94, 72};
int b;
int i = 0;
cin >> b; // 输入的值存在b里面
cout < < a[i] < < endl; // 此处的i依然是0,所以打印出42
if (b == a[i]) // 这里应该是==,不是=的赋值
cout < < "a" < < endl;
else

[/Quote]
xmrforever 2009-02-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fetag 的回复:]
C/C++ code
#include <iostream>
using namespace std;

int main()
{
int a[11] = {42, 55, 73, 28, 48, 66, 30, 65, 94, 72};
int b;
int i = 0;
cin >> b; // 输入的值存在b里面
cout << a[i] << endl; // 此处的i依然是0,所以打印出42
if (b == a[i]) // 这里应该是==,不是=的赋值
cout << "a" << endl;
else
cout << "b" << endl;

retur…
[/Quote]

一楼的正解
maldini20040607 2009-02-12
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

int main()
{
int a[11] = {42, 55, 73, 28, 48, 66, 30, 65, 94, 72};
int b;
int i = 0;
cin >> b;
cout < < a[i] < < endl;
if (b == a[i]) // 这里应该是==,不是=的赋值
cout < < "a" < < endl;
else
cout < < "b" < < endl;

return 0;
}
maldini20040607 2009-02-12
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

int main()
{
int a[11] = {42, 55, 73, 28, 48, 66, 30, 65, 94, 72};
int b;
int i = 0;
cin >> b;
cout << a[i] << endl;
if (b == a[i]) // 这里应该是==,不是=的赋值
cout << "a" << endl;
else
cout << "b" << endl;

return 0;
}
  • 打赏
  • 举报
回复
。。又是这个冏错误。。
wlk 2009-02-12
  • 打赏
  • 举报
回复
==与=
int a[11]={42,55,73,28,48,66,30,65,94,72};
if (b=a[i]) 与 if (1)没什么区别

yellowhwb 2009-02-12
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 szqh97 的回复:]
注意是==而不是=
[/Quote].
szqh97 2009-02-12
  • 打赏
  • 举报
回复
注意是==而不是=
sanguomi 2009-02-12
  • 打赏
  • 举报
回复

if (a[i] == b)
cout < <"a" < <endl;
Pajack 2009-02-12
  • 打赏
  • 举报
回复
容易出错的细节。。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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