![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| help with array to function working on a project for class, and i can't seem to get it to work. I initialize an array to zero then open a file, reassigning the array based on the input info. I must print out what is in the array with a function. i am getting the same output from my function before and after the main part of the body. any hel is appreciated. #include<iostream> #include<cmath> #include<fstream> using namespace std; void printbookings( int [], int ); double income(int inumb[],int ic); int main(){ int trtest, amnts,flightpas,n,cnt; int bookings[100]; for(n=0;n<10;n++){ bookings[n]=0; } printbookings(bookings, n) ; ifstream flightInfo("flightInfo.dat"); flightInfo>>trtest>>amnts>>flightpas; while(flightInfo){ if(trtest==1) bookings[flightpas]+=amnts; else if(amnts>bookings[flightpas]) cout<<" no"<<endl; else bookings[flightpas] -=amnts; flightInfo>>trtest>>amnts>>flightpas; } flightInfo.close(); printbookings(bookings, n); system("pause"); return 0; } void printbookings( int bookings[],int sz){ cout.width(10); cout<<"Flnmb"; cout.width(10); cout<<"Nmbks"; cout<<endl; cout.width(10); for(int i=0;i<sz;i++){ cout<< i; cout.width(10); cout<<bookings[i]; cout.width(10); cout<<endl; } return ; } |
| |
| |||
| If you wish to initialise your array elements to zero, you can declare the array using the following syntax (This means you don't need to loop through and do it manually) Code: int bookings[100] = { 0 }; Could you give a few lines of your input file as an example of the data you're trying to read? I can't see any obvious problems with your code, except that when you use your flightpas variable in your array subscript, you've not checked that its a valid index for your bookings array (ie, a number in the range 0-99). This may or may not be a problem depending on your input file. |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (11-09-2007) | ||
![]() |
| Thread Tools | |
| Display Modes | |
| |