469,927 Members | 1,984 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Formatting the Output Question. Thanks for the help

The goal of this project is to output the following information as follows:

34.5
38.6 4.1
42.4 3.8 close
46.8 4.4 big change.

The values of the first colunm are obtain from a file called: tempInput.txt, and then the information is calculated, and it is output on a different file called tempOutput.txt.

The tempInput.txt containg those numbers as follows:
34.5 38.6 42.4 46.8

I am able to obtain the correct output if I use the numbers already assigned. However, if I change the number on the tempInput.txt file, my output on the tempOutput.txt file is un aligned.

I have tried to twick it in different ways, but I am unable to the correct formatting output with any values of my choice.

I will appreciate any information that will allow me to fix the formatting output with what ever number I use on the tempInput.txt file.

The code I have so far is as follows:

Thank you again!!
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <cmath>
  4. #include <iomanip>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. int main(){
  10.  
  11.     // Input variables
  12.  
  13.     float tempValue1;
  14.     float tempValue2;
  15.     float tempValue3;
  16.     float tempValue4;
  17.     ifstream inData;
  18.     ofstream outData;
  19.  
  20.  
  21.     // Local Varialbles
  22.  
  23.     float tempDif1;
  24.     float tempDif2;
  25.     float tempDif3;
  26.  
  27.  
  28.  
  29.     inData.open("tempInput.txt");
  30.     outData.open("tempOutput.txt");
  31.  
  32.     // Read Values from the file
  33.  
  34.     inData >> tempValue1 >> tempValue2 >> tempValue3 >> tempValue4;
  35.  
  36.     // Calculate values
  37.  
  38.     tempDif1 = fabs(tempValue1 - tempValue2);
  39.     tempDif2 = fabs(tempValue2 - tempValue3);
  40.     tempDif3 = fabs(tempValue3 - tempValue4);
  41.  
  42.     // output results
  43.  
  44.     outData << fixed << showpoint << setprecision(1);
  45.     outData << setw(4) << tempValue1 << endl;
  46.  
  47.     outData << setw(4) << tempValue2 
  48.             << setw(8) << tempDif1;
  49.     if (tempDif1 < 4)
  50.         outData << setw(10) << "close" <<endl;
  51.     else if (tempDif1 > 4.3)
  52.         outData << setw(15) << "big change" << endl;
  53.  
  54.     outData << setw(4) << tempValue3
  55.             << setw(8) << tempDif2;
  56.     if(tempDif2 < 4)
  57.         outData << setw(10) << "close" << endl;
  58.     else if (tempDif2 > 4.3)
  59.         outData << setw(15) << "big change" << endl;
  60.  
  61.     outData << setw(4) << tempValue4
  62.             << setw(8) << tempDif3;
  63.     if(tempDif3 < 4) 
  64.          outData << setw(10) << "close" << endl;
  65.     else if (tempDif3 > 4.3 )
  66.          outData << setw(15) << "big change" << endl;
  67.  
  68.  
  69.     inData.close();
  70.     outData.close();
  71.  
  72.     return 0;
  73.  
  74. }
  75.  
Feb 13 '07 #1
6 1604
horace1
1,510 Expert 1GB
you have endl missing off
Expand|Select|Wrap|Line Numbers
  1.     outData << setw(4) << tempValue2 
  2.             << setw(8) << tempDif1 << endl;  // ** endl missing
  3.  
what data causes you problems?
remember setw() pads with fill characters if the number of characters required to represent the number is less than the field width. If more are required it expands the field width accordingly. Therefore you need to know the largest value that will be output to set the feild width using setw() and then organise the other lines of output to tabulate correctly.
Feb 13 '07 #2
AdrianH
1,251 Expert 1GB
you have endl missing off
Expand|Select|Wrap|Line Numbers
  1.     outData << setw(4) << tempValue2 
  2.             << setw(8) << tempDif1 << endl;  // ** endl missing
  3.  
what data causes you problems?
remember setw() pads with fill characters if the number of characters required to represent the number is less than the field width. If more are required it expands the field width accordingly. Therefore you need to know the largest value that will be output to set the feild width using setw() and then organise the other lines of output to tabulate correctly.
Man, I always found the C++ iostream library a pain in the ass to learn. I would have loved to have a resource like this forum when I was in University. That and some good documentation available.


Adrian
Feb 13 '07 #3
you have endl missing off
Expand|Select|Wrap|Line Numbers
  1.     outData << setw(4) << tempValue2 
  2.             << setw(8) << tempDif1 << endl;  // ** endl missing
  3.  
what data causes you problems?
remember setw() pads with fill characters if the number of characters required to represent the number is less than the field width. If more are required it expands the field width accordingly. Therefore you need to know the largest value that will be output to set the feild width using setw() and then organise the other lines of output to tabulate correctly.

Thanks for the input. As a matter of fact that is part of my problem. If I add the "endl;", and compile the program, and change one of the number from the tempInput.txt file, the output does not show correctly:

It shows like this:

34.5
40.6 6.1
big change
42.4 1.8 close
46.8 4.4 big change

I have already take into the account the right aligment that take effect depending on the variable that changes on the las colunm.

If I use the original data, it works fine, but if I change the numbers, it gets miss up.

If you have any other input, I will really appreciate it. Thanks again.
Feb 13 '07 #4
AdrianH
1,251 Expert 1GB
Thanks for the input. As a matter of fact that is part of my problem. If I add the "endl;", and compile the program, and change one of the number from the tempInput.txt file, the output does not show correctly:

It shows like this:

34.5
40.6 6.1
big change
42.4 1.8 close
46.8 4.4 big change

I have already take into the account the right aligment that take effect depending on the variable that changes on the las colunm.

If I use the original data, it works fine, but if I change the numbers, it gets miss up.

If you have any other input, I will really appreciate it. Thanks again.
Oh, it is because you should put the outData << endl; after each one of these blocks and remove the << endl from the following block too.
Expand|Select|Wrap|Line Numbers
  1.     outData << setw(4) << tempValue2 
  2.             << setw(8) << tempDif1;
  3.     if (tempDif1 < 4)
  4.         outData << setw(10) << "close" <<endl;
  5.     else if (tempDif1 > 4.3)
  6.         outData << setw(15) << "big change" << endl;
  7.  
so it should look like:
Expand|Select|Wrap|Line Numbers
  1.     outData << setw(4) << tempValue2 
  2.             << setw(8) << tempDif1;
  3.     if (tempDif1 < 4)
  4.         outData << setw(10) << "close";
  5.     else if (tempDif1 > 4.3)
  6.         outData << setw(15) << "big change";
  7.     outData << endl;
  8.  
Hope this helps,


Adrian
Feb 13 '07 #5
Hello,

Thanks for all your help.

I was finally able to figure this out.

I had to create two more additional conditions for the output that did not contain any data on the last column. This was cuasing some formating issue, but in reality it had to do with the conditions code.

Now, I can use whatever value on the tempInput.txt file, and the results, and the formatting look perfect.

This is how it works correctly:

if (tempDif1 < 4)
outData << setw(10) << "close" << endl;
else if (tempDif1 > 4.3)
outData << setw(15) << "big change" << endl;
else if (tempDif1 > 4)
outData << setw(10) << " " << endl;
else if(tempDif1 < 4.3)
outData << setw(15) << " " << endl;
Feb 14 '07 #6
AdrianH
1,251 Expert 1GB
Hello,

Thanks for all your help.

I was finally able to figure this out.

I had to create two more additional conditions for the output that did not contain any data on the last column. This was cuasing some formating issue, but in reality it had to do with the conditions code.

Now, I can use whatever value on the tempInput.txt file, and the results, and the formatting look perfect.

This is how it works correctly:

if (tempDif1 < 4)
outData << setw(10) << "close" << endl;
else if (tempDif1 > 4.3)
outData << setw(15) << "big change" << endl;
else if (tempDif1 > 4)
outData << setw(10) << " " << endl;
else if(tempDif1 < 4.3)
outData << setw(15) << " " << endl;
Hi,

Although what you did will work, is was unnecessary complex. Think about it this way. You have five regions:
Expand|Select|Wrap|Line Numbers
  1. number line indicating possible values of x:
  2.  
  3.                             Group 2                      Group 4
  4.                Group 1         |          Group 3           |        Group 5
  5. x < 4   <######################|                            |
  6. x > 4.3                        |                            |#######################>
  7.                                |                            |
  8. x <= 4 && x >= 4.3             ##############################    
  9. (i.e everything else)          |                            |
  10.         _______________________|______________ _____________|________________________
  11.                                4                           4.3
  12. Group 1 is x < 4
  13. Group 2 is x = 4
  14. Group 3 is 4 < x && x < 4.3
  15. Group 4 is x = 4.3
  16. Group 5 is 4.3 < x
  17.  
  1. less than 4 (Group 1)
  2. greater or equal to 4 but less or equal to 4.3 (Group 2, 3 and 4)
  3. greater than 4.3 (Group 5)
In each of these cases, you want (respectivly)
  1. print out "close"
  2. don't print anything
  3. print out "big change"

In every case, you want to have a endl to come out. I don't think you really care if there are a bunch of spaces after the line if you don’t print “close” or “big change”. So, all you need are the conditions that output “close” or “big change” excluding the endl and then at the end of it all, output an endl.

Now, let us take a look at what you wrote here. You have 4 conditions, two of which overlap the same region:

Expand|Select|Wrap|Line Numbers
  1. number line indicating possible values of x:
  2.  
  3.                           Group 2                     Group 4
  4.                Group 1       |          Group 3          |      Group 5
  5. x < 4   <####################|                           |
  6. x > 4.3                      |                           |###################>
  7. x > 4                        |###############################################>
  8. x < 4.3 <################################################|
  9.         _____________________|________________ __________|____________________
  10.                              4                          4.3
  11. Group 1 is x < 4
  12. Group 2 is x = 4
  13. Group 3 is 4 < x && x < 4.3
  14. Group 4 is x = 4.3
  15. Group 5 is 4.3 < x
  16.  
Your if statement will be executed sequencially, and once a true condition is found, the execution will stop looking for truths in the other else if statements and start at the next statement after them.

So, lets ‘execute’ this:
  1. Is tempDif1 < 4? If true, then you have found Group 1. So output “close” with endl. Start executing after if/else if statement.
  2. Otherwise is tempDif1 > 4.3? If true, then you have found Group 5. So output “big change” with endl. Start executing after if/else if statement.
  3. Otherwise, is tempDif1 > 4? If true, you have found Group 3, 4 and 5. Though you have already found group 5 in point 2 so in reality you have just found Group 3 and 4. So Output 10 spaces with endl. Start executing after if/else if statement.
  4. Otherwise, is tempDif1 < 4.3? If true, you have found Group 1, 2 and 3. But you have already found Group 1 in point 1, and Group 3 in point 3, so in reality you have just found Group 2. So output 15 spaces with endl. Start executing after if/else if statement.
Do you see what I am getting at? You can merge three of these groups (Groups 2, 3 and 4) into one, and not print any spaces.

If you insist, you can output an endl at the end of each “close” and “big change”, but in that case you should just have one else as a catchall to just output an endl. But it is unnecessary since when you find any of these groups (Group 1, 2, 3, 4 or 5) each require a endl. So you can put the endl after the if/elseif statement without having a final else statement.

I hope that I am not talking to the wind and you think about this. It is an important concept to grasp in computing theroy when generating good code.


Adrian

What I have just described is called group theroy, which is the analysis of grouping sets (stuff) in order to find set intersections (commonalities or overlapping sections) or set disjunctions (inverse intersections where there is no overlapping area), and other interesting things and can be applied to writing efficent, effective and less buggy computer programmes.
Feb 14 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by shoo | last post: by
6 posts views Thread by shoo | last post: by
7 posts views Thread by BBFrost | last post: by
8 posts views Thread by Vinay Jain | last post: by
6 posts views Thread by Glen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.