473,379 Members | 1,170 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,379 software developers and data experts.

Program Implementation

3
The following program is supposed to prompt the user for a student ID.
Open a text file.
Calculate the student's average GPA and print his transcript.

Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <vector>
  3. #include <iostream>
  4. #include <cmath>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. struct course
  11. {
  12. char session;
  13. int year;
  14. string coursename;
  15. int coursenumber;
  16. double percentage;
  17. string lettergrade;
  18.  
  19. };
  20.  
  21. struct student
  22. {
  23. vector <course> courses;
  24. string firstname;
  25. string lastname;
  26. int id;
  27. };
  28.  
  29.  
  30. void ReadStudent(string id, vector<student>& a);
  31. void PrintTranscript(const vector<student>& a);
  32. double ComputeGPA(const vector<student>& a);
  33. int main()
  34. string id;
  35. string filename;
  36. vector <student> a;
  37.  
  38. cout <<"Please enter your student ID number"<< endl;
  39. cin >> id;
  40. ReadStudent(id, a);
  41. char choice;
  42.  
  43. do
  44. {
  45. cout << "What do you wish to do with the database?" << endl;
  46. cout << "Input the relevant letter: Options are" << endl;
  47. cout << "(a) List the courses taken by the student" << endl;
  48. cout << "(b) Compute and display the student's grade point average (GPA)" << endl;
  49. cout << "(c) Open the file of another student" << endl;
  50. cout << "(d) Quit the program" << endl;
  51.  
  52. cin >> choice;
  53. cout << "You have entered:" << choice << endl;
  54. switch (choice)
  55. {
  56. case ('a'):
  57. PrintTranscript(a);
  58. break;
  59. case ('b'):
  60. ComputeGPA(a);
  61. break;
  62. case ('c'):
  63. cout << "Please enter another student ID" << endl;
  64. cin >> id;
  65. ReadStudent(id,a);
  66. break;
  67. case ('d'):
  68. {
  69. cout << "Program ending" << endl;
  70. }
  71. }
  72. }while(choice != 'd');
  73. return 0;
  74. }
  75.  
  76. void ReadStudent( string id,vector<student>& a)
  77. {
  78. student tmp;
  79. id += ".txt";
  80. ifstream infile(id.c_str());
  81. infile >> tmp.firstname;
  82. infile >> tmp.lastname;
  83. infile >> tmp.id;
  84. tmp.courses.resize(0);
  85. while(!infile.eof())
  86. {
  87. //tmp.courses.push_back(tmp); 
  88. int j = tmp.courses.size()-1;
  89. infile >> tmp.courses.at(j).session;
  90. infile >> tmp.courses.at(j).year;
  91. infile >> tmp.courses.at(j).coursename;
  92. infile >> tmp.courses.at(j).coursenumber;
  93. infile >> tmp.courses.at(j).percentage;
  94. infile >> tmp.courses.at(j).lettergrade;
  95. a.push_back(tmp);
  96. }
  97. }
  98.  
  99. void PrintTranscript( const vector <student>& a )
  100. {
  101. cout << "Here is a list of courses that you have taken:" << endl;
  102. for (int i = 0; i<a.size(); i++)
  103. {
  104. cout << a.at(i).firstname << " " << a.at(i).lastname << " " << a.at(i).id << endl;
  105. for (int j = 0; j<a.size(); j++)
  106. {
  107.  
  108. cout << a.at(i).courses.at(j).session << " " << a.at(i).courses.at(j).year << " " << a.at(i).courses.at(j).coursename << " " << a.at(i).courses.at(j).percentage << " " << a.at(i).courses.at(j).lettergrade << endl;
  109. }
  110. }
  111. }
  112.  
  113. double ComputeGPA(const vector <student>& a)
  114. {
  115. double GPA = 0;
  116. double sum = 0;
  117. for(int j = 0; j<a.size(); j++)
  118. {
  119. for( int i = 0; i<a.at(j).courses.size(); i++)
  120. {
  121. if(a.at(j).courses.at(i).lettergrade == "A+")
  122. sum += 4.0;
  123. if(a.at(j).courses.at(i).lettergrade == "A")
  124. sum += 4.0; 
  125. if(a.at(j).courses.at(i).lettergrade == "A-")
  126. sum += 3.7;
  127. if(a.at(j).courses.at(i).lettergrade == "B+")
  128. sum += 3.3;
  129. if(a.at(j).courses.at(i).lettergrade == "B")
  130. sum += 3.0;
  131. if(a.at(j).courses.at(i).lettergrade == "B-")
  132. sum += 2.7;
  133. if(a.at(j).courses.at(i).lettergrade == "C+")
  134. sum += 2.3;
  135. if(a.at(j).courses.at(i).lettergrade == "C")
  136. sum += 2.0;
  137. if(a.at(j).courses.at(i).lettergrade == "C-")
  138. sum += 1.7;
  139. if(a.at(j).courses.at(i).lettergrade == "D")
  140. sum += 1.0;
  141. if(a.at(j).courses.at(i).lettergrade == "F")
  142. sum += 0.0;
  143. GPA = sum/a.at(j).courses.size();
  144. }
  145. }
  146.  
  147. cout << "Average GPA:" << GPA << endl;
  148. return GPA;
  149. }

the test file is:

123456.txt

John Smith 123456
F 2005 ENGG 205 62 C-
W 2006 ECON 209 79 B+
W 2006 ENGG 233 85 A


I get aborted (core dumped) when running the program.

I suspect it has to do with the read function, what's wrong?
Apr 17 '07 #1
2 1738
Nusc
3
Where else can I get help?
Apr 18 '07 #2
Nusc
3
Does anyone know why?

The code compiles, but it doesn't run.
Apr 18 '07 #3

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

Similar topics

54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
9
by: Elliot Marks | last post by:
Under what circumstances would a C program, containing nothing that is not standard C, prefix a zero result of a calculation with doubles with a minus sign? I have written a program that does...
40
by: findmadhav | last post by:
I need a program in C (something like a TSR) which will automatically press the function key F6, say about every 5 seconds. Can anyone provide me with an exe of such a program? Thanks in advance.
13
by: robinsonreyna | last post by:
Hi everyone Is it possible to write a program which do not have a main() function. The program should compile and run. Please give sample code to do this.
55
by: Dev | last post by:
Hello Folks, I had faced this objective in one of my aptitude exams, that "What could be the smallest "C" program? And, as we know, smallest program means, it should execute single statement,...
8
by: Scorpio | last post by:
Hi all, I found the simple program below, no error, no warning. But it can work with no output. Does anyone know why? int main() { 0; }
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
37
by: Vince C. | last post by:
Hi all. I've installed Bloodshed Dev-C++ on a Windows 2000 SP4 machine. I'm using MinGW 3.4.2. I'd like to temporarily disable standard functions to write to stderr, i.e. for instance...
24
by: Fumeur | last post by:
Is the following program standard conform? (reduced as much as possible so it still shows the problem) /* begin */ #include <stdio.h> void xfprintf(FILE *); void xfprintf(FILE *f) {...
30
by: Anarki | last post by:
The following is the program i am trying to compile //restrict.c #include <stdio.h> int main() { char arr = "Qualifiers" char * restrict p = arr; int i = 0; for(; i < 10; ++i)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.