如何在执行方程时在 C++ 中开始换行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13505720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start a newline in C++ while doing equations
提问by Timothy Rebidue
Im reading through "The C++ Programming Language" and my current assignment is to make a program that takes two variables and determines the smallest, largest, sum, difference, product, and ratio of the values.
我正在阅读“C++ 编程语言”,我当前的任务是编写一个程序,该程序采用两个变量并确定这些值的最小、最大、总和、差、乘积和比率。
Problem is i can't start a newline. "\n" doesn't work because i have variables after the quote. And "<< endl <<" only works for the first line. I googled the hell out of this problem and im coming up short.
问题是我无法开始换行。"\n" 不起作用,因为我在引号后有变量。而 "<< endl <<" 仅适用于第一行。我在谷歌上搜索了这个问题,结果很短。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open() {char ch;cin>>ch;}
int main()
{
int a;
int b;
cout<<"Enter value one\n";
cin>>a;
cout<<"Enter value two\n";
cin>>b;
(a>b); cout<< a << " Is greater than " << b;
(a<b); cout<< a << " Is less than " << b;
keep_window_open();
return 0;
}
回答by emartel
You are looking for std::endl, but your code won't work as you expect.
您正在寻找std::endl,但您的代码无法按预期工作。
(a>b); cout<< a << " Is greater than " << b;
(a<b); cout<< a << " Is less than " << b;
This is not a condition, you need to rewrite it in terms of
这不是条件,您需要根据以下条件重写它
if(a>b) cout<< a << " Is greater than " << b << endl;
if(a<b) cout<< a << " Is less than " << b << endl;
You can also send the character \nto create a new line, I used endlas I thought that's what you were looking for. See this threadon what could be issues with endl.
你也可以发送角色\n来创建一个新行,我用过,endl因为我认为这就是你要找的。请参阅此线程,了解endl.
The alternative is written as
替代写为
if(a>b) cout<< a << " Is greater than " << b << "\n";
if(a<b) cout<< a << " Is less than " << b << "\n";
There are a few "special characters" like that, \nbeing new line, \rbeing carriage return, \tbeing tab, etc... useful stuff to know if you're starting.
有一些像这样的“特殊字符”,\n换行符,\r回车符,\t制表符等...有用的东西,知道你是否开始。
回答by dasblinkenlight
You can output std::endlto the stream to move to the next line, like this:
您可以输出std::endl到流以移动到下一行,如下所示:
cout<< a << " Is greater than " << b << endl;

