C++ Structs

program needs to read students' names followed by their scores...

Page 1 of 1

11 Replies - 1105 Views - Last Post: 24 September 2009 - 10:50 PM Rate Topic: -----

#1 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

C++ Structs

Posted 24 September 2009 - 03:59 PM

Program needs to read the students' first name,last name and test scores.Suppose that class has 20 students.Use an array of 20 components of type studentType.

Output : last name followed by comma,space first name , score, revelant grade, and find the highest test score, print names of the students having the highest test score.
Name must be left-justified??? HOW


After I press n or N it doesn't output any names or scores ???
can someone pls let me know what is the problem???

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

 struct studentType
	{
		string studentFName, studentLName;
		int testScore;
		char grade;
	};

int main ( )
{	

	float avg, sum=0;
	char x;
	int i,j=0;
	struct studentType student[20];
	student[0];
	
	x = 'y';
	cout<<"Please type y or Y to enter a new student record x = "<<x<<endl;

	while ( (x == 'y') | ( x == 'Y') )
			   {
				   j=j+1;

				cout<<"Enter student's first name"<<endl;   
					cin>>student[j].studentFName;

				cout<<"Enter student's last name"<<endl; 
					cin>>student[j].studentLName;

				cout<<"Enter five scores."<<endl;
					cin>>student[j].testScore;
						  
					 if (student[j].testScore>=90)
						 student[j].grade='A';
					 else if (student[j].testScore>=80)
						 student[j].grade='B';
					 else if (student[j].testScore>=70)
						 student[j].grade='C';
					 else if (student[j].testScore>=60)
						 student[j].grade='D';
					 else 
						 student[j].grade='F';

					cout<<"Enter 'y' for next student or"
					  <<" Enter 'n' or 'N'to end."<<endl;
					   cin>>x;
 }
	void printstudent (studentType student)
{	
	cout << student.studentLName<<", "<<student.studentFName
<< " " << student.testScore
<< " " << student.grade<<endl;
}
int tempo=0; scanf("...", tempo);

return 0;
}



This post has been edited by need_helpp: 24 September 2009 - 04:52 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Structs

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6087
  • View blog
  • Posts: 23,597
  • Joined: 23-August 08

Re: C++ Structs

Posted 24 September 2009 - 04:04 PM

iomanipulators
Was This Post Helpful? 0
  • +
  • -

#3 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: C++ Structs

Posted 24 September 2009 - 04:04 PM

Suppose you are gonna save several students then your declaration in main should be
studentType students[20];

Now you have created 20 students.

Now you fill it with the info
students[0] 
is gonna be your first student and so till 20 or how many you need ;)

This post has been edited by poncho4all: 24 September 2009 - 04:05 PM

Was This Post Helpful? 0
  • +
  • -

#4 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: C++ Structs

Posted 24 September 2009 - 05:05 PM

Here!
Was This Post Helpful? 0
  • +
  • -

#5 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 689
  • Joined: 16-February 09

Re: C++ Structs

Posted 24 September 2009 - 05:09 PM

Define your printstudent function outside of the main function.

Then invoke the printstudent function after the while body:

void printstudent()
{
	...
}

int main()
{
	while (...)
	{
		...
	}

	printstudent();

}


Was This Post Helpful? 0
  • +
  • -

#6 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Re: C++ Structs

Posted 24 September 2009 - 05:19 PM

e_barroga,
I did what you told me to do, but for some reason it is giving me an error for the invoked printStudent.
Was This Post Helpful? 0
  • +
  • -

#7 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: C++ Structs

Posted 24 September 2009 - 05:23 PM

show your code.

void printStuden();//function declaration

int main()//main function
{
	 printStuden();//invoked print student
	 return 0;
}

void printStuden()//function definition
{
	
}

This post has been edited by carltech: 24 September 2009 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#8 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Re: C++ Structs

Posted 24 September 2009 - 06:58 PM



#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;


 struct studentType
	{
		string studentFName, studentLName;
		int testScore;
		char grade;
	};

void printstudent (studentType student);

int main ( )
{	

	float avg, sum=0;
	char x;
	int i,j=0;
	struct studentType student[20];
	student[0];
	
	x = 'y';

	while ( (x == 'y') | ( x == 'Y') )
			   {
				   j=j+1;

				   cout<<"Enter student's first name:"<<endl;   
					cin>>student[j].studentFName;

					cout<<"Enter student's last name:"<<endl; 
					cin>>student[j].studentLName;

					cout<<"Enter test score:"<<endl;
					cin>>student[j].testScore;
						  
					 if (student[j].testScore>=90)
						 student[j].grade='A';
					 else if (student[j].testScore>=80)
						 student[j].grade='B';
					 else if (student[j].testScore>=70)
						 student[j].grade='C';
					 else if (student[j].testScore>=60)
						 student[j].grade='D';
					 else 
						 student[j].grade='F';

					cout<<"Enter 'y' for next student or"
					  <<" Enter 'n' or 'N'to end."<<endl;
					   cin>>x;
 }


int tempo=0; scanf("...", tempo);
return 0;
}

void printstudent (studentType student)
{	
	cout << student.studentLName<<", "<<student.studentFName
		 << " " << student.testScore
		 << " " << student.grade<<endl;
	
}



Was This Post Helpful? 0
  • +
  • -

#9 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: C++ Structs

Posted 24 September 2009 - 07:21 PM

Try something like this:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

struct studentType
{
	string studentFName, studentLName;
	int testScore;
	char grade;
};

void printstudent(studentType student);

int main()
{
	float avg, sum=0;
	char x = '\0';
	int j = 0;
	studentType student[20];
	int numberOfStudents = 0;

	for(int j = 0; j < 20; j++)
	{		
		cout<<"Enter student's first name:"<<endl;
		cin>>student[j].studentFName;
		cout<<"Enter student's last name:"<<endl;
		cin>>student[j].studentLName;
		
		cout<<"Enter test score:"<<endl;
		cin>>student[j].testScore;
		
		if (student[j].testScore>=90)
			student[j].grade='A';
		else if (student[j].testScore>=80)
			student[j].grade='B';
		else if (student[j].testScore>=70)
			student[j].grade='C';
		else if (student[j].testScore>=60)
			student[j].grade='D';
		else
			student[j].grade='F';

		// increment number of students
		numberOfStudents++;

		cout<<"Enter 'y' for next student or"
			<<" Enter 'n' or 'N'to end."<<endl;
		cin>>x;

		if(x == 'y' || x == 'Y')
		{
			// Do nothing
		}
		else
		{
			break;
		}
	}

	// Print out the students
	for(int i = 0; i < numberOfStudents; i++)
	{
		printstudent(student[i]);
	}


	cin.sync();
	cin.get();
	return 0;
}

void printstudent (studentType student)
{	
	cout << student.studentLName<<", "<<student.studentFName
		 << " " << student.testScore
		 << " " << student.grade<<endl;
   
}


This post has been edited by eker676: 24 September 2009 - 07:21 PM

Was This Post Helpful? 0
  • +
  • -

#10 need_helpp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 72
  • Joined: 08-September 09

Re: C++ Structs

Posted 24 September 2009 - 07:55 PM

One last thing what kind of loop should I write to find the student with the highest score???
Was This Post Helpful? 0
  • +
  • -

#11 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: C++ Structs

Posted 24 September 2009 - 10:37 PM

Well you need to go throw the array maybe with a for loop or while loop which ever you handle better and, compare two of the array position and get a max value, then if next array position is higher then max value equals the new one you found and so on till the array is over :)
Was This Post Helpful? 0
  • +
  • -

#12 trotski  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 151
  • Joined: 04-September 08

Re: C++ Structs

Posted 24 September 2009 - 10:50 PM

View Postneed_helpp, on 25 Sep, 2009 - 06:55 AM, said:

One last thing what kind of loop should I write to find the student with the highest score???


You ask for 5 scores. I do not see any function that finds either their average or their total, and most importantly, their percentage (which is necessary for assigning their grades).

Moreover, why have you used scanf instead of cin?

This post has been edited by trotski: 24 September 2009 - 11:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1