Begginer program using Arrays, and File input output



Last edited on
Have you tried? The tutorial at this site covers this.
Last edited on
Here's what I have so far
[
Last edited on
Have you tried to compile it?

(No, obviously not...)
Change this
in SIZE;

To this
int SIZE;

near beginning
Last edited on
haha no I've just started working on it over the past few days and that was just the basics I kind of started with
Maybe this will suit you... not to change your style or anything.. but this is just a start..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Student grading

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    
    ofstream grades;
    int id[99];
    int grade[99];
    int score[99];
    string name[99];
    int average[99];
    int i = 0;
    
    cout << "Welcome to the student grade organizer" << endl;
    cout << "The file of grades and names will be saved in the same folder as this" << endl;
    system("pause");
    system("cls");
    
    cout << "How many students are there (1 - 100)? " //Variable starts at 0
    cin >> i;
    
    for (; i>1; --i) //My heads hurting as to whether to count down or up
    {
        cout << "What is the name of the student? ";
        cin >> name[i]
        }
    
    return 0;
}


I haven't really looked through.. it... but its a start.. sorry there isn't any comments.. your code was missing headers for example <fstream> and <iostream> and <string>.. i really had to rush through it.. so if it's unorganized i'm sorry...
Last edited on
...
Last edited on
Look to the right of the text box -->

and you'll see this --> #

click it.. and it will spawn a

(code) (/code) but the brackets are actually []

put your code in between that..

and by the way, if you need further help, we could work on this together.
Last edited on
...
Last edited on
I see, you have to use functions, well since its your project, we can stick to your code but add these to the headers

1
2
#include <fstream>
#include <string> 


And when starting out.. comments are a good habit they help you remember.

Comment = // I am a one line comment

or

Comment =
1
2
3
/* I am a
Multi-line
comment */ 


These pages should help you with your project

http://www.cplusplus.com/doc/tutorial/files.html -- That's for I/O

http://www.cplusplus.com/doc/tutorial/arrays.html -- That's for arrays

http://www.cplusplus.com/doc/tutorial/control.html -- That's for loops (such as for, or while)
Last edited on
Yea he basically said we had to use those basic functions I had up their.
void inputStudentInfo(int IDs[], string studentNames[], float scores[], int& numOfStudents)
1
2
void inputStudentInfo(int[], string[], float[], int&)
[code]
int main()[/code]

Thanks I forgot to add those libraries in
There's quite a bit missing, but you've got (a week I think), so you should be fine. Functions are a good practice if used properly, this site's tutorial has everything you need. You can get it done within 30 minutes (If it took 10 minutes to read, and even if you needed reference and checked back another 20 minutes to actually code). You should try compiling it, if it doesn't compile, read the errors and fix em, if you got a problem that you can't solve (no matter how stupid) bring it to the forums =D. (Lol, I don't wanna sound like a teacher)
Last edited on
Thanks legend I'll post any problems or errors I cant't figure out or if I get stuck no matter how stupid they are here
That's the spirit!

Happy Coding
_
.____ .___
| | ____ ____ ____ ____ __| _/
| | _/ __ \ / ___\_/ __ \ / \ / __ | |
| |__\ ___// /_/ > ___/| | \/ /_/ |
|_______ \___ >___ / \___ >___| /\____ |
\/ \/_____/ \/ \/ \/

lol.. sorry.. I love ASCII
...
Last edited on
Everyday and Night.

If you ever need me to come on or something,

email = johnathaneb@hotmail.com

and you can always find me on facebook

Johnathan Brown is the name

with a picture of a "J.B." in graffiti
Last edited on
...
Last edited on
So, I just built it today. It's got quite a bit of functions. May not be exactly what your looking for. We'll walk through it, and I'll show ya how it's done, I used a function to calculate the avg, a function, to output data to the file, a function to get info, and a function to assign the grades, and there are arrays too so yeah, talk to me on facebook when your ready (if im not online it gets sent to my phone so when you send I'll be on about a minute later but only after 7) because I"m working and can't get to facebook yet... Happy CODING!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Student Grading system

//int i is used as a local variable alot to keep track of stuff

#include <iostream> // Mandatory
#include <fstream> // File I/O
#include <string> // Mandatory for strings

void getInfo(); // Function getInfo
void findAvg(); // Function findAvg
void giveGrade(); // Function giveGrade
void output(); // Function output

using namespace std; //Standard Namespace

//Declarations
float avg = 0;
int ID[200];
string name[200];
float score[200];
string grade[200];
signed int pk=-1; //PK is the incrementer
ofstream file;
string filename;

//Main
int main(void)
{
    //START OF PROGRAM
    system("color 0a"); // Changes system text to green/0a
    system("cls"); // Clears the screen
    // Introduction
    //Make it look good
    cout << "-------------------------------------------" << endl;
    cout << "|Welcome to my Student grading organizer!!|" << endl;
    cout << "-------------------------------------------" << endl << endl;
    // Notifies where file will be saved
    cout << "This program will create a file called 'Grades.txt'" << endl; 
    cout << "in the ORIGINAL folder it was placed in." << endl;
    system("pause"); // Give time to read
    // Calls getInfo
 getInfo ();
    // Calls findAvg 
 findAvg ();
    // Calls giveGrades
 giveGrade();
    // Calls output
 output();
 
 // This is for after output.. Notification of saving
 cout << "Saving file 'Grades.Txt'" << endl;
 // Give Time to Read
 system("Pause");
 //END OF PROGRAM
 return 0;
}

void getInfo() //Used to Gather information
{
     string more = "y"; //While More = True.... Loop
     int i = 0; //To ensure Score is actually integer
     
     while (more=="y")
     
     {     
           pk++; //Incremente
           ID[pk] = pk + 1; //ID Count = PK..
           system("cls");
           cin.sync(); // Clear buffer for each loop
           
           cout << "What is the name of the student? "; //Get name
           getline( cin, name[pk] ); //Put into name[pk]
           
           
           cout << "What is the score of the student? "; //Get score
           
           //Ensure that score is actually an integer
           while (!(cin >> i))
           {
		   cin.clear();
		   cin.ignore(1000,'\n');
		   //If not an integer, repeat until so
		   cout << "What is the score of the student (integer value's only): ";
           }
           score[pk] = i; //If so.. Put score in here
           

           cout << "Would you like to add another student? ";
           cin >> more; //Ask if they'd like to add another student
           
           while (more != "y" && more != "n")
           //While more is not y & not n
           {
                 cout << "(y or n) only please: ";
                 cin >> more;
           }
           /*If it's n... it goes back to main.. to continue next function
             Otherwise it will go back to the beginning of this function*/
           }
}

void findAvg() //Used to calculate the average
{
for (int i=0; i<pk + 1; i++) /* As long as i is less than pk
                                score[1], score[2], and so on
                                will be added to avg*/
{
avg = avg + score[i];
}
avg = avg / (pk + 1); /*To find the average.. 
                      Take the total number of scores (currently in avg)
                      and divide it by pk + 1 because pk starts at -1*/

cout << "Average = " << avg << "%" << endl; /*Display avg and add %
                                              so it looks professional*/
system("pause");    //Give user time to see average
}

void giveGrade()
{ 
 
 /*Standards are as follows
  A student within 5 points of the average gets a B
  A student with more than 5 points above the average gets an A
  A student with more than 5 and within 15 points below the average gets a C
  A student with more than 15 points below the average gets a D 
 */
 
  for(int i = 0; i < pk + 1;i++) //While i is less than pk.. do this
  {
        if (score[i] >= avg + 5) //Standards for an A
        {            //Greater than or = to the average + 5
        grade[i] = "A";
        }
        else if  (score[i] <= avg + 4 && score[i] >= avg - 4)
        {                  //Less than or = to +4 & greater than or = to -4
             grade[i] = "B"; //Standards for a B
             }
             else if (score[i] <= avg -5 && score[i] >= avg - 14)
             {
                  grade[i] = "C"; //Standards for a C
                  }
                  else if (score[i] < avg - 15)
                  {
                      grade[i] = "D"; //If none of the above apply.. Give D
                      }
                                        
  }
}

void output()
{
     //ofstream file declared above
     //Open a file called Grades.txt
     //If non-existant it will be created
     file.open("Grades.txt");
     //Display average in text file
     file << "The Average is: " << avg << "%" << endl << endl;
     
     //Local Variable i is used again
     //while i is less than pk put the array into the textfile
     //For example
     /*
     ID[1] Produces 1
     name[1] Produces Name that was put first
     score[1] Produces score that was put in first
     grade[1] Produces first grade calculated
     << ends the line
     the "" are to make it look neater
     */
     for(int i = 0; i < pk + 1;i++)
     {
     file << ID[i] << ", " << name[i] << ", " << score[i] << "%, " << grade[i] << endl;
     cout << ID[i] << ", " << name[i] << ", " << score[i] << "%, " << grade[i] << endl;
     }
     //Save and close the file.. Go Back to main
     file.close();
}


It's all commented so you can learn as much as you can. Have fun.. NOW UPDATED!! D now gives correct value.. and Output is displayed to the screen while it is being put in a text file
Last edited on
Topic archived. No new replies allowed.