The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


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.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 11-06-2007, 08:26 PM
slid slid is offline
Novice
Join Date: Nov 2007
Posts: 3
iTrader: (0)
slid is on a distinguished road
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 ;
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 11-09-2007, 02:03 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 111
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
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 };
Also, what output are you expecting to get? What output are you actually getting?


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.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (11-09-2007)
  #3 (permalink)  
Old 11-09-2007, 07:12 AM
slid slid is offline
Novice
Join Date: Nov 2007
Posts: 3
iTrader: (0)
slid is on a distinguished road
thanks

thanks for the info. The real problem was that i was getting a segmentation error, like you said, and i was misinterpreting my data. I just re-arranged my input and it seems to work fine.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:05 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50