为什么最后一个cout打印出s = s而不是s = a? [英] Why does the last cout print out s=s and not s = a ?

查看:130
本文介绍了为什么最后一个cout打印出s = s而不是s = a?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< iostream>

使用命名空间std;



int f(int a, double b,char c){

1. if(c =='a'){

2. int b = a + 3;

3. a = b / 2;

4.返回(b - a);

}

5.否则if(c =='s'){

6. int a =(int)b - 2;

7. int b = a - 6;

8.返回a - b;

}

else {

9.返回0;

}

}

int main(){

10. int x = 4;

11. double y = 2.5;

12. char s ='a';

13. x = f((int)y,(double)x,s);

14. cout<< x =<< x<< y =<< y<< s =<< s<<结束;

15. {

16. int x = -4;

17. double y = -1.6;

18. s ='s';

19. y =(double)f(x,y,s);

20. cout<< x =<< x<< y =<< y<< s =<< s<<结束;

21.}

22. cout<< x =<< x<< y =<< y<< s =<< s<<结束;

23.返回0;

}



我尝试了什么:



我今天刚刚学会了c ++中的范围,我不明白为什么最后一个cout显示s = s而不是a = a。

解决方案

因为在打印值之前就给它指定了's':

 s ='s'; 



并且它不会改变你打印的价值:

 y =(double)f(x,y ,s); 
cout<< x =<< x<< y =<< y<< s =<< s<< ENDL;
}
cout<< x =<< x<< y =<< y<< s =<< s<< ENDL;
返回0;

范围与此无关 - s 在整个 main <的范围内声明/ code>函数,并且当它离开语句块时变量中的值不会改变。



你的范围大括号是完全不相关的在这个例子中 - 你可以删除它们而不会影响应用程序。


引用:

我不喜欢不知道为什么最后一个cout显示s = s而不是a = a。



你的代码没有你想象的那样,你不明白为什么?

然后使用调试器并观察你的代码执行。



调试器中没有魔法,它不知道你应该做什么,它没有发现错误,只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]


#include <iostream>
using namespace std;

int f(int a, double b, char c) {
1. if (c == 'a') {
2. int b = a + 3;
3. a = b / 2;
4. return (b - a);
}
5. else if(c == 's') {
6. int a = (int) b - 2;
7. int b = a - 6;
8. return a - b;
}
else {
9. return 0;
}
}
int main() {
10. int x = 4;
11. double y = 2.5;
12. char s = 'a';
13. x = f((int)y, (double)x, s);
14. cout << "x = " << x << " y= " << y << " s = " << s << endl;
15. {
16. int x = -4;
17. double y = -1.6;
18. s = 's';
19. y = (double)f(x, y, s);
20. cout << "x = " << x << " y= " << y << " s = " << s << endl;
21. }
22. cout << "x = " << x << " y= " << y << " s = " << s << endl;
23. return 0;
}

What I have tried:

I just learned about scopes in c++ today, and I don't get why the last cout displays s=s and not a=a.

解决方案

Because you assign 's' to it just before you print the value:

s = 's';


And it doesn't change befoee you print the value:

y = (double)f(x, y, s);
cout << "x = " << x << " y= " << y << " s = " << s << endl;
}
cout << "x = " << x << " y= " << y << " s = " << s << endl;
return 0;

Scope is irrelevant here - s is declared in scope for the whole of the main function, and the value in a variable doesn't change when it leaves a statement block.

Your "scope" curly brackets are completely irrelevant in this example - you can delete them without affecting the application at all.


Quote:

I don't get why the last cout displays s=s and not a=a.


Your code do not behave the way you expect, and you don't understand why ?
Then use the debugger and watch your code execute.

There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]


这篇关于为什么最后一个cout打印出s = s而不是s = a?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆