473,789 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing data into a function

4 New Member
I am currently writing a program which works out student marks and outputs the passes and fails two files. I have written some code but it does seem work as no names are displayed and according to this everybody has passed. My switch function is working, so why dont the other functions work? I have been creating new constructors, adding to the student class and allsorts but I just keep getting errors. My code is below

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. //This is the class definition of student
  8.  
  9. class Student
  10. {
  11. public:
  12. Student();
  13. Student(string n, int y, int m1, int m2);
  14. int getYear(){return year;}
  15. double getOverallMark();
  16. string getformatName();
  17. bool getpassed();
  18. static int GetCountPasses();
  19. static int GetCountFails();
  20.  
  21.  
  22. private:
  23. string studentName;
  24. int mark1;
  25. int mark2;
  26. int year;
  27. string studentFName;
  28. string studentSurname;
  29. string name;
  30. double overallMark;
  31. static int CountPasses();
  32. static int CountFails();
  33. string formatName();
  34. bool passed();
  35.  
  36. };
  37.  
  38. // Default constructor of the Student Class
  39. Student::Student ()
  40. {
  41. studentName = "";
  42. mark1 = 0;
  43. mark2 = 0;
  44. year = 0;
  45. }
  46.  
  47. // This is the constructor of the student class
  48. Student::Student (string n, int y, int m1, int m2)
  49. {
  50. studentName = n;
  51. mark1 = m1;
  52. mark2 = m2;
  53. year = y;
  54. }
  55.  
  56.  
  57. //Function prototype for menuselect
  58. char menuselect();
  59.  
  60. //MenuSelect function
  61. char menuselect()
  62. {
  63. char menusel;
  64. char userchoice;
  65. cout << "Display the student with the overall highest mark (H) or lowest overall mark (L)" << endl;
  66. cin >> menusel;
  67.  
  68. if (menusel == 'H' || menusel == 'h')
  69.  
  70. {
  71. userchoice = 'H';
  72. }
  73.  
  74. else if (menusel == 'L' || menusel == 'l')
  75.  
  76. {
  77. userchoice = 'L';
  78. }
  79. return userchoice;
  80. }
  81.  
  82.  
  83. // This is the major part of the program
  84. int main ()
  85. {
  86. string studentFName;
  87. string studentSurname;
  88. string student_Name;
  89. int yearStudy;
  90. int mark1;
  91. int mark2;
  92. char userChoice;
  93.  
  94. userChoice = menuselect();
  95. ifstream markfile;
  96. markfile.open("student_marks.txt");
  97. if ( markfile.fail ())
  98.  
  99. {
  100. cout << "There is an error in opening the file." << endl;
  101. cout << "The program will now close!" <<endl;
  102. exit(1);
  103. }
  104.  
  105. ofstream passes;
  106. passes.open("pass.txt");
  107. ofstream fails;
  108. fails.open("fail.txt");
  109.  
  110. while ( ! markfile.eof())
  111.  
  112. {
  113. markfile >> studentFName >> studentSurname >> yearStudy >> mark1 >> mark2;
  114. cout << "I'm in the code!" << " " << studentFName << " " << studentSurname << " "<< yearStudy << " " << mark1 << " " << mark2;
  115. student_Name = studentFName + " " + studentSurname;
  116. Student s1(student_Name, yearStudy, mark1, mark2);
  117.  
  118.  
  119. string propname;
  120. double mark;
  121. bool passed;
  122.  
  123. propname = s1.getformatName();
  124. mark = s1.getOverallMark();
  125. passed = s1.getpassed();
  126.  
  127.  
  128.  
  129. if (passed = 1)
  130. {
  131. passes << propname << " " << mark << " " << "PASS" << endl;
  132. }
  133. else if (passed = 0)
  134. {
  135. fails << propname << " " << mark << " " << "FAIL" << endl;
  136. }
  137. }
  138. markfile.close();
  139. passes.close();
  140. fails.close();
  141. return 0;
  142. }
  143.  
  144.  
  145. double Student::getOverallMark()
  146. {
  147. switch (year)
  148. {
  149.  
  150. case 1 :
  151. overallMark = static_cast<double>((mark1+mark2)/2);
  152. break;
  153.  
  154. case 2 :
  155. overallMark = static_cast<double>((mark1 * 0.4) + (mark2 * 0.6));
  156. break;
  157.  
  158. case 3 :
  159. overallMark = static_cast<double>((mark1 * 0.3) + (mark2 * 0.7));
  160. break;
  161.  
  162. default:
  163. cout << "There is an error somewhere, program will now close!!!" << endl;
  164. exit(1);
  165. }
  166. return overallMark;
  167. }
  168.  
  169.  
  170.  
  171. string Student::getformatName()
  172. {
  173. string f;
  174. string s;
  175. string i;
  176. string name;
  177.  
  178. f = "David";
  179. s = "Lee";
  180.  
  181. name = f + " " + s;
  182.  
  183. i = f.substr(0,1);
  184.  
  185. name = s + " ," + i + ".";
  186.  
  187. return name;
  188. }
  189.  
  190.  
  191.  
  192. bool Student::getpassed()
  193. {
  194. if(overallMark >= 40)
  195. return true;
  196. else
  197. return false;
  198. }
  199.  
  200.  
  201.  
  202.  
  203.  
Any help would be appriacited as I have been trying how to work out this problem for hours but to no avail.
Jan 9 '08 #1
7 1967
Savage
1,764 Recognized Expert Top Contributor
By no names are displayed,you mean nothing is written to file,or nothing is outputted with cout,or both?
Jan 9 '08 #2
howmanymiles
4 New Member
Ignore the cout statments, I just put them there to see if the code is working, and I do get an output to screen. What I meant was that there are no names in the files but the comma and full stop is in the files so the function must be working but no data must be getting passed to it.
Jan 9 '08 #3
Savage
1,764 Recognized Expert Top Contributor
Have you couted:


Expand|Select|Wrap|Line Numbers
  1. propname = s1.getformatName();
  2. mark = s1.getOverallMark();
  3. passed = s1.getpassed();
,to see what is in these variables?
Jan 9 '08 #4
howmanymiles
4 New Member
If I do that I get errors including 'getFormatName' undeclared (first use this function)
Jan 9 '08 #5
Savage
1,764 Recognized Expert Top Contributor
If I do that I get errors including 'getFormatName' undeclared (first use this function)
Perhaps you puted getFormatName instead of getformatName,o r if is this just a posting error move getformatName declaration to some place before main.
Jan 9 '08 #6
howmanymiles
4 New Member
What do you mean by the get format name declaration? and how do I pass data into the function. I want getFormatName to use studentFName and studentSurname.
Jan 9 '08 #7
sicarie
4,677 Recognized Expert Moderator Specialist
howmanymiles-

You are having very basic issues with C++ that we are not going to be able to help you with easily. As the language is case-sensitive, there is a difference between getformatName() and getFormatName(). The use of a capital F in one is referencing a completely different function.

Please read a tutorial such as this or your class' reading materials thoroughly before posting again. You're missing key conceptual elements that will take a very long time for us to explain.

That is a very good tutorial site, if you look through the others, you will probably find several things you didn't know before...
Jan 9 '08 #8

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

Similar topics

4
3601
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back in my variables ?? note1: I'm not the one who wrote the web service.. so I can't modify
58
10181
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
2
4845
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short cmd; short fdc; WORD dsf; short boxcar; short average; short chan_ena;
11
8131
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
7
2643
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I make sure that my function is being exported by adding a line to the .def file. This seems to work because when I debug it recognizes the dll and once it hits the function it goes right into the proper location of the dll.
12
2689
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
3307
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
2
5061
by: Hakan Örnek | last post by:
Hi , I want to parameter passing to my windows sevice. I call service commands like this ; '------------------------------------------------------------ Dim sc As ServiceController sc = New ServiceController("ProsetLogServices") sc.MachineName = "." If sc.Status = ServiceControllerStatus.Stopped Then sc.Start() End If
8
3505
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
4
5936
by: John Sheppard | last post by:
Hello there I was wondering if anyone could help me, I am trying to pass a typed dataset to a dialoged child form by reference. I have binding sources sitting on the child form. So to refresh them I just set their datasource. I am guessing this is probably what is causing the problem. Is there a better way to do this? Anyway this all works happily and things show up when the record already exists but I have 2 problems ; 1) When I add...
0
9499
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,...
1
10127
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,...
0
9969
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
8998
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
7519
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
5405
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
5540
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4078
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
2
3677
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.