Question

For C++ Create a student structure that has fields for first name, last name, hometown, state,...

For C++

Create a student structure that has fields for first name, last name, hometown, state, year in college and major. Keep the number of students to a reasonabe level. Place each structure into an array sorted by last name and within last name, by first name. Read the student information from a file and print the sorted array to the screen and then to an output file.

I was using the code below to guide me but it keeps giving out an error saying "strcmp was not declared in this scope".

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//Structure
struct Student
{
string firstName, lastName, homeTown, state, major;
int year;
};

//Main method
int main()
{
//Creating an array of structs
struct Student students[50];
int i, j, stuCnt=0;
struct Student temp, sTemp;

//File for reading
fstream fin("indata9.txt", ios::in);

//File for writing
fstream fout("outdata9.txt", ios::out);

//Reading data from file
while(fin.good())
{
fin >> temp.firstName >> temp.lastName >> temp.homeTown >> temp.state >> temp.year >> temp.major;
//Storing data
students[stuCnt] = temp;
stuCnt++;
}

//Closing file
fin.close();

//Sorting array
for(i=0; i<stuCnt; i++)
{
for(j=i; j<stuCnt; j++)
{
//Comparing students
if(strcmp(students[i].lastName.c_str(), students[j].lastName.c_str()) > 0)
{
//Swapping
sTemp = students[i];
students[i] = students[j];
students[j] = sTemp;
}
//Comparing students
else if(students[i].lastName == students[j].lastName)
{
//Sorting on first name
if(strcmp(students[i].firstName.c_str(), students[j].firstName.c_str()) > 0)
{
//Swapping
sTemp = students[i];
students[i] = students[j];
students[j] = sTemp;
}
}
}
}

cout << "\n\n Students: \n\n";

//Printing array to console
for(i=0; i<stuCnt; i++)
{
//Printing to console and to file
cout << "\n" << students[i].firstName << " " << students[i].lastName << " " << students[i].homeTown << " " << students[i].state << " " << students[i].year << " " << students[i].major;
fout << students[i].firstName << " " << students[i].lastName << " " << students[i].homeTown << " " << students[i].state << " " << students[i].year << " " << students[i].major << "\n";
}

fout.close();

cout << "\n";

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

you need to include :

#include <string.h>

Here is code:

#include <iostream>

#include <fstream>

#include <string>

#include <string.h>

using namespace std;

//Structure

struct Student

{

string firstName, lastName, homeTown, state, major;

int year;

};

//Main method

int main()

{

//Creating an array of structs

struct Student students[50];

int i, j, stuCnt = 0;

struct Student temp, sTemp;

//File for reading

fstream fin("indata9.txt", ios::in);

//File for writing

fstream fout("outdata9.txt", ios::out);

//Reading data from file

while (fin.good())

{

fin >> temp.firstName >> temp.lastName >> temp.homeTown >> temp.state >> temp.year >> temp.major;

//Storing data

students[stuCnt] = temp;

stuCnt++;

}

//Closing file

fin.close();

//Sorting array

for (i = 0; i < stuCnt; i++)

{

for (j = i; j < stuCnt; j++)

{

//Comparing students

if (strcmp(students[i].lastName.c_str(), students[j].lastName.c_str()) > 0)

{

//Swapping

sTemp = students[i];

students[i] = students[j];

students[j] = sTemp;

}

//Comparing students

else if (students[i].lastName == students[j].lastName)

{

//Sorting on first name

if (strcmp(students[i].firstName.c_str(), students[j].firstName.c_str()) > 0)

{

//Swapping

sTemp = students[i];

students[i] = students[j];

students[j] = sTemp;

}

}

}

}

cout << "\n\n Students: \n\n";

//Printing array to console

for (i = 0; i < stuCnt; i++)

{

//Printing to console and to file

cout << "\n"

<< students[i].firstName << " " << students[i].lastName << " " << students[i].homeTown << " " << students[i].state << " " << students[i].year << " " << students[i].major;

fout << students[i].firstName << " " << students[i].lastName << " " << students[i].homeTown << " " << students[i].state << " " << students[i].year << " " << students[i].major << "\n";

}

fout.close();

cout << "\n";

return 0;

}

indata9.txt

Omero Antuk Muncie Indiana 1991 Sebastiano
Mallory Shegog Springfield Missouri 1998 Bar
Simon Cockshot Tacoma Washington 2004 Junina
Emmott Fullom Columbus Ohio 2004 Burtie
Mirna Stygall Ogden Utah 1999 Shepard
Lilla Blakeborough Schenectady New York 1994 Nadean
Sebastiano Brangan Jackson Mississippi 2009 Milissent
Wyatan Gogin Rochester New York 2002 Haskell
Donica Moreby Des Moines Iowa 2007 Donia

Output:

Add a comment
Know the answer?
Add Answer to:
For C++ Create a student structure that has fields for first name, last name, hometown, state,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Rework this project to include a class. As explained in class, your project should have its...

    Rework this project to include a class. As explained in class, your project should have its functionalities moved to a class, and then create each course as an object to a class that inherits all the different functionalities of the class. You createclass function should be used as a constructor that takes in the name of the file containing the student list. (This way different objects are created with different class list files.) Here is the code I need you...

  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • Write the functions needed to complete the following program as described in the comments. Use the...

    Write the functions needed to complete the following program as described in the comments. Use the input file course.txt and change the mark of student number 54812 to 80. /* File: course.cpp A student's mark in a certain course is stored as a structure (struct student as defined below) consisting of first and last name, student id and mark in the course.  The functions read() and write() are defined for the structure student.   Information about a course is stored as a...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • struct student { char name[10]; int rank; }; Using the student structure given above, create an...

    struct student { char name[10]; int rank; }; Using the student structure given above, create an array of size 5 students.. Then write a complete C program to sort the students array based on the students rank. Use the following sorting techniques in your code. Don’t forget to print initial array and final (sorted) array. a. Selection sort b. Insertion sort c. Merge sort / Quick sort.

  • First create the two text file given below. Then complete the main that is given. There...

    First create the two text file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12 Main #include...

  • Expand the payroll program to combine two sorting techniques (Selection and Exchange sorts) for better efficiency...

    Expand the payroll program to combine two sorting techniques (Selection and Exchange sorts) for better efficiency in sorting the employee’s net pay. //I need to use an EXSEL sort in order to combine the selection and Exchange sorts for my program. I currently have a selection sort in there. Please help me with replacing this with the EXSEL sort. //My input files: //My current code with the sel sort. Please help me replace this with an EXSEL sort. #include <fstream>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT