1

I am making it so it adds, multiples, subtracts, and divides. I am running it through my command prompt and I dont know how to make it so there is a space between each calculation. Or Even if I can get them on separate lines. But just a space would be fine.

#include <iostream> //io from console

int main() //app entry point 
{ 


  int num1;  //first variabe
  int num2; //second variable
  int sum; //sum of 2 variabes
  int product; //product of 2 variabes
  int difference; //differencr of 2 variabes
  int quotient; //quotient of 2 variabe

  std::cout << "Enter first number:  "; //prompt user for input
  std::cin >> num1; //assigns input to num1

  std::cout << "Enter second number  "; //prompt user for input
  std::cin >> num2; //assigns inout to num2

  sum = num1 + num2; //calcs the sum
  std::cout << "The sum is      "     <<  sum;   //displays the sum

  product = num1 * num2; //calcs the product
  std::cout << "  The product is     " <<     product;   //displays the product

  difference = num1 - num2; //calcs the product
  std::cout << "  The difference is     "   <<  difference;   //displays the        difference

  quotient = num1 / num2; //calcs the quotient
  std::cout << "  The quotient is   "    <<  quotient;   //displays the quotient

}
Martin Kristiansen
  • 9,875
  • 10
  • 51
  • 83

4 Answers4

4

Put this between each line or at the end of your lines or in the string you are printing.

std::cout << "\n";

Or add << endl; at the end of each output line.

Added example for you below with the \n.


       #include <iostream> //io from console

       int main() //app entry point 
       { 


    int num1;  //first variabe
    int num2; //second variable
    int sum; //sum of 2 variabes
    int product; //product of 2 variabes
    int difference; //differencr of 2 variabes
    int quotient; //quotient of 2 variabe

    std::cout << "\nEnter first number:  "; //prompt user for input
    std::cin >> num1; //assigns input to num1

    std::cout << "\nEnter second number  "; //prompt user for input
    std::cin >> num2; //assigns inout to num2

    sum = num1 + num2; //calcs the sum
    std::cout << "\nThe sum is      "     <<  sum;   //displays the sum

    product = num1 * num2; //calcs the product
    std::cout << "\n  The product is     " <<     product;   //displays the product

    difference = num1 - num2; //calcs the product
    std::cout << "\n  The difference is     "   <<  difference;   //displays the        difference

    quotient = num1 / num2; //calcs the quotient
    std::cout << "\n  The quotient is   "    <<  quotient;   //displays the quotient

}
prolink007
  • 33,872
  • 24
  • 117
  • 185
  • 1
    Note that `"\n"` and `std::endl` are almost interchangeable. Use `endl` if you will be mixing input and output, `"\n"` otherwise. – Mark Ransom Jan 10 '12 at 21:38
  • 1
    @Mark : Why recommend `endl` at all here? The standard already guarantees that standard output will be flushed before attempting to read from standard input. Meanwhile, encouraging new programmers to use `endl` when they really just need a newline implants bad habits. – ildjarn Jan 10 '12 at 21:39
  • @ildjarn: suggested both as options. Not one as a 'standard'. – prolink007 Jan 10 '12 at 21:40
  • @ildjarn you're probably right. Is `endl` only useful if you're expecting a visible wait between lines of output? – Mark Ransom Jan 10 '12 at 21:42
  • 1
    @Mark : As far as using `cout` specifically, that's exactly right. And for writing to file streams, you rarely want to flush manually (except for logging purposes), as that will kill performance. My overall thoughts on `endl` are summed up in [this answer](http://stackoverflow.com/a/5492605/636019) and its comments. – ildjarn Jan 10 '12 at 21:44
2

You can move the cursor to the following line like so:

std::cout << "\n";
std::cout << std::endl;

The endl option will force the stream to flush, as well.

Your code might then look like:

product = num1 * num2;
difference = num1 - num2;

std::cout << "  The product is:   " << product << "\n";
std::cout << "  The difference is " << difference << std::endl;
Bill
  • 14,257
  • 4
  • 43
  • 55
1

You can use:

std::cout << std::endl;

to add new lines

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

Add << std::endl to output a new line character each time.

Rob Agar
  • 12,337
  • 5
  • 48
  • 63