473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

pass values inputted using getline() to array

I need help with my program. Im trying to take input from a file and store the input into different arrays in my structure. The first pary where I get the students name worked fine but now when i try and assing the next 7 numbers into my quizzes array Im gettin problems. It's including the whitespace and therefore not accompilshing what I want to be done. Heres my file im reading in from:
Expand|Select|Wrap|Line Numbers
  1. Smith 9 9.33 8 10 5.5 8 10 20 47.5 47 45 47.5 48 83 87 100 98 
  2. 96 100 98 92 88 96 92 86 92 94 100 96 
  3.  
and heres my main file:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. using namespace std;
  6. using std::ifstream;
  7. using std::ofstream;
  8. //Structure for holding all the grades                                        
  9. struct Grades
  10. {
  11.     string student;
  12.     float quizzes[7];
  13.     float projects[6];
  14.     float exams[2];
  15.     float labs[14];
  16. };
  17.  
  18. int main()
  19. {
  20.     Grades a, b, c, d, e;
  21.     string line;
  22.     ifstream myfile("grades.txt");
  23.     if (myfile.is_open())
  24.     {
  25.         if (! myfile.eof() )
  26.         {
  27.             getline (myfile,line);
  28.             int i = 0;
  29.             while (isalpha(line[i]))
  30.             {
  31.                 a.student += line[i];
  32.                 i++;
  33.             }
  34.             for (int h = 0; h < 7; h++)
  35.             {
  36.                 a.quizzes[h] += line[i+1];
  37.                 i++;
  38.             }
  39.         }
  40.         myfile.close();
  41.     }
  42.     else cout << "Unable to open file";
  43.  
  44. }
  45.  
I need to assign the grades to each different array in my struct....I got the string to hold the students name now I need to assign the next 7 numbers to quizzes then the next 6 to Projects then 2 to exams and so on....Any help with how I can accompish this will be greatly appreciated. **I tried cout'ing the a.quizzes later in the file and thats how I know it's not working right.
Apr 30 '07 #1
1 2503
mac11
256 100+
You have several issues going on.

Expand|Select|Wrap|Line Numbers
  1.             getline (myfile,line);
First your telling getline to give you the whole line so line (your string) is just the text, its not smart enough to know what the text means. You have to break it apart into individual pieces before you can hand the pieces out. Look at using string::find() and/or string::substr().

Expand|Select|Wrap|Line Numbers
  1.             while (isalpha(line[i]))
  2.             {
  3.                 a.student += line[i];
  4.                 i++;
  5.             }
When you do this your adding the name to a.student which is fine because a.student is a string just like line is, but...

Expand|Select|Wrap|Line Numbers
  1.             for (int h = 0; h < 7; h++)
  2.             {
  3.                 a.quizzes[h] += line[i+1];
  4.                 i++;
  5.             }
Here your trying to add text onto a float, a.quizzes can only store floats - remember line only contains text (text that looks like numbers is still text) you have to convert the text into numbers - atof() is good at doing this
May 1 '07 #2

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

Similar topics

5
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...
2
by: jalkadir | last post by:
I am trying to get character string from the user, to do that I use getline(char_type*, streamsize), but I get a segmentation fault??!! Can anyone give me a hand, what am I doing wrong? --snip...
1
by: Mark Dicken | last post by:
Hi All I have found the following Microsoft Technet 'Q' Article :- Q210368 -ACC2000: How to Pass an Array as an Argument to a Procedure (I've also copied and pasted the whole contents into...
3
by: Martin | last post by:
I have a calendar form that sends the inputted date back to a text box . Using the GET method, I would like to pass the date as a variable to another asp page. How do I do this? Thanks.
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
1
by: gdarian216 | last post by:
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. struct records { string name; float quizzes; ...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
2
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.