473,503 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

store values inputted with getline() in variables in struct

57 New Member
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
1 2811
gpraghuram
1,275 Recognized Expert Top Contributor
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

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

Similar topics

5
4246
by: TG | last post by:
Dear PHP Group, I have two forms that are used to collect user information. The first one takes user inputted values such as fullname, city, address etc. I want these values to display in the...
1
2132
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I...
18
8222
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
3
8568
by: Mochuelo | last post by:
Hi, In the constructor of a class, I am taking as a parameter the reference to a "bool", as in this code: ----------------------- public class TCP_synch_client { public string address...
9
2097
by: JD | last post by:
Hi guys What if I need to return two values from a function. What's the best way to do it? Function is to load a text file into memory like so: -----8<----- char *LoadFile(char *infile)
5
3216
by: Denis Petronenko | last post by:
Hello, how can i read into strings from ifstream? file contains values in following format: value11; val ue12; value 13; valu e21;value22; value23; etc. i need to read like file >string,...
2
2365
by: raindeer | last post by:
Hello, I am new at computer programming and I am having trouble using getline() in C. I use an input file that contains 12 variables on every line. From the printf statements below, I notice...
4
7929
by: carmelo | last post by:
Hi! I need to read and store data from a file, but I don't the name of it; Is this code correct and is there a better way to do this thing?? int main() { cout << " type file name:" char *...
2
2485
by: eduardoamfm | last post by:
Hi everybody! I have the following files: (MSC.080806.00, MSC.080806.01, MSC.080806.02...) each one has the same pattern, with two different START and STOP times and values: ... ...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.