GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Apr-2005, 13:21
x3ro x3ro is offline
New Member
 
Join Date: Apr 2005
Posts: 10
x3ro is on a distinguished road
Unhappy

GradeBook Program


I Am new to C++. This is supposed to create a grade book using structs that takes in an id number a student name and a student grade and have to use an arrary to include multiple inputs. I also need to create functions to average and sum the grades of all the students.

So the output is supposed to be like the roster student name, id and grade if you hit the coresponding number.

But I am having trouble figuring this out.

I included the text file.

Any help would be greatley appreciated.
Attached Files
File Type: txt gradebook.txt (1.6 KB, 147 views)
  #2  
Old 02-Apr-2005, 13:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by x3ro
I Am new to C++. This is supposed to create a grade book using structs that takes in an id number a student name and a student grade and have to use an arrary to include multiple inputs. I also need to create functions to average and sum the grades of all the students.

So the output is supposed to be like the roster student name, id and grade if you hit the coresponding number.

But I am having trouble figuring this out.

I included the text file.

Any help would be greatley appreciated.

Sometimes people can help, sometimes not. I suggest that a way to increase the probability that someone can (and will) help is to do this:

1. Copy/paste the code in your post. Some of us won't download attachments. Be sure to read this CodeTags Reference to see how to get properly formatted code.

2. Compile the code. If there are any error or warning messages that you don't understand, ask. Copy/paste the exact error message into your post so that we can see what you see. (Tell us what operating system and what compiler you are using. Sometimes it makes a difference.)

3. If there is some task that you have defined (or has been defined for you) and you understand the task but don't understand how to incorporate various processes into your C or C++ code, tell us what you know and what you don't know. Be specific.

4. In general: Ask specific questions. When you ask for help, you should tell us what you are trying to do (What is the program input and expected output? What happened? What did you expect to happen? What do you understand or not understand about what happened?)

Regards,

Dave
  #3  
Old 02-Apr-2005, 13:54
x3ro x3ro is offline
New Member
 
Join Date: Apr 2005
Posts: 10
x3ro is on a distinguished road
Ok

Here is the code

CPP / C++ / C Code:

#include <iostream>

#include <iomanip>

using std:: cout;

using std::cin;

using std:: endl;

struct book
{
	int id;
	int name;
	float grade;
};

void populate_grade(book *[], int);
void show_grade(book *[], int);
void print_roster(book *[], int);

int menu();

float average, class_total;
int answer, student, quit, choice;


int main()
{
	
	

	

	//cout<<"How big is the array of grades? ";
	//cin<<sizeofa;

	int size=2;
	book BookArray[2];
	choice = menu();
	while(choice != 4)
	{
		choice = menu();
		switch(choice)

		{

		case 1:
			populate_grade(BookArray,size);
			break;

		//case 2: show_grade;
			//cout<< "class total and class average";
			//cin>>class_total;
			//cin>> average;
			break;

		case 3: 
			
			print_roster(BookArray,size);
			break;

		case 4:

			cout <<"quit";
			break;

		}

	}

	 



}

int menu()
{
	int choice;
	cout<< "1. Populate Grade Book"<<endl;
	cout<< "2. Show grade Statistics"<<endl;
	cout<< "3. Print student Roster"<<endl;
	cout<< "4. quit"<<endl;
	cin>> choice;
	return choice;
}

void populate_grade(book *x,int size)
{
	
	for(int i = 0; i<size; i++)
	{
		cout<<"Enter a name";
			cin>>x[size].name;
		cout<<"Enter a student id";
			cin>>x[size].id;
		cout<<"Enter a grade";
			cin>>x[size].grade;
	}

}

void show_grade()
{
	cout<<"Enter a grade";
	
}

void print_roster(book *x, int size)
{
	for(int i = 0; i<size; i++)
	{	
		cout<<"Name: "<<x[size].name<< "/tID: "<<x[size].id<<"/tGrade: "<<x[size].grade<<endl;
	}
} 



Here are the error messages

error C2664: 'populate_grade' : cannot convert parameter 1 from 'book [2]' to 'book *[]'

error C2664: 'print_roster' : cannot convert parameter 1 from 'book [2]' to 'book *[]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

I had it working for a second earlier but, it wasnt going into an array.
  #4  
Old 02-Apr-2005, 14:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by x3ro
Ok

Here is the code

Here are the error messages

error C2664: 'populate_grade' : cannot convert parameter 1 from 'book [2]' to 'book *[]'

error C2664: 'print_roster' : cannot convert parameter 1 from 'book [2]' to 'book *[]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

I had it working for a second earlier but, it wasnt going into an array.


Thanks for using code tags!!!!!

Here is your function prototype:
CPP / C++ / C Code:
void populate_grade(book *[], int);

That says the first argument is an array of pointers to the struct.

Here is where you call the function:

CPP / C++ / C Code:
populate_grade(BookArray,size);

Where BookArray is the name of an array of structs, not an array of pointers to struct. When you use the name of an array of "something" by itself it is treated as a pointer to that "something".

That's what the compiler is complaining about.

Now, here is your function definition:

CPP / C++ / C Code:
void populate_grade(book *x,int size)

And inside the function you treat the argument as a pointer to struct, which is consistent with the way you called it. So ... change the prototype to match the function definition.

Regards,

Dave
  #5  
Old 02-Apr-2005, 21:51
x3ro x3ro is offline
New Member
 
Join Date: Apr 2005
Posts: 10
x3ro is on a distinguished road
Thanks for your help I can now run it, but Im still having trouble ill explain were below:

CPP / C++ / C Code:
#include <iostream>

#include <iomanip>

//..include

using std:: cout;

using std::cin;

using std:: endl;

struct book
{
	int id;
	int name;
	float grade;
};

void populate_grade(book *x, int size);
void show_grade(book *x, int size);
void print_roster(book *x, int size);

int menu();

float average, class_total;
int answer, student, quit, choice;


int main()
{
	
	

	

	//cout<<"How big is the array of grades? ";
	//cin<<sizeofa;

	int size=2;
	book BookArray[2];
	choice = menu();
	while(choice != 4)
	{
		choice = menu();
		switch(choice)

		{

		case 1:
			populate_grade(BookArray,size);
			break;

		//case 2: show_grade;
			//cout<< "class total and class average";
			//cin>>class_total;
			//cin>> average;
			break;

		case 3: 
			
			print_roster(BookArray,size);
			break;

		case 4:

			cout <<"quit";
			break;

		}

	}

	 



}

int menu()
{
	int choice;
	cout<< "1. Populate Grade Book"<<endl;
	cout<< "2. Show grade Statistics"<<endl;
	cout<< "3. Print student Roster"<<endl;
	cout<< "4. quit"<<endl;
	cin>> choice;
	cout<<endl<<endl;;
	return choice;
}

void populate_grade(book *x,int size)
{
	
	for(int i = 0; i<size; i++)
	{
		cout<<"Enter a name";
			cin>>x[size].name;
		cout<<"Enter a student id";
			cin>>x[size].id;
		cout<<"Enter a grade";
			cin>>x[size].grade;
	}
	for(int i = 0; i<size; i++)
	{	
		cout<<"Name: "<<x[size].name<< "\tID: "<<x[size].id<<"\tGrade: "<<x[size].grade<<endl;
	}

}

void show_grade()
{
	cout<<"Enter a grade";
	
}

void print_roster(book *x, int size)
{
	for(int i = 0; i<size; i++)
	{	
		cout<<"Name: "<<x[size].name<< "\tID: "<<x[size].id<<"\tGrade: "<<x[size].grade<<endl;
	}
} 


1. Populate Grade Book
2. Show grade Statistics
3. Print student Roster
4. quit
1


Enter a name2
Enter a student id3
Enter a grade4
Enter a name5
Enter a student id6
Enter a grade7

Name: 5 ID: 6 Grade: 7
Name: 5 ID: 6 Grade: 7 ///Here it Prints out the same thing twice? Dont
/// know why

1. Populate Grade Book
2. Show grade Statistics
3. Print student Roster
4. quit
3 ///After I Hit 3 the Program brings up that is has
///encounterd a problem and needs to end.


Press any key to continue
  #6  
Old 03-Apr-2005, 03:33
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Be careful. You loop trough your array from 0 to size, using i. But when you fill your array in populate_grade and when you try to acquire the data in print_roster you use size for your array's index. You should use i.

CPP / C++ / C Code:
for(int i = 0; i<size; i++)
  {  
    cout<<"Name: "<<x[i].name<< "\tID: "<<x[i].id<<"\tGrade: "<<x[i].grade<<endl;
  }

This will print the elements at the indexes 0 and 1.
By x[size], you try to use the element at index 2. That is not your memory! You declared BookArray like this:

CPP / C++ / C Code:
 book BookArray[2];

I hope this solves your problem (I didn't look through your entire code).

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #7  
Old 03-Apr-2005, 10:22
x3ro x3ro is offline
New Member
 
Join Date: Apr 2005
Posts: 10
x3ro is on a distinguished road
So far so good.

But now I have another problem.

I am not able to cin the size of the array.

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <iomanip>

//..include
using namespace std;
using std::cout;

using std::cin;

using std::endl;

struct book
{
	int id;
	string name;
	float grade;
};

void populate_grade(book *x, int size);
void show_grade(book *x, int size);
void print_roster(book *x, int size);

int menu();

float average, class_total;
int answer, student, quit, choice;



int main()
{
	
	const int size=0;

	cout<<"Please enter the size of the array: ";
	cin>>size;
	
	book BookArray[size];

I get these errors

error C2593: 'operator >>' is ambiguous
error C2466: cannot allocate an array of constant size 0
error C2133: 'BookArray' : unknown size

I figured out that size had to be decleared as a constant. But now the cin statement for size will not work says >> is ambiguous.
  #8  
Old 03-Apr-2005, 11:07
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by x3ro
So far so good.

But now I have another problem.

I am not able to cin the size of the array.

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <iomanip>
// (...) removed code

int main()
{
	
	const int size=0;

	cout<<"Please enter the size of the array: ";
	cin>>size;
	
	book BookArray[size];

I get these errors

error C2593: 'operator >>' is ambiguous
error C2466: cannot allocate an array of constant size 0
error C2133: 'BookArray' : unknown size

I figured out that size had to be decleared as a constant. But now the cin statement for size will not work says >> is ambiguous.


If you want to have size be a const you need to go through the whole process at the same time.

CPP / C++ / C Code:
  int rawsize;

  cout<<"Please enter the size of the array: ";
  cin >> rawsize;
  const int size = rawsize;  // now you have your const with entered val

One other thing, you create your book object but your function needs a pointer to a book. Maybe you already caught this.

Code:
const_prob.cpp: In function `int main()': const_prob.cpp:44: error: cannot convert `book' to `book*' for argument `1' to `void populate_grade(book*, int)'

CPP / C++ / C Code:
book* MyObj[size];

Or something of that nature.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #9  
Old 03-Apr-2005, 12:24
x3ro x3ro is offline
New Member
 
Join Date: Apr 2005
Posts: 10
x3ro is on a distinguished road
CPP / C++ / C Code:
int rawsize;
	

	cout<<"Please enter the size of the array: ";
	cin>>rawsize;
	const int size = rawsize;
	book BookArray[size];

now im getting

error C2133: 'BookArray' : unknown size
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0


I seem to have no luck today.
  #10  
Old 03-Apr-2005, 12:49
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by x3ro
now im getting

error C2133: 'BookArray' : unknown size
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0

I seem to have no luck today.

That's odd. I am using g++ (cygwin) to compile. Here is what I had to test with.

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <iomanip>

//..include
using namespace std;
using std::cout;

using std::cin;

using std::endl;

struct book
{
  int id;
  string name;
  float grade;
};

//void populate_grade(book *x, const int size);
// for testing
void populate_grade(book* x, int size){
   cout << "made it to here with size = " << size << endl;
}

void show_grade(book *x, int size);
void print_roster(book *x, int size);

int menu();

float average, class_total;
int answer, student, quit, choice;

int main()
{
  int rawsize = 0;

  cout<<"Please enter the size of the array: ";
  cin >> rawsize;
  const int size = rawsize;
  
  book* BookArray[size];
  populate_grade(BookArray[size],size);
  
  cout << "size = " << size << endl;
  return 0;
}

This gives me the following output:

Code:
----run 1 begin ------ Please enter the size of the array: 0 made it to here with size = 0 size = 0 ----run 2 begin ------ Please enter the size of the array: 2 made it to here with size = 2 size = 2

Granted, I am not doing anything useful with it, I just was illustrating the const trouble you were having. Run this version I posted and see if you get the same error.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 20:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.