简体   繁体   English

为什么函数二返回 cout << s << " " << t << " " << b << endl; 当它说只返回 (b) 时?

[英]Why does function Two return cout << s << " " << t << " " << b << endl; when it says to only return (b)?

Hello I need help with this tracing problem, here here is a copy of the problem:您好,我需要有关此跟踪问题的帮助,这里是问题的副本:

What is the output of the following program segment?以下程序段的输出是什么?

int u = 4, v = 3;
one(u, v);
cout << u << " " << v << endl;
cout << two(u, v) << " " ;
cout << u << " " << v << endl;
void one(int &x, int& y){
 int a;
 a = ++x ;
 x += y++;
 y = ++a;
}
int two(int s, int &t){
 int b;
 b = s – t;
 s += t + b ;
 t += 4 * b;
 cout << s << " " << t << " " << b << endl;
return ( b ) ;
}

I managed to find the first output of function One, then I plugged it into function Two to find its output.我设法找到函数一的第一个输出,然后将其插入函数二以找到其输出。 But function Two returned the cout instead of the return (b), can anyone show me what I am doing wrong, any help would be greatly appreciated!但是函数二返回了 cout 而不是返回(b),谁能告诉我我做错了什么,任何帮助将不胜感激! Here is a copy of the output after plugging it into visual studios:这是将其插入 Visual Studios 后的输出副本:

Output:输出:
8 6 8 6
16 14 2 16 14 2
2 8 14 2 8 14

The relevant fragment is equivalent to the following, with the step-by-step breakdown in comments.相关片段等效于以下内容,并在评论中逐步细分。

cout << u << " "   // prints 8
     << v          // prints 6
     << endl;      // ends first line "8 6"

int n = two(u, v); // calls 'two' which prints the second line "16 4 2" and returns 2

cout << n          // prints 2 which is the value returned by 'two' at the previous step
     << " " ;      // prints a space but does not end the line

cout << u << " "   // prints 8 next on the third line
     << v          // prints 14
     << endl;      // ends third line and prints "2 8 14"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么cout &lt;&lt; * s &lt;&lt; endl会产生段错误? - Why does cout << *s << endl produce a segfault? 请解释这个语句(cout &lt;&lt; 1 + int((a &lt; b) ^ ((b - a) &amp; 1)) &lt;&lt; endl) - please explain this statement(cout << 1 + int((a < b) ^ ((b - a) & 1)) << endl) 为什么cout返回smanip? - Why does cout return smanip? 为什么endl(std :: cout)会编译 - Why does endl(std::cout) compile 为什么&#39;std :: endl&#39;在语句&#39;std :: cout &lt;&lt; std :: endl;“中使用时需要命名空间限定,给定参数依赖查找? - Why does 'std::endl' require the namespace qualification when used in the statement 'std::cout << std::endl;", given argument-dependent lookup? 为什么`&amp;*(&amp;*d)` 返回“c”的地址而不是“b”的地址? - Why does `&*(&*d)` return address of "c" instead of address of "b"? “cout &lt;&lt;(a,b)”的输出是什么?为什么? - What is the output of “cout << (a, b)” and why? 为什么cout.tellp总是返回-1? - why does cout.tellp always return -1? 将 class A 的指针转换为 function 中的 class B 的指针,当 B 具有带有“A*”的构造函数时 - Converting pointer of class A to class B in function return, when B has constructor with `A*` 为什么不'QTextStream(stdout)<< endl;'编译时,'QTextStream(stdout)<<“”<< endl;'呢? - Why doesn't 'QTextStream(stdout) << endl;' compile, when 'QTextStream(stdout) << “” << endl;' does?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM