Array troubles?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

Re: Array troubles?

 
0
  #11
Apr 17th, 2005
Originally Posted by marinme
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

Re: Array troubles?

 
0
  #12
Apr 17th, 2005
ok, i read something about passing arrays and ive come to this point in these sections of the prgram

  1. void sortArray(string info[], int v, string course[], char grades[], int hours[])
  2. //use sortArray to sort main input array info
  3. { //into three seperate arrays to return
  4. string first; //info back for a table
  5.  
  6. first = info[v];
  7.  
  8. for(int i = 0, j = 0; i< 3 ; i++)
  9. {
  10. while(first[j] != ' ')
  11. {
  12. course[j] = first[j];
  13. j++;
  14. }
  15. j++; //skips space
  16.  
  17. while(first[j] != ' ')
  18. {
  19. grades[j] = first[j];
  20. j++;
  21. }
  22. j++; //skips space
  23.  
  24. while(first[j] != ' ')
  25. {
  26. hours[j] = first[j];
  27. }
  28. }
  29.  
  30.  
  31. }


and my main program is

  1. int main(){
  2.  
  3. int hours[50], a = 0, b = 0, c = 1, v, totalHours = 0, gpaSum = 0;
  4. char grades[50], gpaForOneClass[50], x;
  5. string course[50], info[50], z, schedule[50];
  6. float gpa = 0.0;
  7. gradevalue in;
  8.  
  9. cout<<"\nEnter a course, the letter grade received, and the credit hours. "<<endl;
  10. cin>>info[a]; //start of input
  11.  
  12.  
  13. while (info[a] != "quit" && info[a] != "QUIT") //ends input when quit or QUIT is typed
  14. {
  15.  
  16. sortArray(info, v, course, grades, hours);
  17. in = convert(in, grades[a]); //converts letter input into right value
  18. gpaForOneClass[a] = grade(in, hours[a]); //gets gpa total for one class
  19. op_grade(in); //outputs grade letter
  20. gpaSum = gpaSum + gpaForOneClass[a]; //totals up gpa
  21. totalHours = totalHours + hours[a]; //totals up hours
  22. a = a + 1;
  23.  
  24. cout<<"\nEnter the next course, the letter grade received, and the credit hours. "<<endl;
  25. cin>>info[a]; // input the next string of info
  26.  
  27.  
  28. }
  29.  
  30.  
  31. gpa = gpaAvg(gpaSum, totalHours); //calls gpa function
  32. bsort(info, c); // calls bsort function
  33. header(); //calls header of table
  34.  
  35. for (b = 0; b < a; b++) //puts the information into the table
  36. {
  37. cout<<course[b]<<" "<<hours[b]<<" "<<grades[b]<<endl;
  38. }
  39.  
  40. cout<<"The total GPA is "<<gpa<<" And you attempted "<<totalHours<<" hours."<<endl;
  41. cin>>a;
  42.  
  43. return 0;
  44. }

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 63
Reputation: marinme is an unknown quantity at this point 
Solved Threads: 1
marinme marinme is offline Offline
Junior Poster in Training

Re: Array troubles?

 
0
  #13
Apr 17th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

Re: Array troubles?

 
0
  #14
Apr 17th, 2005
Originally Posted by marinme
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:

  1. int main(){
  2.  
  3. int hours[50], a = 1, b = 1, c = 1, v, totalHours = 0, gpaSum = 0;
  4. char grades[50], gpaForOneClass[50], x;
  5. string course[50], info[50], z, schedule[50];
  6. float gpa = 0.0;
  7. gradevalue in;
  8.  
  9. cout<<"\nEnter a course, the letter grade received, and the credit hours. "<<endl;
  10. cin>>info[a]; //start of input
  11.  
  12.  
  13. while (info[a] != "quit" && info[a] != "QUIT") //ends input when quit or QUIT is typed
  14. {
  15. bsort(info, c); // calls bsort function
  16. sortArray(info, v, course, grades, hours);
  17. in = convert(in, grades[a]); //converts letter input into right value
  18. gpaForOneClass[a] = grade(in, hours[a]); //gets gpa total for one class
  19. op_grade(in); //outputs grade letter
  20. gpaSum = gpaSum + gpaForOneClass[a]; //totals up gpa
  21. totalHours = totalHours + hours[a]; //totals up hours
  22. a = a + 1;
  23.  
  24. cout<<"\nEnter the next course, the letter grade received, and the credit hours. "<<endl;
  25. cin>>info[a]; // input the next string of info
  26.  
  27.  
  28. }
  29.  
  30.  
  31. gpa = gpaAvg(gpaSum, totalHours); //calls gpa function
  32.  
  33. header(); //calls header of table
  34.  
  35. for (b = 1; b <= a; b++) //puts the information into the table
  36. {
  37. cout<<course[b]<<" "<<hours[b]<<" "<<grades[b]<<endl;
  38. }
  39.  
  40. cout<<"The total GPA is "<<gpa<<" And you attempted "<<totalHours<<" hours."<<endl;
  41. cin>>a;
  42.  
  43. return 0;
  44. }

if you change the line of:

  1. 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:
  1. 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
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

Re: Array troubles?

 
0
  #15
Apr 17th, 2005
scratch my last post... gotten beyond that... having problems with calling functions now... might post later if needed...

Thanks everybody for the help
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC