| | |
Array troubles?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
i have win xp home edition... and my compiler is dev-C++ although i dont think its a compiler issue....
This is my project needs: (my attempted code follows)
(
Write a main function with all required internal functions subprograms (defined later) that tracks your college classes, grades and GPA. Sample I/O is shown below. Your lab instructor will give you final data to run.
Input: Engl325 C 2 (class, grade, hours)
Chem111 B 4
CS110 A 4
Phys115 D 4
Stat215 F 3
Phil201 W 0
QUIT or quit
Output: List of My Courses
Class Hours Grade GP
Chem111 4 B 12
CS110 4 A 16
Engl325 2 C 6
Phil201 3 W 0
Phys115 4 D 4
Stat215 3 F 0
Total 20 38 GPA = 2.23
Rules:
1. You must define an enum type to handle all grades (A,B,C,D,F,W,I). Each grade is input as a char, then you must change the char via a switch-stmt to your enum type.
2. Your main function must contain the following subprograms:
accepts return stmt? Out or InOut?
hours and grade for 1 course GP
hours, GP (2 arrays) GPA
3 arrays: sorts ascending on Class name all 3 arrays
prints heading void
skips to next line; print 1 line of table void
3. Use cin to input each class, grade, and hours, 3 items per line.
4. Sort the classes in ascending order for strings: CS110 < Engl325 < Phys115, etc.
Your lab instructor will talk about sorting on array.
5. Make your program stop when either quit or QUIT is entered as a class.
)
and here is my attempt at it....
to further explain my problem...
The program compiles just fine... the problem is with the output..
say you input math A 3 for math an A in the class and 3 hours
it returns the value A and you input more
after 2 or 3 inputs you have to type quit 2 times, not just one for the program to quit. Also once you do this... it just states the line
"Enter a course, the letter grade received, and the credit hours" over and over with the last letter value "A" or whatever in between each of the lines... It continues this until i get an error message from windows (it also looks around like 50 times) thats why i am curious if i did my arrays wrong or my bubblesort or what?
Thanks everybody who looks at it or tries to help or anything.
This is my project needs: (my attempted code follows)
(
Write a main function with all required internal functions subprograms (defined later) that tracks your college classes, grades and GPA. Sample I/O is shown below. Your lab instructor will give you final data to run.
Input: Engl325 C 2 (class, grade, hours)
Chem111 B 4
CS110 A 4
Phys115 D 4
Stat215 F 3
Phil201 W 0
QUIT or quit
Output: List of My Courses
Class Hours Grade GP
Chem111 4 B 12
CS110 4 A 16
Engl325 2 C 6
Phil201 3 W 0
Phys115 4 D 4
Stat215 3 F 0
Total 20 38 GPA = 2.23
Rules:
1. You must define an enum type to handle all grades (A,B,C,D,F,W,I). Each grade is input as a char, then you must change the char via a switch-stmt to your enum type.
2. Your main function must contain the following subprograms:
accepts return stmt? Out or InOut?
hours and grade for 1 course GP
hours, GP (2 arrays) GPA
3 arrays: sorts ascending on Class name all 3 arrays
prints heading void
skips to next line; print 1 line of table void
3. Use cin to input each class, grade, and hours, 3 items per line.
4. Sort the classes in ascending order for strings: CS110 < Engl325 < Phys115, etc.
Your lab instructor will talk about sorting on array.
5. Make your program stop when either quit or QUIT is entered as a class.
)
and here is my attempt at it....
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<iomanip> #include<cmath> #include<cstdlib> #include<string> using namespace std; enum gradevalue {a, b, c, d, e, f, w, i}; void header () { cout<<"\nList of the Courses"<<endl<<"\n Class Hours Grade \n"; } gradevalue convert(gradevalue& in, char grades) { switch (grades) { case 'a':case 'A': in = a; break; case 'b':case 'B': in = b; break; case 'c':case 'C': in = c; break; case 'd':case 'D': in = d; break; case 'f':case 'F': in = f; break; case 'w':case 'W': in = w; break; case 'i':case 'I': in = i; break; } return in; } int grade(gradevalue in, int hours ) { if(in == a) return 4 * hours; if(in == b) return 3 * hours; if(in == c) return 2 * hours; if(in == d) return 1 * hours; if(in == f) return 0 * hours; if(in == w) return 0 * hours; if(in == i) return 0 * hours; } void op_grade(gradevalue in) { if(in == a) cout<<"A"; if(in == b) cout<<"B"; if(in == c) cout<<"C"; if(in == d) cout<<"D"; if(in == f) cout<<"F"; if(in == w) cout<<"W"; if(in == i) cout<<"I"; } void bsort(int arrayC[], int c) { int temp = 0, i = 0, j = 0; for (i = 1; i < c; i++); { for (j = 0; j < c - 1; j++); { if (arrayC[j] > arrayC[i]) { temp = arrayC[j+1]; arrayC[j+1] = arrayC[j]; arrayC[j] = temp; } } } } float gpaAvg(int gpaSum, int totalHours ) { float gpa; gpa = float ((gpaSum * 100) / totalHours); return gpa; } int main(){ int hours[50], gpaForOneClass[50], arrayC[50], a = 0, b = 0, c = 0, totalHours = 0, gpaSum = 0; char grades[50]; string course[50]; float gpa = 0.0; gradevalue in; cout<<"\nEnter a course, the letter grade received, and the credit hours "<<endl; cin>>course[a]>>grades[a]>>hours[a]; while (course[a] != "quit" || course[a] != "QUIT") { in = convert(in, grades[a]); gpaForOneClass[a] = grade(in, hours[a]); op_grade(in); gpaSum = gpaSum + gpaForOneClass[a]; totalHours = totalHours + hours[a]; a = a + 1; cout<<"\nEnter the next course, the letter grade received, and the credit hours. "<<endl; cin>>course[a]>>grades[a]>>hours[a]; } gpa = gpaAvg(gpaSum, totalHours); bsort(arrayC, c); header(); for (b = 0; b < c; b++) { cout<<course[b]<<" "<<hours[b]<<" "<<grades[b]<<endl; } cout<<"The total GPA is "<<gpa<<" And you attempted "<<totalHours<<" hours."<<endl; return 0; }
to further explain my problem...
The program compiles just fine... the problem is with the output..
say you input math A 3 for math an A in the class and 3 hours
it returns the value A and you input more
after 2 or 3 inputs you have to type quit 2 times, not just one for the program to quit. Also once you do this... it just states the line
"Enter a course, the letter grade received, and the credit hours" over and over with the last letter value "A" or whatever in between each of the lines... It continues this until i get an error message from windows (it also looks around like 50 times) thats why i am curious if i did my arrays wrong or my bubblesort or what?
Thanks everybody who looks at it or tries to help or anything.
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
supposing no one wanted to talk that
(i know i wouldnt)... ill shrink things down here.. as i have made progress 
I created one input array called info[] for 3 objects and when inputted... seperated by a comma.
I need to sort this out now into 3 seperate arrays. I think i got the first one seperated but im not sure where to go after i have the first array set up...
also this section of the coding...
the line while(i !=",") returns the error message
ISO C++ forbids comparison between pointer and integer...
Thanks in advance.
(i know i wouldnt)... ill shrink things down here.. as i have made progress 
I created one input array called info[] for 3 objects and when inputted... seperated by a comma.
I need to sort this out now into 3 seperate arrays. I think i got the first one seperated but im not sure where to go after i have the first array set up...
C++ Syntax (Toggle Plain Text)
void sortArray(int info[], int a) { int size = 0, i; char com; string first, course[50], hour[50], grade[50]; first = info[a]; for (i = 0; i < first.length(); i = i + 1) { while (i != ",") { course[i] = first.at(i); } } }
also this section of the coding...
the line while(i !=",") returns the error message
ISO C++ forbids comparison between pointer and integer...
Thanks in advance.
i is an integer, "," is a string. You can't compare them directly. Did you mean:
C++ Syntax (Toggle Plain Text)
while ( first[i] != ',' )
I'm here to prove you wrong.
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
yeah, that helped that problem... but I am still confused about how to further distingush what i type in as the "course, grade, hour" as three different arrays.
Also, the endless loop i had prior is now fixed, however i am curious why it still asked me to input more information after i type quit. Would it be better if i change it to an if statement or is there just a simple error in my order?
Also, the endless loop i had prior is now fixed, however i am curious why it still asked me to input more information after i type quit. Would it be better if i change it to an if statement or is there just a simple error in my order?
•
•
Join Date: Apr 2005
Posts: 63
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by jhdobbins
yeah, that helped that problem... but I am still confused about how to further distingush what i type in as the "course, grade, hour" as three different arrays.
C++ Syntax (Toggle Plain Text)
for(int i = 0, j = 0; i< 3 ; i++) { while(first[j] != ',') { course[j] = first[j]; j++; } j++;//skip comma //and repeat for hour and grade }
but then again, you could always just use a 2d array and put it inside the for loop to go through the 3 you need.
You may also want to look into how you are going to return these three arrays, right now, they expire at the end of the sortArray function.
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
•
•
•
•
Originally Posted by marinme
You may also want to look into how you are going to return these three arrays, right now, they expire at the end of the sortArray function.
I tried to put
C++ Syntax (Toggle Plain Text)
void sortArray(int info[], int a, int& course[], int& hour[], int& grade[])
and i get declaration of course as array of references as a compiling error...
i can mess with it and get rid of that im sure... I know i have to pass them like that somehow because you cant just return the three values with a simple return statement.
I'm pretty new to programming if you cant tell..

Thanks for the help.
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
here is the entire program with some comments...
Its probably a bit easier to read now...
Sorry if I am a pain for anyone...
Thanks alot.
#include<iostream> // all the classes i could ever need for this program #include<iomanip> // and a few extra just incase. #include<cmath> #include<cstdlib> #include<string> using namespace std; enum gradevalue {a, b, c, d, e, f, w, i}; //declared so it can be used globally //these are the letter grade values void header () // this section will display the heading for the output table { cout<<"\nList of the Courses"<<endl<<"\n Class Hours Grade \n"; } gradevalue convert(gradevalue& in, char grades) //switch stmt to get all the { //letters into the enum switch (grades) //catagories { case 'a':case 'A': in = a; break; case 'b':case 'B': in = b; break; case 'c':case 'C': in = c; break; case 'd':case 'D': in = d; break; case 'f':case 'F': in = f; break; case 'w':case 'W': in = w; break; case 'i':case 'I': in = i; break; } return in; } int grade(gradevalue in, int hours) //used to calculate the totalgpa for { //one class if(in == a) return 4 * hours; if(in == b) return 3 * hours; if(in == c) return 2 * hours; if(in == d) return 1 * hours; if(in == f) return 0 * hours; if(in == w) return 0 * hours; if(in == i) return 0 * hours; } void op_grade(gradevalue in) //outputs the letter grade received in a class { if(in == a) cout<<"A"; if(in == b) cout<<"B"; if(in == c) cout<<"C"; if(in == d) cout<<"D"; if(in == f) cout<<"F"; if(in == w) cout<<"W"; if(in == i) cout<<"I"; } void bsort(string info[], int c) //This bubblesort is used to put the classes into { //alphabetical order int i = 0, j = 0; string temp; for (i = 1; i < c; i++); { for (j = 0; j < c - 1; j++); { if (info[j] > info[i]) { temp = info[j+1]; info[j+1] = info[j]; info[j] = temp; } } } } void sortArray(int info[], int a) //use sortArray to sort main input array info{ //into three seperate arrays to return string first, course[50], hours[50], grades[50]; //info back for a table first = info[a]; for(int i = 0, j = 0; i< 3 ; i++) { while(first[j] != ',') { course[j] = first[j]; j++; } j++; //skips comma while(first[j] != ',') { hours[j] = first[j]; j++; } j++; //skips comma while(first[j] != ',') { grades[j] = first[j]; } } } float gpaAvg(int gpaSum, int totalHours ) //This is used to calculate overall gpa { //gpa = totalofgpa / totalhours float gpa; gpa = float (gpaSum / totalHours); return gpa; } int main(){ int hours[50], a = 0, b = 0, c = 1, totalHours = 0, gpaSum = 0; char grades[50], gpaForOneClass[50], x; string course[50], info[50], z; float gpa = 0.0; gradevalue in; cout<<"\nEnter a course, the letter grade received, and the credit hours. "<<endl; cin>>info[a]; //start of input while (info[a] != "quit" || info[a] != "QUIT") //ends input when quit or QUIT is typed { in = convert(in, grades[a]); //converts letter input into right value gpaForOneClass[a] = grade(in, hours[a]); //gets gpa total for one class op_grade(in); //outputs grade letter gpaSum = gpaSum + gpaForOneClass[a]; //totals up gpa totalHours = totalHours + hours[a]; //totals up hours a = a + 1; cout<<"\nEnter the next course, the letter grade received, and the credit hours. "<<endl; cin>>info[a]; // input the next string of info } gpa = gpaAvg(gpaSum, totalHours); //calls gpa function bsort(info, c); // calls bsort function header(); //calls header of table for (b = 0; b < c; b++) //puts the information into the table { cout<<course[b]<<" "<<hours[b]<<" "<<grades[b]<<endl; } cout<<"The total GPA is "<<gpa<<" And you attempted "<<totalHours<<" hours."<<endl; return 0; }
Its probably a bit easier to read now...
Sorry if I am a pain for anyone...
Thanks alot.
•
•
Join Date: Apr 2005
Posts: 63
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by jhdobbins
C++ Syntax (Toggle Plain Text)
void sortArray(int info[], int a, int& course[], int& hour[], int& grade[])
and i get declaration of course as array of references as a compiling error...
i can mess with it and get rid of that im sure... I know i have to pass them like that somehow because you cant just return the three values with a simple return statement.
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
•
•
•
•
Originally Posted by marinme
Pass them as pointers and treat them as arrays, just don't try to reallocate or deallocate memory and you should be fine. Though I'm sure someone will post in here about how I'm wrong on this, but it will get the job done and I'm sure you're probably not going to be re-using this.
This is probably going to sound stupid...
but i have no idea what a pointer is.... and as for the memory?? i dont know how to do either of those purposely....
as for reusing it... this program is due tomorrow/ tomorrow night... along with another one like it.... I am supposed to use this base code but put the functions into their own classes and the call them with a header file... I understand most of how to do that and that isnt what I even want to think about right now... my main concern is getting this program running and working

but please... explain what a pointer is - i know call by value and call by reference if thats what it is...
Also, to swap the arrays around and sort them out and everything... is calling them into the function by an int okay or do i need to turn them into strings?
•
•
Join Date: Apr 2005
Posts: 63
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by jhdobbins
but i have no idea what a pointer is....
•
•
•
•
Originally Posted by jhdobbins
I am supposed to use this base code but put the functions into their own classes and the call them with a header file...
•
•
•
•
Originally Posted by jhdobbins
but please... explain what a pointer is - i know call by value and call by reference if thats what it is...
Here is a link to a tutorial here on daniweb that explains them decently.
•
•
•
•
Also, to swap the arrays around and sort them out and everything... is calling them into the function by an int okay or do i need to turn them into strings?
Any other questions?
![]() |
Other Threads in the C++ Forum
- Previous Thread: minor problem with vectors and OOP
- Next Thread: error checking of user input
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






