Thread: Can't get array to output correctly

  1. #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!

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Doh! Somehow forget one of the variables.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread