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....

#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.

Recommended Answers

All 14 Replies

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...

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:

while ( first[i] != ',' )

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?

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.

Well, they way you are inputting them into the arrays suggests you will need 3 different while loops within the for loop.

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.


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.

Yeah I was toying somewhat with this idea too...

I tried to put

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.

here is the entire program with some comments...

#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.

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.

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.

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?

but i have no idea what a pointer is....

And... they are having you do this without learning about pointers??? Well, if they haven't explained pointers, you probably shouldn't use them.

I am supposed to use this base code but put the functions into their own classes and the call them with a header file...

ok, if it's going to be in a class, why not make them data members of the class and just omit declaring them in the function or passing them to the function, just use them from the class..

but please... explain what a pointer is - i know call by value and call by reference if thats what it is...

a pointer is basically as it says... something that points.. in C/C++ syntax it points to addresses in memory..

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?

I'm not sure what you mean calling them into the function by an int... do you mean passing them as arguments for the function as an int type? If so, this won't be the best way, you should send them as you are getting them - type char.

Any other questions?

And... they are having you do this without learning about pointers??? Well, if they haven't explained pointers, you probably shouldn't use them.

Then how do i do it?? i dont understand.

ok, if it's going to be in a class, why not make them data members of the class and just omit declaring them in the function or passing them to the function, just use them from the class..

for the class thing... that is a seperate assignment after this one is finished

I'm not sure what you mean calling them into the function by an int... do you mean passing them as arguments for the function as an int type? If so, this won't be the best way, you should send them as you are getting them - type char.

yep that is exactly what i meant

Any other questions?

none at this moment, give like like 5 minutes... ha

ok, i read something about passing arrays and ive come to this point in these sections of the prgram

void sortArray(string info[], int v, string course[], char grades[], int hours[]) 
                                                      //use sortArray to sort main input array info
{                                                       //into three seperate arrays to return
    string first;   //info back for a table
     
    first = info[v];
     
    for(int i = 0, j = 0; i< 3 ; i++)
    {
            while(first[j] != ' ')
            {
                           course[j] = first[j];
		                   j++;
            }
	        j++;  //skips space
	
	        while(first[j] != ' ')
	        {
                           grades[j] = first[j];
                           j++;
            }
            j++;  //skips space
            
            while(first[j] != ' ')
            {
                           hours[j] = first[j];
            }
    }
                                               
     
}

and my main program is

int main(){
    
    int hours[50], a = 0, b = 0, c = 1, v, totalHours = 0, gpaSum = 0; 
    char grades[50], gpaForOneClass[50], x;
    string course[50], info[50], z, schedule[50];  
    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
    {
          
          sortArray(info, v, course, grades, hours);
          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 < a; 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; 
    cin>>a;
    
    return 0;
}

my question here is this...

after i input the info string the first time... I believe it goes right into sortArray and does its thing...

but my program ends suddenly as soon as i input things when its run.

Also if i type quit right away, i get an error message from windows and it automatically closes like all programs do with error messages. Where is this problem? I feel i am array illiterate as i work more and more on this.

my question here is this...

after i input the info string the first time... I believe it goes right into sortArray and does its thing...

Setup breakpoints along the way or just step through the program to see how it is working. I've never used Dev-C++, but in MSVC++ you are able to see your data during the breakpoints, just make sure they are inputting what you have put in there... Pretty much, when you come up with problems like this, you want to eliminate code until it works fine(should have been compiling along the way anyways), then just add a bit and see if it works like you want to, and repeat. This will help you find what part of the code is being troublesome.

but my program ends suddenly as soon as i input things when its run.

Also if i type quit right away, i get an error message from windows and it automatically closes like all programs do with error messages. Where is this problem? I feel i am array illiterate as i work more and more on this.

What error message are you getting?

Setup breakpoints along the way or just step through the program to see how it is working. I've never used Dev-C++, but in MSVC++ you are able to see your data during the breakpoints, just make sure they are inputting what you have put in there... Pretty much, when you come up with problems like this, you want to eliminate code until it works fine(should have been compiling along the way anyways), then just add a bit and see if it works like you want to, and repeat. This will help you find what part of the code is being troublesome.

I have been doing this...

in this part of the code:

int main(){
    
    int hours[50], a = 1, b = 1, c = 1, v, totalHours = 0, gpaSum = 0; 
    char grades[50], gpaForOneClass[50], x;
    string course[50], info[50], z, schedule[50];  
    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
    {
          bsort(info, c);           // calls bsort function
          sortArray(info, v, course, grades, hours);  
          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
    
    header();  //calls header of table
    
    for (b = 1; b <= a; 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; 
    cin>>a;
    
    return 0;
}

if you change the line of:

sortArray(info, v, course, grades, hours);

to a comment or take it out

the program has you input info as always, after you input info.. it asks you to input more however it will print the line:

Enter the next course, the letter grade received, and the credit hours. "

3 times if you enter in info such as cs110 a 3
2 times if you enter in info such as cs110 a
and only once if you just enter in cs110

when you type quit

it displays information from the header function so that part reads right... but
the output is all messed up... It will print a number followed by an ascii character for a few lines

then it prints out the output for this line:

cout<<"The total GPA is "<<gpa<<" And you attempted "<<totalHours<<" hours."<<endl;

however on that line.. the gpa is always 0 and the hours are some very very high number... usually in the millions.


What error message are you getting?

When you leave the line

sortArray(info, v, course, grades, hours);

in the program and run the program (compiles just fine)...

when you type in any info the program immediately closes as before
when you type quit... the window pops up that says grades.exe has encountered a problem and needs to close. we are sorry... just like it always does for any error a program experiences in windows and then lets you report it or not.

scratch my last post... gotten beyond that... having problems with calling functions now... might post later if needed...

Thanks everybody for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.