5 Replies - 1187 Views - Last Post: 25 October 2006 - 10:27 AM Rate Topic: -----

#1 the_rook13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 02-October 06

Strings/Arrays

Posted 24 October 2006 - 06:37 PM

I am trying to get the name to match the grade. But i can't figure out why it wont work. This is just a little bit of the code
#include <cstdlib>
#include <iostream>
using namespace std;
int main()

{
 double arrystudent_grades[4][5]={{5,8,9,8,6},{2,5,10,5,3},{8,8,8,8,9},{10,9,8,7,6}};
  int grade;
  int student;
  int student_id;
  int num_students=4;
  int num_grades=5;
 char menu_response;
 char strstud_name[4][35] = {"Amy","Ben","Cathy","Doug"};
 int stud,name;
int quiz_num;
  double grade_change;
  int main()

for(student=0;student<num_students;++student)
   {
   cout<<"\nGrades for students"<<student+1<<":\t";
  for(grade=0;grade<num_grades;++grade)
  cout<<arrystudent_grades[student][grade]<<"\t";
  cout<<"\n";
}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Strings/Arrays

#2 dragonlady  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 07-August 05

Re: Strings/Arrays

Posted 24 October 2006 - 09:08 PM

Okay there's a few things happening here. First of all you've got two int main() calls. Is the problem that it's not compiling, incorrect output, what?
Was This Post Helpful? 0
  • +
  • -

#3 the_rook13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 02-October 06

Re: Strings/Arrays

Posted 25 October 2006 - 05:42 AM

sorry about that. Iam tryig to get it to look like this.
Rob 5 8 9 8 6
Joe 2 5 10 5 3
Kodie 8 8 8 8 9
Doug 10 9 8 7 6

#include <cstdlib>
#include <iostream>
using namespace std;
int main()

{
 double arrystudent_grades[4][5]={{5,8,9,8,6},{2,5,10,5,3},{8,8,8,8,9},{10,9,8,7,6}};
  int grade;
  int student;
  int student_id;
  int num_students=4;
  int num_grades=5;
 char menu_response;
 char strstud_name[4][35] = {"Rob","Joe","kodie","Doug"};
 int stud,name;
int quiz_num;
  double grade_change;

 cout<<"Output Progress report\n";
	for(student=0;student<num_students;++student)
   { 
   cout<<strstud_name[]<<strstud_name[]+1<<":\t";
  for(grade=0;grade<num_grades;++grade)
  cout<<arrystudent_grades[student][grade]<<"\t";
  cout<<"\n";
}
system("PAUSE");
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Strings/Arrays

Posted 25 October 2006 - 06:01 AM

View Postthe_rook13, on 25 Oct, 2006 - 05:42 AM, said:

sorry about that. Iam tryig to get it to look like this.
Rob 5 8 9 8 6
Joe 2 5 10 5 3
Kodie 8 8 8 8 9
Doug 10 9 8 7 6

Something to consider.. choice of data type can adversely impact on coding...

Something that takes that form suggests there is a better way to group your data..

did you consider using a struct?
Was This Post Helpful? 0
  • +
  • -

#5 dragonlady  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 07-August 05

Re: Strings/Arrays

Posted 25 October 2006 - 09:33 AM

Gregory's suggestion to use a struct would indeed help to organize your code better. A struct is like an object type; in this case it would be logical to have, for instance, a Student struct, and the fields would be a string for a name and an array of numbers representing grades.

However, for this case, it's possible to achieve your desired output as is. Let's look at the following lines.
for(student=0;student<num_students;++student)
   { 
   cout<<strstud_name[]<<strstud_name[]+1<<":\t";
  for(grade=0;grade<num_grades;++grade)
  cout<<arrystudent_grades[student][grade]<<"\t";
  cout<<"\n";
}




The line cout<<strstud_name[]<<strstud_name[]+1<<":\t"; is going to give you a compiler error. You are trying to access an individual element of an array, yet with strstud_name[], you are not supplying an index. You'd have to put a number 0 through 3 in those square brackets. Since you're using a for loop, O'd put "student" in those brackets. In the next cout line, you got it right. Also, referring back to the line I just mentioned,
cout<<strstud_name[]<<strstud_name[]+1<<":\t";
you don't need the "strstud_name[]+1" part. I think what you're trying to do is output the next student name (correct me if I'm wrong), but you already have the for loop doing it for you.

Try those two suggestions and I believe it should work.
Was This Post Helpful? 0
  • +
  • -

#6 the_rook13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 02-October 06

Re: Strings/Arrays

Posted 25 October 2006 - 10:27 AM

Yea thats what i was trying to do thanks for all the help it works now!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1