473,804 Members | 2,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question on program using functions and arrays

57 New Member
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 1932
tuvok90
1 New Member
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
11778
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
8299
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 code? many thx!
11
3194
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 accomplish it. For example: int main void() { while there are more data types { print next data type; }
21
3228
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 column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
24
3464
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 declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
1
1249
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 dimensional array text file. Then x, y, and z coordinate values are assigned to each value of the 3d array. These four arrays (3d array, x, y, z) get used in quite a few functions of my program. So, I define them all as globals. My program needs to...
23
2274
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 crippled all the constructors. So now all the operations and constructors that were used do is just return BigFloats but no memory is actually accessed at any point, nor is any allocated. However, when I reenable those parts of the constructor that...
6
1668
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 to access the data like this: x->row.coll.value.d;
4
1386
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 comprise a portion of a much larger simulation. These classes exist in separate libraries although the library that class BkgWater is in links in the library containing the BkgLand class (sounds confusing I know). In the simulation architecture that...
2
2793
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 ADODB.Recordset Dim recSet2 As ADODB.Recordset Set con1 = CurrentProject.Connection Set con2 = CurrentProject.Connection Set recSet1 = New ADODB.Recordset Set recSet2 = New ADODB.Recordset
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10578
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, 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...
0
10077
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 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...
0
9152
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, 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...
1
7620
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 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...
0
6853
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();...
0
5522
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.