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

question on program using functions and arrays

I am reading in different names and scores from a file. I have created a struct that is made up of arrays and a string. Im tring to but the string and scores in the correct array by using a function. I am having trouble and im stuck if anyone can provide some insight I would appreciate it.

this is what i have so far.....


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7.  
  8. //structure that holds student record information.
  9. struct records
  10. {
  11.         string name;
  12.         float quizzes[7];
  13.         float projects[6];
  14.         float exams[2];
  15.         float labs[14];
  16. };
  17.  
  18. const int num_quiz = 7;
  19. const int num_exams = 2;
  20. const int num_projects = 6;
  21. const int num_labs = 14;
  22.  
  23.  
  24. const int quiz_pts = 70;
  25. const int exam_pts = 200;
  26. const int projects_pts = 300;
  27. const int lab_pts = 150;
  28.  
  29. get_scores(string name, int num_scores);
  30.  
  31. int main()
  32. {
  33.         // Ask the user for a file name
  34.         cout << "Enter a file name: ";
  35.         string file;
  36.         cin >> file;
  37.         cout << file;
  38.  
  39.         int counter = 1;
  40.         ifstream in_file;
  41.  
  42.         record grades;
  43.         // Define a file input stream and open the file
  44.         in_file.open("file.txt"); //opening file
  45.  
  46.         if (in_file.fail())
  47.         {
  48.         // Failed to open the file (file doesn't exist or isn't readable)
  49.         cout << "Could not open file: " << file  << "\n";
  50.         exit(1);
  51.         }
  52.  
  53.  
  54.         float quiz_ave = 0.0;
  55.         float exam_ave = 0.0;
  56.         float project_ave = 0.0;
  57.         float lab_ave = 0.0;
  58.  
  59.         // Repeatedly get characters from the file
  60.  
  61.  
  62.         get_scores(num_quiz);   //function for inputing info in struct.
  63.  
  64.         get_scores(num_projects);
  65.  
  66.         get_scores(num_exams);
  67.  
  68.         get_scores(num_labs);
  69.  
  70.     // Close the file
  71.     in_file.close();
  72.  
  73.  
  74.     return 0;
  75. }
  76.  
  77. //function that takes in parameters to input scores in array
  78. get_scores(int num_scores)
  79. {
  80.         int index = 0;
  81.         in_file >> student.name;
  82.  
  83.         for (int i = 0; i < num_scores; i++)
  84.         {
  85.          in_file >> student.quizes[i];
  86.         float quiz_ave += student.quizes[i];
  87.         }
  88.  
  89. }
thanks
May 10 '07 #1
1 1915
I am reading in different names and scores from a file. I have created a struct that is made up of arrays and a string. Im tring to but the string and scores in the correct array by using a function. I am having trouble and im stuck if anyone can provide some insight I would appreciate it.

this is what i have so far.....

thanks
Here are some observations to help you debug your program:

Function prototypes must match the actual function signature. Take a look at your prototype for get_scores and then look at the actual function signature.

Your in_file variable is not in the proper "scope" to be used in get_scores function. It's declared in main so it is only visible within main.

The input stream in_file is extracting "strings" from the input. You must convert the input to the proper data type (in this case "float") before you can do arithmetic operations on the data.

It looks like you want to store your data into a variable of type "struct records" named "student". student isn't declared anywhere. Here is an example of creating struct variables:
struct records {
int age;
} myRecord; // myRecord1, myRecord2, myRecord3; declares 3 records
myRecord.age = 20; // now I can do something with myRecord

You could also create a variable this way:
struct records myRecord;

These are just technical observations. These might help your program to compile, but they won't fix any logical errors. It looks to me like your algorithm won't work as you might expect. I can't write the program for you, else you won't learn. But I will suggest the following:

Think about how your input file is laid out. If you haven't already, try writing your algorithm out on paper (pseudocode) and step through your code by hand - (pretend you are the computer). Don't worry about the implementation details at first, just get a high level algorithm and then refine it - aka "top down stepwise refinement". Assuming this is a school project - don't be afraid to ask your teacher or TA for help if you get stuck.


Here are some online C++ tutorials:
http://www.cprogramming.com/tutorial.html

Finally, here is a link to some awesome online C++ books, these books are a bit heavy for beginners, I'd suggest going through the tutorials first and/or get through an introductory book first.
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

I hope my suggestions help. Good Luck:)
May 10 '07 #2

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

Similar topics

7
by: csx | last post by:
Hi everyone! two quick questions relating to arrays. Q1, Is it possible to re-assign array elements? int array = {{2,4}, {4,5}}; array = {2,3}
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
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...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
1
mikejfe
by: mikejfe | last post by:
Hi all. I have a question that is probably simple to answer, but I don't even know where to get started (i.e. for searching old posts, Google, etc.) I am writing a program that reads in a 3...
23
by: mike3 | last post by:
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even...
6
by: jason | last post by:
Hello, I have a question about what kind of datastructure to use. I'm reading collumn based data in the form of: 10\t12\t9\t11\n 24\t11\t4\t10\n ..... I now have a structure which allows me...
4
by: BSand0764 | last post by:
Apologies for the length of this message, but I'm having problems getting an alternate function to be executed via a functor implementation. I have two classes (BkgLand and BkgWater) that...
2
by: Kosmos | last post by:
I am opening the following connections as such although I am only referring to the first connection for this question: Dim con1 As ADODB.Connection Dim con2 As ADODB.Connection Dim recSet1 As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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
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.