Another C++ Question

OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
<div class="IPBDescription">Or two</div> Can you use a variable in an equation and make the result of that the same variable? Er.. let me give example of what I'm doing...

<!--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-->
TV * 400 = TV
VCR * 220 = VCR
<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

That and how do I make an input text bold and italics.

Comments

  • RuByRuBy Join Date: 2002-12-12 Member: 10732Members
    in Java you can... not sure about C++ though <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->
  • NumbersNotFoundNumbersNotFound Join Date: 2002-11-07 Member: 7556Members
    Sure can.. Would be kinda silly if you couldn't.. as that's the basis for counter and accumulators.

    Count=Count+1;

    Acc=Acc+Result;

    etc.

    You just have it reversed.

    (for brownies points, do tv*=400; or vcr*=220; it's also against convention to have variables which aren't constants in all caps)
  • CreepieCreepie Join Date: 2003-02-19 Member: 13734Members
    <!--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-->it's also against convention to have variables which aren't constants in all caps<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->Hehe - I wouldn't go saying anything about convention - it'll all go Pete Tong - you know it <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo-->
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    while you're talking about operators the -- and ++ actually act differently depending if you put them before or after the variable =3

    if you do var++ it uses it then adds 1, if you do ++var it adds 1 before using the variable (this actually saves a little bit of memory)
    so if you did a for loop with (var=0; var < whatever; ++var) var would equal 1 on the first run of the loop instead of 0.

    Don't know why but I thought I'd bring that up o.O

    remember otto, equals first ^^
    it's not maths; it's programming lol
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    edited September 2003
    Yea I know its annoying as crap. And this program is gonna be messy as all hell. Heres what I got so far

    <!--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-->
    // Program by Aaron Friedley
    // September 27, 2003
    // Takes input numbers for a register and calculates cost

    #include <iostream.h>
    #include <iomanip.h>

    int main(void)

    {


    // Declarations
    int TV = 0;
    int VCR = 0;
    int CTRLR = 0;
    int CDS = 0;
    int RCRDR = 0;
    float subtotal = 0;
    float sales_tax = 0;
    float total = 0;
    int TVcost = 0;
    int VCRcost = 0;
    int CTRLRcost = 0;
    int CDScost = 0;
    int RCRDRcost = 0;

    // Output / Input
    cout << "Program by Aaron Friedley MWF 10:00"
         << "\n\nHow many TVs were sold? ";
    cin >> TV;
    cout << "\nHow many VCRs were sold? ";
    cin >> VCR;
    cout << "\nHow many remote controls were sold? ";
    cin >> CTRLR;
    cout << "\nHow many CDs were sold? ";
    cin >> CDS;
    cout << "\nHow many tape recorders were sold? ";
    cin >> RCRDR;
    cout << "\n\n";

    // Calculations
    TVcost = TV * 400;
    VCRcost = VCR * 220;
    CTRLRcost = CTRLR * 35.20;
    CDScost = CDS * 300;
    RCRDRcost = RCRDR * 150;
    subtotal = TVcost + VCRcost + CTRLRcost + CDScost + RCRDRcost;
    sales_tax = subtotal * .825;
    total = subtotal + sales_tax;

    // Output
    cout << "QTY  DESCRIPTION    UNIT PRICE   TOTAL PRICE\n"
         << "---  -----------    ----------   -----------\n"
         << TV << setw(3) << "     TV 400.00" << TVcost << "\n"
         << VCR << setw(3) << "     VCR 220.00" << VCRcost << "\n"
         << CTRLR << setw(3) <<"     REMOTE CTRLR 35.20" << CTRLRcost << "\n"
         << CDS << setw(3) << "     CD PLAYER 300.00" << CDScost << "\n"
         << RCRDR << setw(3) << "     TAPE RECORDER 150.00"<< RCRDRcost << "\n"
         << setw(50) << "-----------\n"
         << "SUBTOTAL " << setfill('*') << setw(7) << subtotal << "\n"
         << "TAX" << setfill(' ') << setw(15) << sales_tax << "\n"
         << "TOTAL" << setw(13) << total << "\n\n";

    return 0; // Terminate main
    }
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

    I have to fix all the spaces in the output, make it so that total price displays decimals to two places even if its .00 (which I'm still not sure how to do *cough*), and uhh. Er thats it. But still I'm having trouble right now getting the quantity columns to line up if theres different values inside quantity... Er.. it will look like this:

    <!--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-->
    QTY
    1      TV
    10       VCR
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

    *Edit*

    And now its **** me off because I tried to use the following to get the decimals
    <!--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 << showpoint << fixed << setprecision(2);
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
    And its telling me this when I try to compile:
    <!--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-->
    cxx: Error: hwk05.cxx, line 54: identifier "showpoint" is undefined
           cout << showpoint << fixed << setprecision(2)
    ----------------^
    cxx: Error: hwk05.cxx, line 54: identifier "fixed" is undefined
           cout << showpoint << fixed << setprecision(2)
    -----------------------------^
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    ~sighs and stretches~

    this is one of those things where you know the answer but can't quite remember it... I hate it when that happens lol
    um... %f2.. nuh-uh... gah... I know how to set the floating point numbers I just can't remember it right now =/
  • UltimaGeckoUltimaGecko hates endnotes Join Date: 2003-05-14 Member: 16320Members
    This doesn't really have to do with that (I think it might be setpoint instead of showpoint, you might also not have the right header file, but I was never very good with those. The one I had to make did almost nothing). I don't have my old program printouts here... They're stacked up in my closet at home.

    I haven't done C++ in two years, but why don't you use (no actual effect of a program, but...oh well):

    void main()

    instead of:

    int main (void)

    Maybe it's just how we were taught. Makes me want to learn C++ all over again! *cry*. Just throwing out stuff that probably doesn't help at all.
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Yea well thats how our professor wants it. Anyways I'm still trying to get the fixed and showpoint crap to work, plus I realized that I have to isolate it to only the output for the total prices, not quantity (don't want decimal on the quantity). My head hurts <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif'><!--endemo-->
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Okie bumpage. I got everything fixed now. But how do you do a bold / italics on text.
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
    <!--QuoteBegin--OttoDestruct+Sep 29 2003, 12:29 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Sep 29 2003, 12:29 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->But how do you do a bold / italics on text.<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    If the assignment doesn't explicitly ask for bold/italicized text, I'd say leave it out.
    Otherwise, use the setf() method.
    This method lets you set format flags. According to my C++ book, format flags are implementation-specific.
    If you want to know which flags control the "bold" and "italicized" attributes, consult the documentation for your compiler.
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    edited September 2003
    <!--QuoteBegin--OttoDestruct+Sep 29 2003, 02:41 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Sep 29 2003, 02:41 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Can you use a variable in an equation and make the result of that the same variable? Er.. let me give example of what I'm doing...

    <!--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-->
    TV * 400 = TV
    VCR * 220 = VCR
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

    <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    Of course.

    <!--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-->TV *= 400
    VCR *= 220
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

    I would have thought they'd have taught you this when they taught the assignment operator. Oh well.

    <!--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-->That and how do I make an input text bold and italics.<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    Short answer: you can't.

    Long answer: I assume you're working with console applications; in that case the font is always the same. If you *aren't* working with console applications then it depends completely on the API you're using, and I can't help.

    [edit]While we're at it, "format flags" do NOT control font. They control how numbers are displayed: what radix, what alignment etc.[/edit]

    What do you want it for, anyway?
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    <!--QuoteBegin--UltimaGecko+Sep 29 2003, 04:10 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (UltimaGecko @ Sep 29 2003, 04:10 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> I haven't done C++ in two years, but why don't you use (no actual effect of a program, but...oh well):

    void main()

    instead of:

    int main (void) <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    EVIL! DIE!

    The first version is non-standard. It's just some bull[dung] supported by Microsoft Visual C++. Any good textbook will tell you to use int main().

    By the way, declaring a parameter list as void is a C idiom which is deprecated in C++. That means it's now considered to be unnecessary.
  • DOOManiacDOOManiac Worst. Critic. Ever. Join Date: 2002-04-17 Member: 462Members, NS1 Playtester
    so is putting .h at the end of standard STL includes too.

    i.e. it should be:<!--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-->#include <iostream><!--c2--></td></tr></table><span class='postcolor'><!--ec2--> instead of <!--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-->#include<iostream.h><!--c2--></td></tr></table><span class='postcolor'><!--ec2--> or <!--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-->#include "iostream.h"<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

    Again, just a style thing for now. but if its is deprecated that means it'll go away sometime later and then you'll just get a syntax error and have to figure out why :P
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    Well, it's not *as* evil. <span style='font-family:Courier'>void main()</span> is not standard, <span style='font-family:Courier'>#include <iostream.h></span> kinda still is but is deprecated. I think compilers that want to be standards compilant still have to support <span style='font-family:Courier'>.h</span>, but generate a warning message.
Sign In or Register to comment.