Printing into different lines.

How do I print out my answer into different lines without writing each line manually? (a=3, b=8, x=3).
data answer
3 8 3 3 6
4 7
5 8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void Read (int & a, int & b, int & x);
void Print (int a, int b, int x);
int main()
{
setlocale(LC_ALL, "Lithuanian");
int a, b, x;
Read (a, b, x);
Print (a, b, x);
return 0;
}
void Read (int & a, int & b, int & x)
{
    ifstream fd ("staciakampis_data.txt");
    fd >> a >> b >> x;
}
void Print (int a, int b, int x)
{
    ofstream fr ("staciakampis_res.txt");
    for (int i=0; i < 1; i++)
    {
       if (a + x <= b)
       {
          fr << a++ << " " << a++ + x;

       }
       else if (b + x <= a)
       {
          fr << b << " " << b + x;
       }
    }

}
I'm not clear exactly what you're asking, but... if you stream std::endl to fr, that will end one line and start a new one:

1
2
3
4
5
void Print ()
{
    ofstream fr ("staciakampis_res.txt");
    fr << 1 << " " << 2 << std::endl << 3 << " " << 4 << std::endl;
}

would put:

1 2
3 4

into your file.
Last edited on
//////data/////////answer////
//3(a) 8(b) 3(x)//3 6/////
///////////////////4 7/////
//////////////////5 8/////
basically I count the sides of a rectangle from a given interval [a;b], with one side being x units longer. Your approach doesn't work, since I still have to type everything manually, i.e it's good for just this data.
Your approach doesn't work, since I still have to type everything manually, i.e it's good for just this data.

My "approach" was merely to demonstrate how using std::endl allows you to end one line and start a new one. Your task is to understand what I did, and then work out how to modify your code so that you use std::endl when you want to, to end lines when you want to.
Last edited on
Topic archived. No new replies allowed.