C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-21-2009, 10:19 AM   #1
Registered User
 
Join Date: Apr 2009
Posts: 8
Post Can't get array to output correctly

I am working with a problem that pulls information from an input file. Here is the .cpp file:

Code:
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

main(){
    char firstName[5][12];
    char lastName[5][15];
    int ssn[9];
    int hoursWorked[5];
    double grossPay[5],netPay[5],hourlyRate[5],otHours[5],otPay[5],taxRate[5], regularPay[5],taxAmount[5];

    int counter=0;
    int i;
    ifstream fin("payroll.txt");
    if(!fin){
        cout<<"Could not read input file. \n";
        return 1;
    }

while(fin>>firstName[counter]>>lastName[counter]>>ssn[counter]>>hoursWorked[counter]>>hourlyRate[counter]>>otHours[counter]>>otPay[counter]>>regularPay[counter]>>grossPay[counter]>>taxAmount[counter]>>netPay[counter])
    {
        cout<<grossPay[counter]<<endl;
        //cout<<lastName[counter];
        cout<<"Counter is: "<<counter;
        counter++;
        cout<<"Now: "<<counter;
    }
} // end main
The payroll.txt file contains the following data:
SampleFirst SampleLast M 555555555 40 35 0 0 1400 1400 140 1260
John Smith M 77777777 50 10 10 100 500 600 30 570

Oddly enough, if I take out the braces around the while loop, it will output the counter values. The statement to check if the file is opened always fires correctly (i'm assuming, since it does not throw an error).

Thanks for the help!
blueshift1980 is offline   Reply With Quote
Old 04-21-2009, 12:46 PM   #2
Registered User
 
Join Date: Apr 2009
Posts: 8
Doh! Somehow forget one of the variables.
blueshift1980 is offline   Reply With Quote
Old 04-21-2009, 01:16 PM   #3
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
Yep, the loop evaluates to false the first time through since you're attempting to read in an integer ssn where there is a char in the file... so you never enter the loop body.
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
hk_mp5kpdw is offline   Reply With Quote
Reply

Tags
arrays, datafile, input file

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help for my output array qwertysingh C Programming 1 02-17-2009 03:08 PM
Modify an single passed array element swgh C Programming 3 08-04-2007 08:58 AM
Merge sort please vasanth C Programming 2 11-09-2003 12:09 PM
Struct *** initialization Saravanan C Programming 20 10-09-2003 12:04 PM
Array Program emmx C Programming 3 08-31-2003 12:44 AM


All times are GMT -6. The time now is 08:36 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22