Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Redirect output to txt file on multiple lines

#include<stdio.h>  

int main()   
{   
  int n, count = 1;   
  float x, average, sum = 0;   
  printf("How many numbers do you wish to test: ");  
  scanf ("%d",&n);   
  while (count <= n)   
     {   
      printf ("Enter number #%d: ",count);   
      scanf("%f", &x);   
      sum += x;   
      ++count;   
     }   
    average = sum/n;   
    printf("\nThe Average is: %.2f\n", average);   
}

This program asks the user for a certain amount of numbers to be entered and then calculates the average of those numbers. I then use a input.txt file to redirect the input and direct to an output.txt file. So using the command

./main < input.txt > output.txt

My question is, how do I make it so when the output is redirected, the output appears on multiple lines instead of all in one single line. For example, when I run the command and look at my output.txt file it looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How many numbers do you wish to test: Enter number #1: Enter number
#2: Enter number #3: Enter number #4: Enter number #5: The Average is: 6.00

Is there a way to make it look like this instead:

How many numbers do you wish to test:
Enter number #1:
Enter number #2:
Enter number #3:
Enter number #4:
Enter number #5:
The Average is: 6.00

>Solution :

The only line break you output is at the very end of your code. If you want line breaks in the output, output them wherever you want them, just as you did in the last line of code.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading