4 Replies - 2846 Views - Last Post: 22 November 2006 - 01:51 PM Rate Topic: -----

#1 csstarter9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 18-September 06

Help with Data Structures

Posted 21 November 2006 - 06:06 AM

Data Structures:

- Write a program that reads student information (first name, last name, NID, and test score) and displays all of the students with scores below a requested cutoff score.
- The program must first request the number of students in the class. Validate the entry to accept only integers from 0 to 100.
- The student information must be stored in a struct with the following form:
student[].name.first
student[].name.last
student[].nid
student[].score

- The program must request all the student information on a student-by-student basis.
- The first name must be validated to only accept lowercase and uppercase letters (limited to 30 characters).
- The last name must be validated to only accept lowercase and uppercase letters (limited to 30 characters).
- The NID must be validated to only accept lowercase letters and numbers (limited to 30 characters).
- The score must be validated to only accept integers from 0 to 100.
- The program must then request a cutoff score (validated as an integer from 0 to 100) and display all the information for students with scores below the cutoff.
- The number of students, first name, last name, NID, score, and cutoff score, as well as the student information display must be done through four different functions with prototypes as shown below:
bool GetString(char []);
bool GetNID(char []);
bool GetScore(int *);
void DisplayStudent(StudentData);

Here is what i have:

#include <iostream>
#include <cstdlib>

using namespace std;

struct NameType
{
	char first[30];
	char last[30];
};

struct StudentType
{
	NameType name;
	int age;
	char nid[10];
	float score;
};

void main()
{
	StudentType student[100];
	float AveAge = 0.0;
	float AveSco = 0.0;
	int i,nStudents;

	do
	{
		cout << "Enter the number of students [1-100]: ";
		cin  >> nStudents;
		if ((nStudents < 1) || (nStudents > 100))
		{
			cout << "Incorrect value. Try again... " << endl;
		}
	} while ((nStudents < 1) || (nStudents > 100));

	for (i=0;i<nStudents;i++)
	{
		cout << "Enter information for Student #" << i+1 << endl;
		cout << "  First name: ";
		cin  >> student[i].name.first;
		cout << "  Last name:  ";
		cin  >> student[i].name.last;
		cout << "  Age:		";
		cin  >> student[i].age;
		cout << "  NID:		";
		cin  >> student[i].nid;
		cout << "  Grade:	  ";
		cin  >> student[i].score;
	}

	for (i=0;i<nStudents;i++)
	{
		AveAge = AveAge + student[i].age;
		AveSco = AveSco + student[i].score;
	}
	AveAge = AveAge / nStudents;
	AveSco = AveSco / nStudents;

	cout << "First name, Last name, Age, NID, and Grade" << endl;
	for (i=0;i<nStudents;i++)
	{
		cout << student[i].name.first << " ";
		cout << student[i].name.last  << " ";
		cout << student[i].age		<< " ";
		cout << student[i].nid		<< " ";
		cout << student[i].score	  << endl;
	}

	cout << "The average student age is:   " << AveAge << endl;
	cout << "The average student grade is: " << AveSco << endl;
}

This post has been edited by Dark_Nexus: 21 November 2006 - 11:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Data Structures

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Help with Data Structures

Posted 21 November 2006 - 08:19 AM

Can you describe the problem you're having, along with any error messages that are being generated?
Was This Post Helpful? 0
  • +
  • -

#3 csstarter9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 18-September 06

Re: Help with Data Structures

Posted 21 November 2006 - 11:08 AM

View PostAmadeus, on 21 Nov, 2006 - 08:19 AM, said:

Can you describe the problem you're having, along with any error messages that are being generated?


I need to know how to incorporate these requirements:
- The program must request all the student information on a student-by-student basis.
- The first name must be validated to only accept lowercase and uppercase letters (limited to 30 characters).
- The last name must be validated to only accept lowercase and uppercase letters (limited to 30 characters).
- The NID must be validated to only accept lowercase letters and numbers (limited to 30 characters).
- The score must be validated to only accept integers from 0 to 100.
- The program must then request a cutoff score (validated as an integer from 0 to 100) and display all the information for students with scores below the cutoff.
- The number of students, first name, last name, NID, score, and cutoff score, as well as the student information display must be done through four different functions with prototypes as shown below:
bool GetString(char []);
bool GetNID(char []);
bool GetScore(int *);
void DisplayStudent(StudentData);
Was This Post Helpful? 0
  • +
  • -

#4 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: Help with Data Structures

Posted 21 November 2006 - 12:06 PM

View Postcsstarter9, on 21 Nov, 2006 - 11:08 AM, said:

View PostAmadeus, on 21 Nov, 2006 - 08:19 AM, said:

Can you describe the problem you're having, along with any error messages that are being generated?


I need to know how to incorporate these requirements:
- The program must request all the student information on a student-by-student basis. make a loop. get info and put it into a struct in the loop
- The first name must be validated to only accept lowercase and uppercase letters (limited to 30 characters).if(name!=name.tolower() || name.length()>30)...
- The last name must be validated to only accept lowercase and uppercase letters (limited to 30 characters).
- The NID must be validated to only accept lowercase letters and numbers (limited to 30 characters).
- The score must be validated to only accept integers from 0 to 100.if(score>100 || score<0)...
- The program must then request a cutoff score (validated as an integer from 0 to 100) and display all the information for students with scores below the cutoff.Use a loop to check through your structs and if score<cutoff, display info
- The number of students, first name, last name, NID, score, and cutoff score, as well as the student information display must be done through four different functions with prototypes as shown below:
bool GetString(char []);
bool GetNID(char []);
bool GetScore(int *);
void DisplayStudent(StudentData);


This might be useful. (From cplusplus.com tutorial.)

// array of structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

#define N_MOVIES 3

struct movies_t {
  string title;
  int year;
} films [N_MOVIES];

void printmovie (movies_t movie);

int main ()
{
  string mystr;
  int n;

  for (n=0; n<N_MOVIES; n++)
  {
	cout << "Enter title: ";
	getline (cin,films[n].title);
	cout << "Enter year: ";
	getline (cin,mystr);
	stringstream(mystr) >> films[n].year;
  }

  cout << "\nYou have entered these movies:\n";
  for (n=0; n<N_MOVIES; n++)
	printmovie (films[n]);
  return 0;
}

void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}


Was This Post Helpful? 0
  • +
  • -

#5 csstarter9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 18-September 06

Re: Help with Data Structures

Posted 22 November 2006 - 01:51 PM

View Postcsstarter9, on 21 Nov, 2006 - 11:08 AM, said:

View PostAmadeus, on 21 Nov, 2006 - 08:19 AM, said:

Can you describe the problem you're having, along with any error messages that are being generated?


I need to know how to incorporate these requirements:
- The program must request all the student information on a student-by-student basis.
- The first name must be validated to only accept lowercase and uppercase letters (limited to 30 characters).
- The last name must be validated to only accept lowercase and uppercase letters (limited to 30 characters).
- The NID must be validated to only accept lowercase letters and numbers (limited to 30 characters).
- The score must be validated to only accept integers from 0 to 100.
- The program must then request a cutoff score (validated as an integer from 0 to 100) and display all the information for students with scores below the cutoff.
- The number of students, first name, last name, NID, score, and cutoff score, as well as the student information display must be done through four different functions with prototypes as shown below:
bool GetString(char []);
bool GetNID(char []);
bool GetScore(int *);
void DisplayStudent(StudentData);


This is what i got so far and i keep getting these 2 errors:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

struct fullname
{
std::string first;
std::string last;
};

struct studentdata
{
fullname name;
std::string nid;
int score;
};

bool getstring(std::string& thestring);
bool getnid(std::string& thestring);
bool getscore (int *score);
void displaystudent(studentdata);

int main()
{
studentdata student [100];
int n,i;
float avesco = 0.0;
char space = ' ';
bool valid;
int score;

do
{
cout << "Please enter the number of students (1-100): ";
valid = getscore (&n);
if (!valid)
{
cout << "Invalid, please try again\n";
}
} while (!valid);

for (i=0;i<n;i++)
{
cout << "Student #" << i+1 << endl;

do
{
cout << "Please enter the first name of student #" << i+1 << ": ";
valid = getstring(student[i].name.first);
if (!valid)
{
cout << "Invalid, please try again\n";
}
} while (!valid);

do
{
cout << "Please enter the last name of student #" << i+1 << ": ";
valid = getstring(student[i].name.last);
if (!valid)
{
cout << "Invalid, please try again\n";
}
} while (!valid);

do
{
cout << "Please enter the NID of student #" << i+1 << ": ";
valid = getnid(student[i].nid);
if (!valid)
{
cout << "Invalid, please try again\n";
}
} while (!valid);

do
{
cout << "Please enter the score of student #" << i+1 << ": ";
valid = getscore(student[i].score);
if (!valid)
{
cout << "Invalid, please try again\n";
}
} while (!valid);
}

cout << endl << endl;
do
{
cout << "Please enter the curve score (0-100): ";
valid = getscore(&score);
if (!valid)
{
cout << "Invalid, please try again\n";
}
} while (!valid);

for (i=0;i<n;i++)
{
if (student[i].score < score)
{
displaystudent (student[i]);
}
}
}

bool getstring(std::string& thestring)
{
unsigned int i;
bool validstring = true;

getline(cin, thestring);
i=0;
do
{
if ( !isalpha(thestring[i]) )
validstring = false;
i++;
} while ((validstring) && (i < thestring.size()));

return validstring;
}

bool getnid(std::string& thestring)
{
unsigned int i;
int score;
bool validstring = true;

getline(cin, thestring);
i=0;
do
{
if ( !isdigit(thestring[i]) )
validstring = false;
i++;
} while ((validstring) && (i < thestring.size()));

if (validstring)
{
score = atoi(thestring.c_str());
if (score > 100)
{
validstring = false;
}
}
return validstring;
}

bool getscore(std::string thestring);
{
unsigned int i;
int score;
bool validstring = true;

getline(cin, thestring);
i=0;
do
{
if ( !isdigit(thestring[i]))
validstring = false;
i++;
} while ((validstring) && (i < thestring.size()));

if (validstring)
{
*score = atoi(thestring.c_str());
if (*score > 100)
{
validstring = false;
}
}

return validstring;
}


void displaystudent(studentdata entry)
{
cout << entry.name.first << " ";
cout << entry.name.last << " NID: ";
cout << entry.nid << " Score is: ";
cout << entry.score << endl;
}

These are the 2 errors that i keep getting:
1>.\Assignment 8.cpp(83) : error C2664: 'getscore' : cannot convert parameter 1 from 'int' to 'int *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>.\Assignment 8.cpp(155) : error C2447: '{' : missing function header (old-style formal list?)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1