Programming C++ Nublet

OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
edited September 2003 in Off-Topic
The assignment is to make a program to calculate the input length and width to area and perimeter, I got that, but my professor also said they wanted it to go out two decimal spots and for it to line up, but I got no clue how to do this.

<!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->
#define __USE_STD_IOSTREAM
#include <iostream.h>
#include <iomanip.h>


int main(void)

{

//Declarations
float length;
float width;
float area;
float perim;


//Request inputs
cout << "This program is written by Aaron Friedley" << endl
      << "for the MWF 10:00 Computer Science class." << endl
      << "This program takes two input numbers and calculates "
      << "area and perimeter" << endl << endl;
cout << "Enter length: ";
cin >> length;
cout << "Enter width: ";
cin >> width;


//Formulas
area = length * width;
perim = (length * 2) + (width * 2);


//Display perimeter and area
cout << endl << endl << "A rectangle with "
      << width << " width and "
      << length << " length yields:" << endl
      << "Area: " << setfill(' ') << setw(10) << area << endl
      << "Perimeter: " << setfill(' ') << setw(10) << perim
      << endl << endl;


return 0; //Terminate main

} <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->

*Edit*

I tried doing it with the setw but with no luck.

Comments

  • Marik_SteeleMarik_Steele To rule in hell... Join Date: 2002-11-20 Member: 9466Members
    edited September 2003
    By "going out two decimal spots," do you mean restricting the output to two places after the decimal? (Example: computer uses formula and gets answer of #.########, and is then told to only output #.##)

    [edit]Found an example of the manipulator function I was thinking of, <span style='font-family:Courier'>setPrecision()</span>. My personal experience (and the book I'm reading this out of) uses Doubles rather than Floats, but both count as floating-point numbers, so you should be fine.

    Just so see an example of how setprecision works, go ahead and add these two lines towards the end of your program (obviously not at the VERY end...still have to make use of return 0 after all):
    <!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->cout << "Area: " << area << endl;
    cout << "Area: " << setprecision([COLOR=orange]any reasonably small integer of your choice[/COLOR]) << area << endl;<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

    Keep compiling and running the program to see how the output changes when you change that integer. Remember that due to limitations inherent in the built-in types <span style='font-family:Courier'>int, double,</span> and <span style='font-family:Courier'>float</span>, you can't have an infinite number of #'s after the decimal anyway, so don't expect the program to act normally if you put a ridiculously large integer.

    I'm currently looking up how to use this in conjunction with <span style='font-family:Courier'>setw()</span>, but for now just try getting the decimal stuff to work. If you're lucky you may not even need setw after all.

    [edit2]I also noticed you have the header for the main function saying <span style='font-family:Courier'>int main(void)</span>. Having the return type of int makes sense because you're making use of the <span style='font-family:Courier'>return 0;</span> at the end, but I've never made a function--main or not--that has (void) alone in the parentheses. Normally if I have nothing to put in the parentheses (you'll learn later when to do that if you haven't learned already) I've just put () completely empty. If it compiles and runs correctly, C++ is more tolerant of that than I would've expected.
  • HawkeyeHawkeye Join Date: 2002-10-31 Member: 1855Members
    edited September 2003
    println("This is my float %f.2", myfloat);

    The "%f" means that's where a float should go. The .2 following is a format detail which means restrict to 2 decimal places.
    This is a C command, but if your professor isn't too picky, this would work with C++ too.
  • minskminsk Join Date: 2003-01-09 Member: 12077Members
    edited September 2003
    <!--QuoteBegin--Marik_Steele+Sep 17 2003, 05:48 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Marik_Steele @ Sep 17 2003, 05:48 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> I also noticed you have the header for the main function saying <span style='font-family:Courier'>int main(void)</span>.  Having the return type of int makes sense because you're making use of the <span style='font-family:Courier'>return 0;</span> at the end, but I've never made a function--main or not--that has (void) alone in the parentheses.  Normally if I have nothing to put in the parentheses (you'll learn later when to do that if you haven't learned already) I've just put () completely empty.  If it compiles and runs correctly, C++ is more tolerant of that than I would've expected. <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    Not my fault, spent too much time in comp.lang.c...

    Traditional (not quite archaic, but close) C allowed you to make a forward declaration to a function of as-yet-undefined parameters using an empty parameter list. Obviously a declaration of a function that took no parameters required something else -- hence void in the parameter list. And hence (in the wonders of backward compatibility) C++ supports it...

    The only case you should see a difference in is if a function is declared with empty parameters (like in a header, without a definition). Then C will let you call it with arbitrary parameters, but C++ will require an empty parameter list.

    At least I belive, as usual I could be wrong.

    int printf();
    int main(void) {
    printf("Don't try this at home folks.\n");
    }

    [edit]Poofreading[/edit]
  • JavertJavert Join Date: 2003-04-30 Member: 15954Members
    Use setw( ) to numbers up like a table.
  • BurncycleBurncycle Join Date: 2002-11-24 Member: 9759Members, NS1 Playtester
    edited September 2003
    This was the "wonder line" we used every time we wanted to see decimal places.

    Just stick it somewhere above your cout

    cout << setprecision(2) << setiosflags( ios::fixed | ios::showpoint) << endl;

    Try it out, it should put a decimal and display 2 of the digits afterwards.

    If you only want certain answers to have the decimal, you have to be more careful where you put that line of code.

    Also, your coding style is whack (your martial arts is whack! lol, j/p). We use "double" instead of "float".... guess I'm just strange like that...




    As for lining it up, thats trial and error. This was something similar to what we used to do-

    <!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    cout << "Amount of cookies:   "  << cookies << endl;
    cout << "Cost of Cookies:       "  << cookiecost << endl;
    cout << "Taste of cookies:       " << cookietaste << endl;<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

    Notice how, while INSIDE the quotes, I put spaces after each colon so that the end quotation marks are lined up (well, close)? This will put spaces after the colon before displaying your variable. Hopefully the answeres will line up, even though your statements were different lengths. It all depends on where the ending quote lies, since the answer is displayed just to the right of the endquote. Heres a more detailed version showing how this works- Lets assume ans = 48.

    <!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->//If you do this:
    cout << "answer:" << ans << endl;

    //This will display:
    answer:48                            //notice no space?

    //However if you put spaces after the quote, like so:
    cout << "answer:   " << ans << endl;

    //This will display:
    answer:   48                      //spacey!<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

    See?

    Thats the quick and dirty method. For more complex things like tables, you should brush up on using the setw(x), where x is the number of spaces you want.
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    Die, printf()! *bash* *bash* *bash*

    I haven't used it myself, but I think the best solution is the .width() function, which is a member of (among other things), cout.

    Here's the example code from the book I've got:
    <!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    cout.width(7);
    cout.precision(1);
    cout << 1.0 << endl;
    cout << 22.53 << endl;
    cout << 100.2 << endl;

    //The output of this code is:

       1.0
     22.5
    100.2
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

    Bleah. It's not showing up properly, but imagine that the rightmost digits of those numbers are all perfectly lined up.
Sign In or Register to comment.