C++ infile.getline(), pointer, char array error

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1436 Views - Last Post: 26 October 2013 - 02:10 PM Rate Topic: -----

#16 jimblumberg  Icon User is online

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: C++ infile.getline(), pointer, char array error

Posted 26 October 2013 - 01:57 PM

Post your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#17 mcphersonad21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-September 13

Re: C++ infile.getline(), pointer, char array error

Posted 26 October 2013 - 02:04 PM

well, never mind..... i figured it out. I hate that it was something so small, i was focusing so much on my read in function i forgot there was some testing done late last night in my main { } so once i noticed it and adjusted according to the new test it printed and stopped at Barrack Obama.... Now the next step is for me to start formatting it properly and then sort by last name, thank you everyone for the wonderful help so far.... i felt hopeless for a moment!!!
Was This Post Helpful? 0
  • +
  • -

#18 mcphersonad21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-September 13

Re: C++ infile.getline(), pointer, char array error

Posted 26 October 2013 - 02:10 PM

View Postjimblumberg, on 26 October 2013 - 01:57 PM, said:

Post your current code.

Jim


code update

#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#define File "/Users/anthonymcpherson/Desktop/school/cs24400/program assignments/Assignment 5/Assignment 5/data5-1.txt"
#define MAX 45
#define Maxscores 4
#define WS "      "
#define Sentinel "No"

using namespace std;


void  ReadData ( ifstream &infile, char *Fname[], char *Lname[], float score[ ][4], int &REU, int &CEU);
void Print(ofstream &Outfile, char *Fname[], char *Lname[], float score[][4], int REU, int CEU);


int main(int argc, const char * argv[])
{
    char *Fname[MAX];
    char *Lname[MAX];
    float score[MAX][4] = {0};
    int REU=0;
    int CEU=0; 
    
    // open data4.txt file to read data from
    ifstream infile;
    infile.open(File,ios::in); 
    // create and open output file to print to... named Results4
    ofstream Outfile("Results5",ios::out);
    
    infile.setf(ios::fixed);
    cout.setf(ios::fixed);
    cout.precision(1);
    Outfile.setf(ios::fixed);// set my output print to fixed
    Outfile.precision(1);    // set fixed precision of float output to 2 digits after decimal    
    
    
    ReadData(infile, Fname, Lname, score, REU, CEU);

    Print(Outfile, Fname, Lname, score, REU, CEU);
    
    
    // close the infile
    infile.close();
    // close the outfile
    Outfile.close();     
    
    return 0;
}

//*********************** Function Header ****************************
void  ReadData ( ifstream &infile, char *Fname[], char *Lname[], float score[ ][4], int &REU, int &CEU)
{   
    char *FNptr;
    char TempF[16];
    char *LNptr;
    char TempL[16];
    int i = 0;
    

    infile.getline(TempF, 16);
    infile.getline(TempL, 16);
    
    
    REU = 0;
        
    //while( strncmp( TempF, Sentinel, strlen(Sentinel)) != 0 )
    //while( strcmp(TempF,"No            ") != 0 && infile.good() )
    while( strncmp(TempF,"No",2) !=0 )
    {           
        
        FNptr = new char[16];
        strncpy(FNptr,TempF,14);
        Fname[REU] = FNptr;
        
        LNptr = new char[16];
        strncpy(LNptr,TempL,14);
        Lname[REU] = LNptr;
        
        
        score[REU][CEU] = REU;
        CEU = 0;
        for(i = 0; i < 4; ++i)
        {
            infile >> ws;
            infile >> score[REU][i];
        }
        
        
        REU++;
            
        infile >> ws;
        infile.getline(TempF, 16);
        infile.getline(TempL, 16);
        
        
        //REU++; 
        
    }
    CEU = i;
    
    return; 
}
//************************ Print Function *******************************
void Print(ofstream &Outfile, char *Fname[], char *Lname[], float score[][4], int REU, int CEU)
{
    int i;
    int j;
    
    for(i = 0; i < REU; i++)
    {
        cout << Fname[i] << Lname[i] << "|";        
        {
            for( j= 0; j < 4; j++)
            {
                cout << setw(7) << score[i][j];
            }
            
            cout << endl;
        }
    }
}




and this is what my print currently looks like .... again i will format this with headers and also send it to Outfile "Results5"

Quote

George Washington | 96.8 99.9 97.7 95.6
John Adams | 99.5 88.8 85.9 94.5
Thomas Jefferson | 88.8 97.6 86.6 95.9
James Madison | 90.4 77.3 72.2 68.8
James Monroe | 77.2 77.8 88.3 83.2
John Quincy Adams | 55.9 89.9 82.2 84.6
Andrew Jackson | 91.8 93.5 94.1 90.2
Martin VanBuren | 89.9 33.5 76.4 67.1
William Henry Harrison | 66.5 55.7 55.8 44.9
John Tyler | 88.9 88.0 81.4 82.4
James K. Polk | 86.7 86.8 91.2 95.9
Zachey Taylor | 55.5 66.6 66.2 62.1
Millard Fillmore | 67.3 77.8 65.9 67.2
Franklin Pierce | 59.3 62.2 87.8 60.3
James Buchanan | 66.1 62.6 54.1 45.2
Abraham Lincoln | 95.4 90.4 93.4 97.1
Andrew Johnson | 47.4 66.8 75.5 55.2
Ulyses S. Grant | 60.3 78.9 71.4 62.2
Rutherford B. Hayes | 77.8 77.9 72.4 80.1
James A. Garfield | 79.9 69.9 59.9 84.6
Chester A. Arthur | 66.7 77.1 56.6 59.3
Grover Cleveland | 88.2 82.2 78.9 68.3
Benjamin Harrison | 88.3 89.3 78.4 69.4
Grover Cleveland | 77.9 34.7 48.9 78.4
William McKinley | 56.6 45.6 34.5 23.8
Teddy Roosevelt | 76.7 62.2 88.9 75.9
William Taft | 72.3 72.3 84.3 80.2
Woodrow Wilson | 40.9 58.3 68.8 51.1
Warren G. Harding | 76.8 87.4 87.1 79.3
Calvn Coolidge | 75.8 89.9 78.2 80.3
Herbert Hoover | 66.8 90.9 58.4 69.4
Franklin D. Roosevelt | 89.9 68.7 60.2 77.5
Harry S. Truman | 77.9 71.2 76.7 75.3
Dwight D. Eisenhower | 82.2 81.9 72.6 75.6
John F. Kennedy | 68.3 87.3 63.7 79.2
Lyndon B. Johnson | 57.3 66.3 64.8 30.6
Richard M. Nixon | 76.2 67.3 72.1 75.6
Gerald Ford | 88.8 59.2 67.7 80.5
Jimmy Carter | 55.5 36.6 54.5 39.9
Ronald Reagan | 97.8 98.2 93.3 96.5
George Bush | 88.8 87.8 66.6 68.2
Bill Clinton | 45.5 56.5 34.6 45.6
George W. Bush III | 80.7 89.6 67.2 61.4
Barrack Obama | 50.6 60.2 47.7 52.4


You guys are great, thank you so much for taking the time to help a complete stranger in need!! i cant even explain what that means....
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2