473,473 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Assigning data to member variables in a structure?

kordric
7 New Member
Hello Im trying to figure out how to do what the title says.
Here is what my structure looks like
Expand|Select|Wrap|Line Numbers
  1. //Structure for the Student Recored
  2. struct studentRecord
  3. {
  4.     int studentID;
  5.     string first_name;
  6.     string last_name;
  7.     int quiz1;
  8.     int quiz2;
  9.     int midTerm;
  10.     int finalExam;
  11.     int totalPoints;
  12.     double percentTotal;
  13.     char letterGrade;
  14. };
  15.  
The only data that will need to be assigned will be the:
Expand|Select|Wrap|Line Numbers
  1. studentID, quiz1, quiz2, midTerm, finalExam, 
because these are variables the user will input.

Also i need to be able to process multiple records, because more than one student will be entered. So my question about this is do i need to change my structure?

Please help!

Thank you.
Mar 27 '08 #1
11 2157
gpraghuram
1,275 Recognized Expert Top Contributor
Hello Im trying to figure out how to do what the title says.
Here is what my structure looks like
Expand|Select|Wrap|Line Numbers
  1. //Structure for the Student Recored
  2. struct studentRecord
  3. {
  4.     int studentID;
  5.     string first_name;
  6.     string last_name;
  7.     int quiz1;
  8.     int quiz2;
  9.     int midTerm;
  10.     int finalExam;
  11.     int totalPoints;
  12.     double percentTotal;
  13.     char letterGrade;
  14. };
  15.  
The only data that will need to be assigned will be the:
Expand|Select|Wrap|Line Numbers
  1. studentID, quiz1, quiz2, midTerm, finalExam, 
because these are variables the user will input.


Also i need to be able to process multiple records, because more than one student will be entered. So my question about this is do i need to change my structure?

Please help!

Thank you.

The only data that will need to be assigned will be the:
Expand|Select|Wrap|Line Numbers
  1. studentID, quiz1, quiz2, midTerm, finalExam, 
because these are variables the user will input.

from where you will get other data?


Also i need to be able to process multiple records, because more than one student will be entered.

You can have array of these structures or a vector of this structure

Raghuram
Mar 27 '08 #2
kordric
7 New Member
I changed it a little,

Expand|Select|Wrap|Line Numbers
  1. //Structure for the Student Records
  2. struct studentRecord
  3. {
  4.     int studentID;
  5.     string first_name;
  6.     string last_name;
  7.     int quiz1;
  8.     int quiz2;
  9.     int midTerm;
  10.     int finalExam;
  11.  
  12. };
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     studentRecord student;
  4.     get_data(student);
  5.  
  6.     int totalPoints;
  7.     int totalPointsPossible;
  8.     double percentTotal;
  9.     char letterGrade;
  10.  
  11.     //Calculate Total Points
  12.     // This adds together all quizes and exams
  13.     totalPoints = student.quiz1 + student.quiz2 + student.midTerm + student.finalExam;
  14.  
  15.     //Calulate Percentage in class
  16.     totalPointsPossible = 200;
  17.     percentTotal = ((totalPoints / totalPointsPossible) * 100);
  18.  
  19.     //Figure out letter grade
  20.     if (percentTotal>= 90)
  21.     {
  22.         letterGrade = 'A';                           
  23.     }
  24.     else if ((percentTotal>= 80) || (percentTotal < 90))
  25.     {
  26.          letterGrade = 'B';
  27.     }
  28.     else if ((percentTotal>= 70) || (percentTotal < 80))
  29.     {
  30.          letterGrade = 'C';
  31.     }
  32.     else if ((percentTotal>= 60) || (percentTotal < 70))
  33.     {
  34.          letterGrade = 'D';
  35.     }
  36.     else if (percentTotal<60)
  37.     {
  38.          letterGrade = 'F';
  39.     }
  40.  
  41.  
  42.  
  43.     //Display Student Info
  44.     cout << "\nStudent ID =  " << student.studentID <<"";
  45.     cout << "\nFirst Name =  " << student.first_name <<"";
  46.     cout << "\nLast Name =  " << student.last_name <<"";
  47.     cout << "\nQuiz 1 Score =  " << student.quiz1 <<"";
  48.     cout << "\nQuiz 2 Score =  " << student.quiz2 <<"";
  49.     cout << "\nMid Term Score =  " << student.midTerm <<"";
  50.     cout << "\nFinal Exam Score =  " << student.finalExam <<"";
  51.     cout << "\nTotal Points =  " << totalPoints <<"";
  52.     cout << "\nPercent Total =  " << percentTotal <<"%";
  53.     cout << "\nLetter Grade =  " << letterGrade <<"\n";
  54.  
  55.     system("PAUSE");
  56.     return 0;
  57. }    
The studentRecord comes from
Expand|Select|Wrap|Line Numbers
  1. void get_data(studentRecord& the_student)    
  2.     //Check student ID
  3.     cout << "\nEnter Student Id\n";
  4.     cin >> the_student.studentID;
  5.     //Check for mistakes
  6.     // Correct Value does this
  7.     if ((the_student.studentID> 1) || (the_student.studentID < 99999))
  8.     {
  9.     }
  10.     // Incorrect does this
  11.     // A loop To validate the Student ID
  12.     while ((the_student.studentID < 1) || (the_student.studentID > 99999)) 
  13.     {
  14.           cout <<"\nMust enter an Student ID between 1 and 99999.\n";
  15.           cout <<"Re Enter Student ID:\n";
  16.           cin >> the_student.studentID;
  17.           cout <<"\n";
  18.     } 
  19.  
  20.     //Get First and last names
  21.     cout << "\nEnter First name\n";
  22.     cin >> the_student.first_name;
  23.  
  24.     cout << "\nEnter last name\n";
  25.     cin >> the_student.last_name;
  26.  
  27.  
  28.     //Check Quiz 1 score
  29.     cout << "\nEnter quiz 1 score\n";
  30.     cin >> the_student.quiz1;
  31.     //Check for mistakes
  32.     // Correct Value does this
  33.     if ((the_student.quiz1> 0) || (the_student.quiz1 < 25))
  34.     {
  35.     }
  36.     // Incorrect does this
  37.     // A loop To validate the quiz1
  38.     while ((the_student.quiz1 < 0) || (the_student.quiz1 > 25)) 
  39.     {
  40.           cout <<"\nMust enter an quiz1 score between 0 and 25.\n";
  41.           cout <<"Re Enter quiz1 score:\n";
  42.           cin >> the_student.quiz1;
  43.           cout <<"\n";
  44.     } 
  45.  
  46.     //Check Quiz 2 score
  47.     cout << "\nEnter quiz 2 score\n";
  48.     cin >> the_student.quiz2;
  49.     //Check for mistakes
  50.     // Correct Value does this
  51.     if ((the_student.quiz2> 0) || (the_student.quiz2 < 25))
  52.     {
  53.     }
  54.     // Incorrect does this
  55.     // A loop To validate the quiz2
  56.     while ((the_student.quiz2 < 0) || (the_student.quiz2 > 25)) 
  57.     {
  58.           cout <<"\nMust enter an quiz2 score between 0 and 25.\n";
  59.           cout <<"Re Enter quiz2 score:\n";
  60.           cin >> the_student.quiz2;
  61.           cout <<"\n";
  62.     } 
  63.  
  64.     //Check Midterm
  65.     cout << "\nEnter Mid Term score\n";
  66.     cin >> the_student.midTerm;
  67.     //Check for mistakes
  68.     // Correct Value does this
  69.     if ((the_student.midTerm> 0) || (the_student.midTerm < 50))
  70.     {
  71.     }
  72.     // Incorrect does this
  73.     // A loop To validate the midTerm
  74.     while ((the_student.midTerm < 0) || (the_student.midTerm > 50)) 
  75.     {
  76.           cout <<"\nMust enter an midTerm score between 0 and 50.\n";
  77.           cout <<"Re Enter a midTerm score:\n";
  78.           cin >> the_student.midTerm;
  79.           cout <<"\n";
  80.     }
  81.  
  82.     //Check Final
  83.     cout << "\nEnter Final Exam score\n";
  84.     cin >> the_student.finalExam;
  85.     //Check for mistakes
  86.     // Correct Value does this
  87.     if ((the_student.finalExam> 0) || (the_student.finalExam < 100))
  88.     {
  89.     }
  90.     // Incorrect does this
  91.     // A loop To validate the finalExam
  92.     while ((the_student.finalExam < 0) || (the_student.finalExam > 100)) 
  93.     {
  94.           cout <<"\nMust enter an finalExam score between 0 and 100.\n";
  95.           cout <<"Re Enter a finalExam score:\n";
  96.           cin >> the_student.finalExam;
  97.           cout <<"\n";
  98.     }
  99. }


Also the percentTotal is coming out as 0.
Edit: FIXED the above changed code to this.
Expand|Select|Wrap|Line Numbers
  1. percentTotal = ((totalPoints * 100) / totalPointsPossible);
also i added this after calcs.
Expand|Select|Wrap|Line Numbers
  1.     //Show to first decimal place.
  2.     cout.setf(ios::fixed);
  3.     cout.setf(ios::showpoint);
  4.     cout.precision(1);

Also If i use an array for adding additional students can it be unlimited, or is there a way to add in a question at the end saying yes add or N for no.

Secondly i need to figure out a way to add all the scores of the students entered and have an average score of them.

I'm quite new to programing and this is all a learning experience for me.

Thank you!
Mar 27 '08 #3
gpraghuram
1,275 Recognized Expert Top Contributor
You calculate yhe percenttotal like this
Expand|Select|Wrap|Line Numbers
  1. percentTotal = ((totalPoints / totalPointsPossible) * 100);
  2.  
but the totalPoints and totalPointsPossible are both ints and you wont have the decimal part for this.


Raghuram
Mar 27 '08 #4
kordric
7 New Member
Yes thanks, i changed those to double, so they would work properly.

Now need to figure out the rest!

I would like to use an array, but is there a way to have it automatically figure out how many entries there are? Cause they may vary each time

Would i need something like

Expand|Select|Wrap|Line Numbers
  1. const int number_of_students = (somehow add all entries together after confirming finish)
  2.  
then the array would be like

Expand|Select|Wrap|Line Numbers
  1. int i, score[number_of_students], max;
Mar 27 '08 #5
kordric
7 New Member
For the numbers of students, just have a variable inside a loop increment every time the loop restarts.

For the high and low points, use an if statement to determine whether or not the total points is higher or lower than the previous one and store the new value it if it is.

For the average, just keep adding the total points every loop then divide it by the number of students.

Just some ideas, trying to figure out how to do them correctly.

Opps double post meant to edit.
Mar 27 '08 #6
gpraghuram
1,275 Recognized Expert Top Contributor
You can have a dynamic array to hold the same like
Expand|Select|Wrap|Line Numbers
  1. int *score = (int*)malloc(sizeof(struct) *number_of_students)
  2.  
Raghuram
Mar 27 '08 #7
kordric
7 New Member
would that go under int main()?
Mar 27 '08 #8
gpraghuram
1,275 Recognized Expert Top Contributor
would that go under int main()?
You can have it there...
But this address has to be passed to the calling function if you are changing the values inside the calling function.


Raghuram
Mar 27 '08 #9
kordric
7 New Member
So should i go ahead and make another void function, bear with me here, like i said im just learning this stuff.

Thanks
Mar 27 '08 #10
gpraghuram
1,275 Recognized Expert Top Contributor
So should i go ahead and make another void function, bear with me here, like i said im just learning this stuff.

Thanks
You can do that way also, but if you are writing the code in main function itsef.
Better write the code and post me, so i can take a look into it

Raghuram
Mar 28 '08 #11
kordric
7 New Member
You can do that way also, but if you are writing the code in main function itsef.
Better write the code and post me, so i can take a look into it

Raghuram
I need a new structure first, but how does it determine how many entries to save, since it may change each time?

Also the program must prompt the keyboard operator to determine whether there is another student to be processed, if not, display results, but i cant figure out what i need to do for this part.

So far heres what i have,
The Structure
Expand|Select|Wrap|Line Numbers
  1. struct allstudentRecord
  2. {
  3.     int totalStudents;
  4.     int highestPoints;
  5.     int lowestPoints;
  6.     int avgTotalPoints;
  7.     char avgLetterGrade;
  8.  
  9. };
Then i create a void
Expand|Select|Wrap|Line Numbers
  1. void get_allData (allstudentRecord& allthe_student)
  2. {
  3.       int *allthe_student.score = (int*)malloc(sizeof(struct) *allthe_student.totalStudents)
But i got stuck here cause the declaration of allthe_student shadows a parameter. So im not sure what to do.
Mar 28 '08 #12

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

Similar topics

2
by: Michael Easterly | last post by:
What is a good way to read a text file and read each line, then assign data to variables? I have this so far, #include <iostream> #include <fstream> using namespace std; string...
7
by: Lynn | last post by:
I am rewriting some memory management code and I need to have a forward declaration of a data structure. I am not converting this data structure into a class (yet). How do I generate a forward...
11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
2
by: Brent | last post by:
I have variables in a structure loaded into a list box. I thought I could use FieldInfo.SetValue to update the items value when the user clicks on it, but it is not working. .. .. .. Dim fi...
4
by: Jon | last post by:
This seems strange, but maybe there's some basic concept I'm missing. When I assign one class member to another, any methods that are applied to one are applied to both variables.I can't get the...
1
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. ...
17
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void...
3
by: =?Utf-8?B?Umljb2hEZXZlbG9wZXI=?= | last post by:
I have a class library project that uses unmanaged C dll to perform some image handling. This DLL requires me to pass in a structure containing image coordinates. In VB6, I could use a...
8
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a large class with a lot of member variables. I also have a function in the class that I would like to change ALL Of the member variables. I am trying to assign...
0
Oralloy
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,...
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...
0
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
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,...
1
isladogs
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...
0
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.