"display formatting"

This is a discussion on "display formatting" within the C Programming forums, part of the General Programming Boards category; I've written a program that is supposed to calculate pressure at different volumes. the volume increases at constant intervals. the ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    Question "display formatting"

    I've written a program that is supposed to calculate pressure at different volumes. the volume increases at constant intervals. the program is supposed to display to the user (it displays in the command prompt) two columns of values, one for the different volumes, the other for the different pressures. How can i edit my code so that it formats the display i want the user to see in those two columns? is it even possible to do that? any input would be greatly appreciated. the program5 ccp is attatched.
    Attached Files Attached Files
    Peace

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Newlines would be a good start, then just tailor the justification to meet the needs of your columns:
    Code:
    void Pressure(double initial_volume,double CO2, double temp, double volume_increment, double final_volume )
    {
      double b= 0.0427;
      double a= 3.592;
      double R= 0.08206;
      double pressure = 0;
      double index= initial_volume;
    
      printf("Volume (ml)\tPressure (atm)\n");
      for (index=initial_volume; index<=final_volume; index+=volume_increment)
      {
        pressure= (CO2*R*temp/(index)-b*CO2)-(a*(CO2*CO2)/(index*index));
        printf("%11.2lf\t%14.2lf\n", initial_volume, pressure);
      }
      if (initial_volume>final_volume)
        pressure= (CO2*R*temp/(final_volume)-b*CO2)-(a*(CO2*CO2)/(final_volume*final_volume));
      printf("%11.2lf\t%14.2lf\n", final_volume, pressure);
    }
    Whenever possible, avoid using spaces as field formatters, they generally cause trouble when mixed with printf's formatting widths. And if you're using C, compile as C. There are times when compiling as C++ (as you're doing with that .cpp file) will result in errors even though the code is valid C.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Try something like this:

    Code:
        printf("Volume (ml)        Pressure (atm)\n");
        for (index=initial_volume; index<=final_volume; index+=volume_increment)
        {
    
            pressure= (CO2*R*temp/(index)-b*CO2)-(a*(CO2*CO2)/(index*index));
            printf("  %.2lf               %.2lf\n", index, pressure);
    
        }
        if (initial_volume>final_volume)
        
    
            pressure= (CO2*R*temp/(final_volume)-b*CO2)-(a*(CO2*CO2)/(final_volume*final_volume));
            printf("  %.2lf               %.2lf\n", final_volume, pressure);
    Note that I removed tabs from your printf() strings. Put in enough spaces so that it looks good to you. (Then if you want to, say, move the whole display a couple of spaces to the right, simply add a couple of spaces at the start of each output string.)

    Note that I changed the printf() argument inside the loop to index, so that the correct output is presented.

    Dave

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    your function Pressure looks to have no return type - if it has no return type make it void:
    Code:
    void Pressure(....)
    {
    ......
    }
    ~/

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    thank you

    Thank you very much for your help, it seems to be ligning up the values in two columns... i've attatched what the program looks like. the program is calculating something, but it is not the desired outcome... i'll keep trying to fix this, any further tips would also be appreciated. Thank you
    Attached Images Attached Images  
    Peace

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but it is not the desired outcome
    You haven't made all of the suggested changes judging from your output.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Originally posted by Prelude
    >but it is not the desired outcome
    You haven't made all of the suggested changes judging from your output.
    what am i missing? i followed your suggestion, i don't know how to not compile it on C, i'm using microsoft visual studio... i just click the build button. maybe the equation itself is wrong... ???
    Peace

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what am i missing?
    Well, the header and the first line of data suggests you forgot to print a newline at the end of the header. Post the code you're using now.

    >i don't know how to not compile it on C
    Save the file as .c instead of .cpp.

    >maybe the equation itself is wrong... ???
    Are the values not what you expected? I'm referring only to the formatting.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by camagitoe
    what am i missing?
    Besides missing the '\n' in the printf() for the column header, note the following:


    Change the actual format any way you want to, but inside the for(){} loop:
    Code:
            printf("  %.2lf               %.2lf\n", index, pressure);
    Note that you should use index not initial_volume in the printf().

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Originally posted by Prelude
    >
    >maybe the equation itself is wrong... ???
    Are the values not what you expected? I'm referring only to the formatting.
    \
    yeah, the values are wrong... thanks about the formatting.
    Peace

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    i corrected the index, in the for loop, thanks dave.

    i'm confused about this tho.
    Besides missing the '\n' in the printf() for the column header
    Attached Files Attached Files
    Peace

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    /\ /\ /\ /\

    that's the updated program by the way
    Peace

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Ah, you beat me to it, and now you have a width with your format specifiers. As far as what was meant about the newline with your column header, you already fixed it:

    Code:
    printf("Volume (ml)\tPressure (atm)\n");
    ~/

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by camagitoe
    i corrected the index, in the for loop, thanks dave.

    i'm confused about this tho.
    index is the value of volume that you are using at each pass through the loop.

    It is set initially to initial_volume, then after each pass through the loop, it is incremented by volume_increment. The loop terminates when index is no longer <= final_volume.

    Dave
    Last edited by Dave Evans; 03-21-2004 at 08:40 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21