Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 2nd, 2004, 11:19 AM   #1
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,671
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
Ok my problem is i have a struct and im tring to ready a line into the struct using fstream. here is the code i have its just a basic layout of what will come later on. but here it is

// file: main.cpp
// author: Kyle Kjorsvik
// date: 8/31/04
// class: CSIS 352

// This is the file that contains the main function for assignment 1.

#include <iostream>
#include <fstream>
#include <string>
#include "date.h"
//#include "inout.h"
//#include "agecalc.h"
//#include "data.h"

using namespace std;

struct birthdate
{
    char name[];
    char bdate[];
};

int main()
{
    char chrName[25];
    int maxvalue = 10;
    birthdate bd[maxvalue + 1];
    int i = 0;

    ifstream inFile("data"); // input file
    if (inFile.fail())
    {
       cerr << "An error occurred. Unable to read input file." << endl;
        exit(1);
    }
    cout << "File opened" << endl;

    while(!inFile.eof())
    {
        inFile.get(bd[i].name[],25,",");
       //cout << bd[i].name << endl;
        ++i;
    }

    inFile.close();
    return 0;
}

and here is the file i am reading in

Torii Hunter, 7/18/1975
Jacque Jones, 4/25/1975
Corey Koskie, 6/28/1973
Justin Morneau, 5/15/1981
Some Young Kid, 7/9/1999
Shannon Stewart, 2/25/1974
Joe Nathan, 11/22/1974
Lew Ford, 8/12/1976
Matthew LeCroy, 12/13/1975
J. C. Romero, 6/4/1976

i need to read it in sperating the name and the birthdate. using the comma as the seperator. can someone help me on how i should be reading this into the array of structs? thanks

edit: if you need more info let me know
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Sep 2nd, 2004, 11:50 AM   #2
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,671
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
dont know exactly what i was doing wrong as i havent taken time to compare the code that i was give but here is what works

// file: main.cpp
// author: Kyle Kjorsvik
// date: 8/31/04
// class: CSIS 352

// This is the file that contains the main function for assignment 1.

#include <iostream>
#include <fstream>
#include <string>
#include "date.h"
//#include "inout.h"
//#include "agecalc.h"
//#include "data.h"

using namespace std;

struct birthdate{
  string name;
  string bdate;
};

int main() {
  const int maxvalue = 10;
  birthdate bd[maxvalue + 1];
  int i = 0, j;

  ifstream inFile("data");
  if (inFile.fail()) {
    cerr << "An error occurred. Unable to read input file." << endl;
    exit(1);
  }
  cout << "File opened" << endl;

  while(!inFile.eof() && i < maxvalue) {
    getline(inFile, bd[i].name, ',');
    getline(inFile, bd[i].bdate);
    ++i;
  }
  inFile.close();

  for (j=i-1; j>=0; j--){//print backwards for giggles
    cout << "\t" << bd[j].name << "\t" << bd[j].bdate << endl;
  }

  return 0;
}
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Sep 2nd, 2004, 1:31 PM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,475
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
glad you got it working
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC