8

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;
}
Timothy Rebidue
  • 101
  • 1
  • 2
  • 5
  • note that the difference between a `"\n"` and `std::endl` is that the latter includes a `flush`; this will make no difference for you in this case. – Keith Nov 22 '12 at 04:09
  • You can chain `<<` as you have already done: `if (a > b) cout << a << " is greater than " << b << "\n";`. Note that `(a > b);` by itself has no effect; it merely computes whether `a` is greater than `b` and does nothing with the result. You want `if (condition) { ... }` for conditional branching. – Jon Purdy Nov 22 '12 at 04:13

2 Answers2

8

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.

Community
  • 1
  • 1
emartel
  • 7,712
  • 1
  • 30
  • 58
  • 1
    There is no good reason for him to prefer `std::endl` over `"\n"` in this program, and there is reason for him to prefer `"\n"` generally. Google "endl fiasco." – Robᵩ Nov 22 '12 at 04:25
  • @Robᵩ, you do realize that it's probably his first C++ program and that he simply wants a new line? `endl` is probably how it's explained in the book too... haven't read it in years – emartel Nov 22 '12 at 04:27
  • Updated the question with a more detailed answer – emartel Nov 22 '12 at 04:30
  • @emartel - Thanks for updating the question. I think we have an obligation to show first-program programers a better way, particularly when it comes to `std::endl`, `using namespace std;`, and other widely-taught mistakes. – Robᵩ Nov 22 '12 at 04:32
  • @Robᵩ I guess I have to agree with you :) – emartel Nov 22 '12 at 04:33
3

You can output std::endl to the stream to move to the next line, like this:

cout<< a << " Is greater than " << b << endl;
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523