Another C++ Question
OttoDestruct
Join Date: 2002-11-08 Member: 7790Members

in Off-Topic
<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.
<!--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
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)
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
<!--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-->
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 =/
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.
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.
<!--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?
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.
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