473,388 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 1859
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

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

Similar topics

6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
8
by: Vinay Jain | last post by:
Hi.. I am newbe in postgresql so please help me though the question may be very easy to answer.. Is there any formatting function to get output with fix lengths..for example my query is.. schema...
9
by: sck10 | last post by:
Hello, I have a page with an ImageButton that is used to redirect to another page. When the page first opens, everything looks as expected. However, when I click on the image, the new page...
6
by: Glen | last post by:
Hello again, I don't blame anyone for not answering my last post, since I obviously hadn't spent much time researching, but I've come a little ways and have another question. How can I better...
8
by: SMERSH009 | last post by:
Hi All. Let's say I have some badly formatted text called doc: doc= """ friendid Female 23 years old
10
by: sara | last post by:
Hi - I have a report that is 14 columnar sub-reports (Line up: Position- holders in each of our 14 locations - Manager, Assistant Manager, Receiving, Office, etc). I output directly to PDF...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.