Ok Im trying to use a struct to take in input from a file "grades.txt " which looks like:
Smith 9 9.33 8 10 5.5 8 10 20 47.5 47 45 47.5 48 83 87 100 98 96 100 98 92 88 96 92 86 92 94 100 96
Jones 9 8 6 6 8 7.5 8 20 35 40 42.5 40 44 62 72 100 88 86 90 92 92 88 86 88 86 94 88 86 90....etc i don't know how many students their will be.
And I need to store them in a structre like how I have in my code...Im unsure how to get all the input to go into each of these arrays . ANy help will be greatly appreciated..he res wha i have so far. -
#include <iostream>
-
#include <string>
-
#include <vector>
-
#include <fstream>
-
using namespace std;
-
-
//Structure for holding all the grades
-
struct Grades
-
{
-
string student;
-
float quizzes[7];
-
float projects[6];
-
float exams[2];
-
float labs[14];
-
};
-
-
int main()
-
{
-
ifstream inFile;
-
inFile.open("grades.txt");
-
if (inFile.fail()) {
-
cerr << "unable to open file input.dat for reading" << endl;
-
exit(1);
-
}
-
-
-
}
-
the student name comes first then the quizzes (7) then the Projects (6) then exams (2) then labs (14) I dont know how I'm going to get the input to go into the right arrays...Also I don't know how I will be able to take all that input at once (defferent amounts of students and their scores and get them to go into seperate structs so that the one struct doesn't have more than 1 student in it....Im kinda lost and I need some direction any help will be great. In the future of this program im going to need to output a table like this if thats any help to you:
No. –Name-- --Quiz-- -Project- -Exam- -Lab- -Total- Grade
--- ----------- -------- ---------- ------ ----- ------- -----
1. Smith 90.55 94.44 85.00 94.86 91.41 A-
2. Jones 77.50 82.04 67.00 89.57 78.88 C+
.
.
.
--- ----------- -------- ---------- ------ ----- ------- -----
Avg: ?
5 4956 Savage 1,764
Recognized Expert Top Contributor
Hi,lets see:
I think that nested loops are best solution to ur problem.
Outer while loop should loop until eof.(end of file).
Then we read in one line using fgets or any simmilar method.
Inner while loop should go trough that line until reaching specific character that u can specify or until reaching '\n' character in line
now:
if is alpha we put that char in temporary char array and later we asign it to the string in ur structure.
else read in specific amount of numbers and store it the same way to the floats and other variables in ur struct.
Savage
so will this work then...and then just start with some nested loops inside the while loop... -
#include <iostream>
-
#include <string>
-
#include <vector>
-
#include <fstream>
-
using namespace std;
-
-
//Structure for holding all the grades
-
struct Grades
-
{
-
string student;
-
float quizzes[7];
-
float projects[6];
-
float exams[2];
-
-
float labs[14];
-
};
-
-
int main()
-
{
-
string line;
-
ifstream myfile ("grades.txt");
-
if (myfile.is_open())
-
{
-
while (! myfile.eof() )
-
{
-
getline (myfile,line);
-
}
-
myfile.close();
-
}
-
-
else cout << "Unable to open file";
-
}
-
-
-
}
-
Savage 1,764
Recognized Expert Top Contributor
Yes,that should do it.
Savage
Yes,that should do it.
Savage
how would i do an if is alpha loop for (int i = 0, line[i] == char, i++){
student = student + line[i];}
im not sure about that one ive never done it.
Savage 1,764
Recognized Expert Top Contributor
how would i do an if is alpha loop for (int i = 0, line[i] == char, i++){
student = student + line[i];}
im not sure about that one ive never done it.
This function is builded so that it returns 0 if the char is not a alpha.
So simple while loop like:
i=0;
while(isalpha(l ine[i])
{
//extract it to ur string in struct
}
should work.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mark Hannon |
last post by:
I am trying to wrap my brain around storing form elements inside
variables & arrays before I move on to a more complicated project. I
created this simple example to experiment and as far as I can tell, it
should work but it doesn't. Can someone tell me where I went wrong?
<html><head>
<title>Form Test</title>
<script language="JavaScript>
<!--
function copy(){
|
by: becte |
last post by:
I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8)
like this
11111122|22223333|33444444
Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1
and the output is int o1,o2,o3, range 0 .. 2^8-1
How to do this in a clever way?
(The 6 bits integers represent characters in range A-Z and 0-9)
|
by: Wynand Winterbach |
last post by:
I think every C programmer can relate to the frustrations that malloc
allocated arrays bring. In particular, I've always found the fact that
the size of an array must be stored separately to be a nightmare.
There are of course many solutions, but they all end up forcing you to
abandon the array syntax in favour of macros or functions.
Now I have two questions - one is historical, and the other practical.
1.) Surely malloc (and...
|
by: Kay |
last post by:
A linked list is storing several names. I want to make a queue if I
input a name that is same as the linked list. How to make each node of a
linked list storing a queue that are different with each other node, do
I need to add one more item in the ListNode OR I only call the queue
insert function to do it ?
|
by: Michel Rouzic |
last post by:
It's the first time I try using structs, and I'm getting confused with
it and can't make it work properly
I firstly define the structure by this : typedef struct { char *l1; int
*l2; int Nval; } *arrays;
It's supposed to be a structure containing an array of chars, an array
of ints and an int.
I declare functions like this : arrays *parseline(char *line, int N)
| |
by: Stainless |
last post by:
I have a public class Globals, which obviously holds all my global data.
I have an array of 243 items, currently structs of type
typedef struct STAR
{
int x;
int y;
int stellar_class;
}t_star;
|
by: sherifffruitfly |
last post by:
Hi,
I've a got a little (exercise) program that reads data from a file and
puts it into struct members. I run into trouble when one of the data
pieces is comprised of several words (eg "john doe", with a space in
it).
For console input, cin.getline(var, howMuchIWant) or cin.get() has done
the trick for me in the past. It doesn't seem to work for me nearly so
well with a file stream. I wouldn't have thought cpp regarded
|
by: Christoph Scholtes |
last post by:
Hi,
I have two questions about the following code snippet. I am trying to
read in a series of strings and save them to character arrays. Since I
dont know how long my string is going to be (and I dont want to waste
memory by allocating an array of 1000 for all the input strings) I
allocate the memory myself according to the length of the string.
char *inputData;
char line;
|
by: assgar |
last post by:
Hi
Developemnt on win2003 server. Final server will be linux
Apache,Mysql and PHP is being used.
I use 2 scripts(form and process).
The form displays multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply.
Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money.
Note The above informaton is...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 we have to send another system
| |