By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

store values inputted with getline() in variables in struct

P: 57
I am trying to read in inputs from a file to my program. I have declared a struct that holds a string and some arrays.

Expand|Select|Wrap|Line Numbers
  1. struct records
  2. {
  3.         string name;
  4.         float quizzes[7];
  5.         float projects[6];
  6.         float exams[2];
  7.         float labs[14];
  8. };
I was wondering how to read in the a string of letters and then store the scores in the arrays.

This is what I have so far if anyone can help

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.         // Ask the user for a file name
  4.         cout << "Enter a file name: ";
  5.         string record;
  6.         cin >> record;
  7.         cout << record;
  8.  
  9.         // Define a file input stream and open the file
  10.         ifstream inStream(record.c_str());
  11.  
  12.         if (!inStream)
  13.         {
  14.         // Failed to open the file (file doesn't exist or isn't readable)
  15.         cout << "Could not open file: " << record  << "\n";
  16.         exit(1);
  17.         }
  18.  
  19.         string line;
  20.  
  21.                 //declare saved variables for averages.
  22.  
  23.  
  24.         // Repeatedly get characters from the file
  25.         while(! inStream.eof())
  26.         {
  27.         getline(inStream, line);
  28.                 //function for inputing info in struct.
  29.                 //function for averaging and storing info.
  30.  
  31.         }
  32.  
  33.     // Close the file
  34.     inStream.close();
  35.  
  36.  
  37.     return 0;
  38. }
May 2 '07 #1
Share this Question
Share on Google+
1 Reply


gpraghuram
Expert 100+
P: 1,275
Hi,
You can use sscanf to read the variables from a string and store it into values.
Check this page for more info
Thanks
Raghuram
May 2 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.