No, you are not. You are trying to store input into an array, without specifying WHERE in the array you want to store it, and you are especially not writing to the file.
And you really MUST fix your indentation!
No, you are not. You are trying to store input into an array, without specifying WHERE in the array you want to store it, and you are especially not writing to the file.
And you really MUST fix your indentation!
Does that compile?
Assuming you were reading into part[i], then your loop goes from 1 to 5, which is outside the range of 0..3 that part[4] allows you to use.
--
Mats
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.
both of them do compile and it goes 0(1),1(2),2(3),3(4),4(5) so there are 5 inputs right, ?
should i do something different when i write it to the file?
Code:for(x=1;x<=5;x++){ cout<<"Part Number:"; cin>>part; file<<part<<endl; cout<<"Part Description:"; cin>>des; file<<des<<endl; cout<<"Quality on Hand:"; cin>>qual; file<<qual<<endl; cout<<"Unit Cost:"; cin>>cost; file<<cost<<endl;}
After looking at it again, I realize why it compiles: You are reading a string - char part[4] is a string in C (and thus C++). So you are not actually reading into an array (in the general meaning of array - strictly speaking, C style strings are of course arrays, but that's not what any of use meant when we said "use an array"). Of course, in this case, you are just outputting what you input, so it doesn't really matter how you read it in - using arrays would not really mean much in THIS case - as long as you don't enter anything longer than 3 digits or letters, as the array is 4 characters long, and you need a zero-character at the end of the string.
And as to the index of an array with n elements: You write that as(T is any valid type, n is some integer constant type). You then have valid valuse for [x] in the range 0..n-1. SoCode:T arr[n]will have four elements, with the index 0, 1, 2 and 3. Note that what you write in the declaration of the array is THE NUMBER OF ELEMENTS, and then use the indices up to one less than the number of elements.Code:int array[4];
--
Mats
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.
how would i get the data to be input into the array with a loop on it
Code:#include<iostream> #include<string> #include<fstream> using namespace std; using std::string; int main(){ int array1[4]; int array2[4]; int array3[4]; int array4[4]; int x; fstream file("Part.txt",ios::out); for(x=1;x<=5;x++){ cout<<"Part Number:"; cin>>array1; file<<array1<<endl; cout<<"Part Description:"; cin>>array2; file<<array2<<endl; cout<<"Quality on Hand:"; cin>>array3; file<<array3<<endl; cout<<"Unit Cost:"; cin>>array4; file<<array4<<endl;} file.close(); system("pause"); return 0; }
And you still have a loop that goes TWO places outside of the array, if you use [i] as the index when reading the input. This is because you start on 1 and end on 5, whilst your array has elements 0..3.
--
Mats
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.
ok my program crashed
here is the data im supposed to use:
Part Number: XL101, XM215, XR218, XG321, XK178
Part Description: Grommet, Chuck, Lathe, Lug, Appeture
Quantity on hand: 125, 254, 111, 654, 235
Reorder Point (when the quantity on hand reaches or falls below this amount): 150, 250, 115, 435, 250
Reorder Quantity: 200, 300, 150, 500, 750
Unit cost: 1.34, 1.77, 4.23, 6.12, 1.54
and heres my code that crashes
Code:#include<iostream> #include<string> #include<fstream> using namespace std; using std::string; int main(){ char array1[100]; int n; char array2[200]; float array3[50]; float array4[50]; int x; fstream file("Part.txt",ios::out); for(x=1;x<=5;x++){ cout<<"Part Number:"; cin>>array1[n]; file<<array1[n]<<endl; cout<<"Part Description:"; cin>>array2[n]; file<<array2[n]<<endl; cout<<"Quality on Hand:"; cin>>array3[n]; file<<array3[n]<<endl; cout<<"Unit Cost:"; cin>>array4[n]; file<<array4[n]<<endl;} file.close(); system("pause"); return 0; }
But you are not listening to what we say!
You have YET to fix the indentation.
And you have YET to fix the loop itself!
Go back and read the previous replies before you do anything more, and before you reply, fix everything that the previous replies mentioned.
After you've done that, I will tell you what's wrong with the code.
So, first of all 'n' that Elysia talks about is "a number indicating which element in the array you are referring to". In your case, n is a new variable, and it's got no value at all - bad idea. You probably want to use i, rather than n.
Second, part is supposed to be a text string, which is an array of char in itself.
Third. why are you using anonymous names such as array1 and array2 - much better to call them something that reflect what the actual content is.
Fourth, what is the purpose of loading the values in an array when you are just storing it to write out again?
--
Mats
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.
im storing it in a txt file so i can read it with another program.
as i said earlier if it a part quality is below the reorder point it will write a line that says you must reorder that part now!
how would i type that if statement?
i was thinking something like this
any suggestions?Code:if (qual[0]<=150){ cout<<"Reorder 200 of part "<<part<<endl; total=cost[0]*200; cout<<"Total reorder cost:$"<<total<<endl;} if (qual[1]<=250){ cout<<"Reorder 300 of part "<<part<<endl; total=cost[1]*300; cout<<"Total reorder cost:$"<<total<<endl;} if (qual[2]<=115){ cout<<"Reorder 150 of part "<<part<<endl; total=cost[2]*150; cout<<"Total reorder cost:$"<<total<<endl;} if (qual[3]<=435){ cout<<"Reorder 500 of part "<<part<<endl; total=cost[3]*500; cout<<"Total reorder cost:$"<<total<<endl;} if (qual[4]<=250){ cout<<"Reorder 750 of part "<<part<<endl; total=cost[4]*750; cout<<"Total reorder cost:$"<<total<<endl;}
what do you want me to do indent it until you cant see the code on the screen
Last edited by rfoor; 02-13-2009 at 08:44 AM.
Is that what you call indentation?
I call this indentation:
And another snippet:Code:if (qual[0]<=150){ cout<<"Reorder 200 of part "<<part<<endl; total=cost[0]*200; cout<<"Total reorder cost:$"<<total<<endl; } if (qual[1]<=250){ cout<<"Reorder 300 of part "<<part<<endl; total=cost[1]*300; cout<<"Total reorder cost:$"<<total<<endl; } if (qual[2]<=115){ cout<<"Reorder 150 of part "<<part<<endl; total=cost[2]*150; cout<<"Total reorder cost:$"<<total<<endl; } if (qual[3]<=435){ cout<<"Reorder 500 of part "<<part<<endl; total=cost[3]*500; cout<<"Total reorder cost:$"<<total<<endl; } if (qual[4]<=250){ cout<<"Reorder 750 of part "<<part<<endl; total=cost[4]*750; cout<<"Total reorder cost:$"<<total<<endl; }
This is by no means good looking code, but at least it's readable.Code:#include<iostream> #include<cstring> #include<fstream> using namespace std; using std::string; int main(){ char part[4]; char des[4]; char qual[4]; char cost[4]; char total[4]; int x; fstream file("Part.txt",ios::out); for(x=1;x<=5;x++){ cout<<"Part Number:"; cin>>part; file<<part<<endl; cout<<"Part Description:"; cin>>des; file<<des<<endl; cout<<"Quality on Hand:"; cin>>qual; file<<qual<<endl; cout<<"Unit Cost:"; cin>>cost; file<<cost<<endl; } file.close(); system("pause"); return 0; }