Formatting the Output Question. Thanks for the help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rafael Olarte
    New Member
    • Jan 2007
    • 14

    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!!
    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    int main(){
    
    	// Input variables
    
    	float tempValue1;
    	float tempValue2;
    	float tempValue3;
    	float tempValue4;
    	ifstream inData;
    	ofstream outData;
    
    
    	// Local Varialbles
    
    	float tempDif1;
    	float tempDif2;
    	float tempDif3;
    
    
    
    	inData.open("tempInput.txt");
    	outData.open("tempOutput.txt");
    
    	// Read Values from the file
    
    	inData >> tempValue1 >> tempValue2 >> tempValue3 >> tempValue4;
    
    	// Calculate values
    
    	tempDif1 = fabs(tempValue1 - tempValue2);
    	tempDif2 = fabs(tempValue2 - tempValue3);
    	tempDif3 = fabs(tempValue3 - tempValue4);
    
    	// output results
    
    	outData << fixed << showpoint << setprecision(1);
    	outData << setw(4) << tempValue1 << endl;
    	
    	outData << setw(4) << tempValue2 
    	        << setw(8) << tempDif1;
    	if (tempDif1 < 4)
    		outData << setw(10) << "close" <<endl;
    	else if (tempDif1 > 4.3)
    		outData << setw(15) << "big change" << endl;
    
    	outData << setw(4) << tempValue3
    	        << setw(8) << tempDif2;
    	if(tempDif2 < 4)
    		outData << setw(10) << "close" << endl;
    	else if (tempDif2 > 4.3)
    		outData << setw(15) << "big change" << endl;
    	
    	outData << setw(4) << tempValue4
    	        << setw(8) << tempDif3;
    	if(tempDif3 < 4) 
    		 outData << setw(10) << "close" << endl;
    	else if (tempDif3 > 4.3 )
    		 outData << setw(15) << "big change" << endl;
    	
    	
    	inData.close();
    	outData.close();
    
    	return 0;
    
    }
    Last edited by sicarie; Feb 13 '07, 03:30 PM. Reason: Added code tags.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you have endl missing off
    Code:
    	outData << setw(4) << tempValue2 
    	        << setw(8) << tempDif1 << endl;  // ** endl missing
    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.

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by horace1
      you have endl missing off
      Code:
      	outData << setw(4) << tempValue2 
      	        << setw(8) << tempDif1 << endl;  // ** endl missing
      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

      Comment

      • Rafael Olarte
        New Member
        • Jan 2007
        • 14

        #4
        Originally posted by horace1
        you have endl missing off
        Code:
        	outData << setw(4) << tempValue2 
        	        << setw(8) << tempDif1 << endl;  // ** endl missing
        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.

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          Originally posted by Rafael Olarte
          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.
          Code:
          	outData << setw(4) << tempValue2 
          	        << setw(8) << tempDif1;
          	if (tempDif1 < 4)
          		outData << setw(10) << "close" <<endl;
          	else if (tempDif1 > 4.3)
          		outData << setw(15) << "big change" << endl;
          so it should look like:
          Code:
          	outData << setw(4) << tempValue2 
          	        << setw(8) << tempDif1;
          	if (tempDif1 < 4)
          		outData << setw(10) << "close";
          	else if (tempDif1 > 4.3)
          		outData << setw(15) << "big change";
          	outData << endl;
          Hope this helps,


          Adrian

          Comment

          • Rafael Olarte
            New Member
            • Jan 2007
            • 14

            #6
            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;

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by Rafael Olarte
              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:
              Code:
              number line indicating possible values of x:
              
                                          Group 2                      Group 4
                             Group 1         |          Group 3           |        Group 5
              x < 4   <######################|                            |
              x > 4.3                        |                            |#######################>
                                             |                            |
              x <= 4 && x >= 4.3             ##############################    
              (i.e everything else)          |                            |
                      _______________________|______________ _____________|________________________
                                             4                           4.3
              Group 1 is x < 4
              Group 2 is x = 4
              Group 3 is 4 < x && x < 4.3
              Group 4 is x = 4.3
              Group 5 is 4.3 < x
              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:

              Code:
              number line indicating possible values of x:
              
                                        Group 2                     Group 4
                             Group 1       |          Group 3          |      Group 5
              x < 4   <####################|                           |
              x > 4.3                      |                           |###################>
              x > 4                        |###############################################>
              x < 4.3 <################################################|
                      _____________________|________________ __________|____________________
                                           4                          4.3
              Group 1 is x < 4
              Group 2 is x = 4
              Group 3 is 4 < x && x < 4.3
              Group 4 is x = 4.3
              Group 5 is 4.3 < x
              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.

              Comment

              Working...