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.
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.
#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;
}
The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen.
As a programmer, you can use endl or \n to create new line commands in C++.
Simply comparing to '\n' should solve your problem; depending on what you consider to be a newline character, you might also want to check for '\r' (carriage return).
The symbol \n is a special formatting character. It tells cout to print a newline character to the screen; it is pronounced “slash-n” or “new line.”
You are looking for std::endl
, but your code won't work as you expect.
(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 \n
to create a new line, I used endl
as I thought that's what you were looking for. See this thread on what could be issues with 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, \n
being new line, \r
being carriage return, \t
being tab, etc... useful stuff to know if you're starting.
You can output std::endl
to the stream to move to the next line, like this:
cout<< a << " Is greater than " << b << endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With