944,018 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2620
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 16th, 2005
0

Array troubles?

Expand Post »
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....


C++ Syntax (Toggle Plain Text)
  1.  
  2. #include<iostream>
  3. #include<iomanip>
  4. #include<cmath>
  5. #include<cstdlib>
  6. #include<string>
  7. using namespace std;
  8.  
  9. enum gradevalue {a, b, c, d, e, f, w, i};
  10.  
  11. void header ()
  12. {
  13. cout<<"\nList of the Courses"<<endl<<"\n Class Hours Grade \n";
  14. }
  15.  
  16. gradevalue convert(gradevalue& in, char grades)
  17. {
  18. switch (grades)
  19. {
  20. case 'a':case 'A':
  21. in = a;
  22. break;
  23. case 'b':case 'B':
  24. in = b;
  25. break;
  26. case 'c':case 'C':
  27. in = c;
  28. break;
  29. case 'd':case 'D':
  30. in = d;
  31. break;
  32. case 'f':case 'F':
  33. in = f;
  34. break;
  35. case 'w':case 'W':
  36. in = w;
  37. break;
  38. case 'i':case 'I':
  39. in = i;
  40. break;
  41.  
  42. }
  43.  
  44. return in;
  45. }
  46.  
  47. int grade(gradevalue in, int hours )
  48. {
  49. if(in == a)
  50. return 4 * hours;
  51. if(in == b)
  52. return 3 * hours;
  53. if(in == c)
  54. return 2 * hours;
  55. if(in == d)
  56. return 1 * hours;
  57. if(in == f)
  58. return 0 * hours;
  59. if(in == w)
  60. return 0 * hours;
  61. if(in == i)
  62. return 0 * hours;
  63.  
  64. }
  65.  
  66. void op_grade(gradevalue in)
  67. {
  68. if(in == a)
  69. cout<<"A";
  70. if(in == b)
  71. cout<<"B";
  72. if(in == c)
  73. cout<<"C";
  74. if(in == d)
  75. cout<<"D";
  76. if(in == f)
  77. cout<<"F";
  78. if(in == w)
  79. cout<<"W";
  80. if(in == i)
  81. cout<<"I";
  82. }
  83.  
  84. void bsort(int arrayC[], int c)
  85. {
  86. int temp = 0, i = 0, j = 0;
  87. for (i = 1; i < c; i++);
  88. {
  89. for (j = 0; j < c - 1; j++);
  90. {
  91. if (arrayC[j] > arrayC[i])
  92. {
  93. temp = arrayC[j+1];
  94. arrayC[j+1] = arrayC[j];
  95. arrayC[j] = temp;
  96. }
  97. }
  98. }
  99. }
  100.  
  101. float gpaAvg(int gpaSum, int totalHours )
  102. {
  103. float gpa;
  104. gpa = float ((gpaSum * 100) / totalHours);
  105. return gpa;
  106. }
  107.  
  108. int main(){
  109.  
  110. int hours[50], gpaForOneClass[50], arrayC[50], a = 0, b = 0, c = 0, totalHours = 0, gpaSum = 0;
  111. char grades[50];
  112. string course[50];
  113. float gpa = 0.0;
  114. gradevalue in;
  115.  
  116. cout<<"\nEnter a course, the letter grade received, and the credit hours "<<endl;
  117. cin>>course[a]>>grades[a]>>hours[a];
  118.  
  119.  
  120. while (course[a] != "quit" || course[a] != "QUIT")
  121. {
  122.  
  123. in = convert(in, grades[a]);
  124. gpaForOneClass[a] = grade(in, hours[a]);
  125. op_grade(in);
  126. gpaSum = gpaSum + gpaForOneClass[a];
  127. totalHours = totalHours + hours[a];
  128. a = a + 1;
  129.  
  130. cout<<"\nEnter the next course, the letter grade received, and the credit hours. "<<endl;
  131. cin>>course[a]>>grades[a]>>hours[a];
  132.  
  133. }
  134.  
  135. gpa = gpaAvg(gpaSum, totalHours);
  136. bsort(arrayC, c);
  137. header();
  138.  
  139. for (b = 0; b < c; b++)
  140. {
  141. cout<<course[b]<<" "<<hours[b]<<" "<<grades[b]<<endl;
  142. }
  143.  
  144. cout<<"The total GPA is "<<gpa<<" And you attempted "<<totalHours<<" hours."<<endl;
  145.  
  146.  
  147.  
  148. return 0;
  149. }


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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Apr 16th, 2005
0

Re: Array troubles?

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


C++ Syntax (Toggle Plain Text)
  1. void sortArray(int info[], int a)
  2. {
  3. int size = 0, i;
  4. char com;
  5. string first, course[50], hour[50], grade[50];
  6.  
  7. first = info[a];
  8.  
  9. for (i = 0; i < first.length(); i = i + 1)
  10. {
  11.  
  12. while (i != ",")
  13. {
  14. course[i] = first.at(i);
  15.  
  16. }
  17. }
  18. }

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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Apr 16th, 2005
0

Re: Array troubles?

i is an integer, "," is a string. You can't compare them directly. Did you mean:
C++ Syntax (Toggle Plain Text)
  1. while ( first[i] != ',' )
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 16th, 2005
0

Re: Array troubles?

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?
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Apr 16th, 2005
0

Re: Array troubles?

Quote 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.
Well, they way you are inputting them into the arrays suggests you will need 3 different while loops within the for loop.
C++ Syntax (Toggle Plain Text)
  1. for(int i = 0, j = 0; i< 3 ; i++)
  2. {
  3. while(first[j] != ',')
  4. {
  5. course[j] = first[j];
  6. j++;
  7. }
  8. j++;//skip comma
  9. //and repeat for hour and grade
  10. }

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
marinme is offline Offline
63 posts
since Apr 2005
Apr 17th, 2005
0

Re: Array troubles?

Quote 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.
Yeah I was toying somewhat with this idea too...

I tried to put

C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Apr 17th, 2005
0

Re: Array troubles?

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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Apr 17th, 2005
0

Re: Array troubles?

Quote originally posted by jhdobbins ...
C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
marinme is offline Offline
63 posts
since Apr 2005
Apr 17th, 2005
0

Re: Array troubles?

Quote 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?
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Apr 17th, 2005
0

Re: Array troubles?

Quote originally posted by jhdobbins ...
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.

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

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

Quote ...
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?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
marinme is offline Offline
63 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: minor problem with vectors and OOP
Next Thread in C++ Forum Timeline: error checking of user input





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC