473,404 Members | 2,178 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,404 software developers and data experts.

Can't seem to pass array correctly

Everywhere I declare student[i].score I keep getting these error messages:

line 46: error C2676: binary '[' : 'data' does not define this operator or a conversion to a type acceptable to the predefined operator
line 46: error C2228: left of '.score' must have class/struct/union
line 56: error C2676: binary '[' : 'data' does not define this operator or a conversion to a type acceptable to the predefined operator
line 56: error C2228: left of '.score' must have class/struct/union
line 58: warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
line 83: error C2676: binary '[' : 'data' does not define this operator or a conversion to a type acceptable to the predefined operator
line 83: error C2228: left of '.id' must have class/struct/union
line 84: error C2676: binary '[' : 'data' does not define this operator or a conversion to a type acceptable to the predefined operator
line 84: error C2228: left of '.score' must have class/struct/union
line 85: error C2676: binary '[' : 'data' does not define this operator or a conversion to a type acceptable to the predefined operator
line 85: error C2228: left of '.grade' must have class/struct/union




This is my program:

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6.  
  7. struct data
  8. {
  9.     int id;
  10.     int score;
  11.     char grade;
  12. }; 
  13.  
  14.  
  15.  
  16. float getAverage (data& student);
  17. void readStuData (data& student);
  18. void assignGrades (data student);
  19. void reportResults (data student);
  20.  
  21. int student[10];
  22.  
  23. int main ()
  24. {
  25.     data studentInfo;
  26.  
  27.     readStuData (studentInfo);
  28.     getAverage (studentInfo);
  29.     assignGrades (studentInfo);
  30.     reportResults (studentInfo);
  31.  
  32.     return 0;
  33. }
  34.  
  35. void readStuData (data& student)
  36. {
  37.     int i;
  38.  
  39.     cout << "Please enter student ID: ";
  40.     cin >> student.id;
  41.     cout <<"Enter student score: ";
  42.     cin >> student.score;
  43.  
  44.     //Read 10 students grade information
  45.     for (i = 0; i < 10; i++)
  46.         cin >> student[i].score;
  47. }
  48.  
  49. float getAverage (data& student)
  50. {
  51.     float avg;
  52.     float sum = 0.0;
  53.     int i;
  54.  
  55.     for (i = 0; i < 10; i++)
  56.         sum = student[i].score + sum;
  57.  
  58.     avg = sum/10.0;
  59.  
  60.     return avg;
  61. }
  62.  
  63. void assignGrades (data student)
  64. {
  65.     float avg;
  66.  
  67.     if ((student.score <= avg + 10) && (student.score >= avg - 10))
  68.         student.grade = 'S';
  69.     else if (student.score > (avg + 10))
  70.         student.grade = 'O';
  71.     else if (student.score < (avg - 10))
  72.         student.grade = 'U';
  73. }
  74.  
  75. void reportResults (data student)
  76. {
  77.     float avg;
  78.     int i;
  79.  
  80.     cout << "The average is " << avg << endl;
  81.  
  82.     for (int i = 0; i < 10; i++)
  83.         cout << setw(5) << "Student " << student[i].id << endl;
  84.         cout << setw(10) << "Student's score is " << student[i].score << endl;
  85.         cout <<    setw(15) << "Student's grade is " << student[i].grade << endl; 
  86. }
Please tell me what I'm doing wrong....thanks!
Apr 28 '10 #1
1 2975
newb16
687 512MB
student (data& student) is a reference structure and not an array, it can't be indexed with []. Btw, naming local variable and global array the same name will lead to confusion.
Apr 28 '10 #2

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

Similar topics

15
by: Sue Barth | last post by:
Hi all, I hope I'm in the right ng for this... I made an .asp page that allows users to toggle thru a directory of photos. The page accomplishes the following: selects a directory of photos...
1
by: smurf | last post by:
Seems a simple question, but I can't find a simple answer: I have created a two dimensional array. I wish to send the data, a row at a time to a graph plotting routine which is expected a one...
12
by: Uncle | last post by:
I am an untrained hobbyist. Everything about programming I have learned from the internet. Thank you all for your gracious support. This is what I have: #define CONST_CHAR 0 void some_func(...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
2
by: Greg Merideth | last post by:
Using Visual C# I created two forms such as namespace test { public class SystemTray : System.Windows.Forms.Form { public createwindow() { stuff here; } public fadewindow() { stuff to fade...
6
by: Dan V. | last post by:
I would like to create a 2D string list (2D ArrayList ???). I would like to pass in a table or query as a parameter and have both columns transform into a 2D ArrayList. When I sort the one...
7
by: RayAll | last post by:
I have an array of one of my classes (Client myarray) ,I'd like to pass this array to a static method of a class for further processing what dose the calling Methods signature look like? ...
9
by: tai | last post by:
Hi. I'm looking for a way to define a function that's only effective inside specified function. Featurewise, here's what I want to do: bar_plugin_func = function() { ...; setTimeout(...);...
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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,...
0
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...

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.