Question about putting structs into arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 2
Reputation: Nneedofhelp is an unknown quantity at this point 
Solved Threads: 0
Nneedofhelp Nneedofhelp is offline Offline
Newbie Poster

Question about putting structs into arrays

 
0
  #1
Nov 3rd, 2004
I'm writing a program that reads a text file and puts it into an array of structs. There can only be one transaction per line in this format:

custNumber firstName lastName numItemsPurchased amtSpent

What I dont understand is how do you put the data in the file into an array of structs?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 2
Reputation: DKnight764 is an unknown quantity at this point 
Solved Threads: 0
DKnight764 DKnight764 is offline Offline
Newbie Poster

Re: Question about putting structs into arrays

 
0
  #2
Nov 3rd, 2004
Hello,

First off, a couple of things have to be known.

1) Do you know how to read in the data and sperate it into just regualar variables?
2) Do you know how to construct a strut variable type?
2) Do you know how to make an array of structs?

What you are asking for is pretty broad, and without out right writing the program for you , you need to say what exactly you are having a problem with.

DKnight764
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 2
Reputation: Nneedofhelp is an unknown quantity at this point 
Solved Threads: 0
Nneedofhelp Nneedofhelp is offline Offline
Newbie Poster

Re: Question about putting structs into arrays

 
0
  #3
Nov 3rd, 2004
Ok first...no i'm not sure how to read in the data and separate it and the only thing i know about structs is what i read in the book...

But this is what i have so far:
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct custInfo{
  7. int custNumber, numItems;
  8. string firstName, lastName;
  9. float amtSpent;
  10. };
  11.  
  12. void getData(ifstream &infile, custInfo list[], int listSize);
  13.  
  14. int main(){
  15. ifstream infile;
  16.  
  17. custInfo custList[100];
  18. int size, input;
  19.  
  20. infile.open("E:sales.txt");
  21.  
  22. if(!infile){
  23. cout<<"Cannot open input file."<<endl;
  24. return 1;
  25. }
  26. getData(input, custList[], size);
  27. return 0;
  28. }
  29.  
  30. void getData(ifstream &infile, custInfo list[], int listSize){
  31.  
  32. for (int i=0; i<100; i++){
  33. infile>>list[i].custNumber>>list[i].firstName
  34. >>list[i].lastName>>list[i].numItems
  35. >>list[i].amtSpent;
  36. }
  37.  
  38. for (int count=0; count<100; count++){
  39. cout<<list[count].custNumber<<" "
  40. <<list[count].firstName<<" "
  41. <<list[count].lastName<<" "
  42. <<list[count].numItems<<" "
  43. <<list[count].amtSpent;
  44. }
  45. }

I know this isnt right because when i run it nothing happens. It compiles but that's about it. That's how they put the struct into an array in my C++ book and that's the way the got it to print out too.

I have to read in the data from a text file and properly put them in the array. 100 customers is the allowed limit. I figure if i cant get the data into the array then I can finish the rest of the program myself.
Last edited by alc6379; Nov 3rd, 2004 at 10:19 pm. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC