that how i had it , and it works kindof
i think that is all that matters when coding
that how i had it , and it works kindof
i think that is all that matters when coding
It's far cry from what you had.
And you are dead wrong if all you think that matters is working.
Readability is first and foremost important.
And second is semantically correct, not just something that "works" (it shall not be undefined behavior).
If you fail to follow these principles, then you will become another esbo. A pitiful programmer who is full of bad practices and creates poor, unmaintainable code.
you cant just make up names. that's unfair
i want to make a name now.
but really my if statements
they are not right are they.Code:if (qual[0]<=150) if (qual[1]<=250) if (qual[2]<=115) if (qual[3]<=435) if (qual[4]<=250)
I didn't make the name up.
And yes, that works, if you initialize those variables with something first.
like this
Code:float qual[4];
That (a) initializes nothing at all and (b) makes sure you don't have an element called "qual[4]". If you need an element called "qual[4]", then you need to have five elements total, which means you need to declare your array to have five elements.
(Edit: And presumably, in your actual program, you wouldn't initialize these elements, so much as read them in from the user.)
if (qual[0]<=150.0f)
if (qual[1]<=250.0f)
if (qual[2]<=115.0f)
if (qual[3]<=435.0f)
And what you posted is the definition of the array, not the initialization.
The variables must be assigned some value before you test them. If you do that, then yes, it works.
so to initialize them i would go
part[0]=part1
part[1]=part2
because im reading the quality from a txt file and im going to use it in my statement saying that you have to reorder something.
idk i just want it to read it and if it is less print out that part number along with a total .
or do you think i should let the other program use the if statement and print it out on the txt file and then i just read that(assuming it prints to the txt file meeting the requirments).Code:#include<iostream> #include<string> #include<fstream> using namespace std; using std::string; float total; char part[4]; float qual[4]; float cost[5]; int main(){ ifstream file; file.open("Part.txt"); if (qual[0]<=150){ cout<<"Reorder 200 of part "<<part[0]<<endl; total=cost[0]*200; cout<<"Total reorder cost:$"<<total<<endl;} if (qual[1]<=250){ cout<<"Reorder 300 of part "<<part[1]<<endl; total=cost[1]*300; cout<<"Total reorder cost:$"<<total<<endl;} if (qual[2]<=115){ cout<<"Reorder 150 of part "<<part[2]<<endl; total=cost[2]*150; cout<<"Total reorder cost:$"<<total<<endl;} if (qual[3]<=435){ cout<<"Reorder 500 of part "<<part[3]<<endl; total=cost[3]*500; cout<<"Total reorder cost:$"<<total<<endl;} if (qual[4]<=250){ cout<<"Reorder 750 of part "<<part[4]<<endl; total=cost[4]*750; cout<<"Total reorder cost:$"<<total<<endl;} file.close(); system("pause"); return 0; }
Last edited by rfoor; 02-13-2009 at 09:48 AM.
No, let's keep that out of the file.
So read in the data and make it work.
But first, you still haven't fixed the indentation.
And another problem is that you are comparing a float to an integer:
if (qual[3]<=435)
A floating point number is represented with the addition of .0f, so it should be:
if (qual[3]<=435.0f)
Your current code doesn't read anything from the file...
AND for the 5th time from me alon: This is using an element that doesn't exist in qual array:
I would also suggest that you put the end-brace on the line after cout, rather than at the end of that line - it makes it much easier to "find" when you are scanning through the code.Code:if (qual[4]<=250){ cout<<"Reorder 750 of part "<<part[4]<<endl; total=cost[4]*750; cout<<"Total reorder cost:$"<<total<<endl;}
--
Mats
Last edited by matsp; 02-13-2009 at 09:58 AM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Maybe what you need is an overall clear picture of how the array numbering system works:
This does not give you index #4. It gives you 4 indices total. Which are numbered 0, 1, 2, and 3.Code:int MySampleArray[4];
...and
does not read any content of a file no more than double clicking a .txt file on your desktop would make you learn the contents. It simply opens it. You have to read it if you want to know what is in it, AFTER you open it. Read ThisCode:file.open