OK I need the firstname astname and grade to all be put in an array i cant get it to work. Where do I put the array?

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std; 
const int MAX = 3;
struct Person
{
 string Firstname, Lastname;
 int grade[2];
 Person()
 {
  Firstname="No name assigned";
  Lastname="No name assigned";
  grade = -1;
 }
 Person (string l, string f, int g)
 {
 Firstname = f;
 Lastname =l;
 grade = g;
 }
};
int main()
{
 int x = 0; 
 while (x++ < 20)
  {
   int grade;
   string strFirstname, strLastname;
   cout <<"****************************************"<< endl;
   cout <<"Enter person's first name: ";
   getline(cin, strFirstname);
   cout <<"Enter person's last name: ";
   getline(cin, strLastname);
   cout <<"Enter the grade: ";
   cin>>grade;
   cin.ignore();
    while (grade < 0 || grade > 100)
     {
      cout << "Try againand enter a valid grade" <<endl;
      cin >> grade;
     }
   //Person p1(strLastname, strFirstname, grade);
   //cout <<"The person's name is: "
   //<<p1.Lastname<<", "<<p1.Firstname<<endl;
   //cout <<"The grade is: "
   //<<p1.grade<<endl;
   cout <<"****************************************"<< endl;
 }
  
 return 0;
}

>Where do I put the array?
There's only one place: main. But since you're using std::strings you're probably also in a position to use std::vectors as well. They should be preferred over arrays:

// Other includes
#include <vector>

// Person declaration

int main()
{
  std::vector<Person> v;
  bool done = false;

  while ( true ) {
    v.push_back( get_person() );
    // Some kind of test to break the loop
  }
}

Naturally you need to fill in the blanks and write get_person.

ok but where would i plug that in my program bellow main? when i put it in it would not work.

>ok but where would i plug that in my program bellow main?
You don't plug it in, you incorporate it. That means engaging your brain and writing code that works within the current framework of your program or rewriting your program to meet the new requirements.

>when i put it in it would not work.
"It doesn't work" is about the least useful phrase you could possibly utter. If you don't care enough about getting help to properly tell us what doesn't work, how it doesn't work, and what you expected, we don't care enough to help you.

#include <vector>

I have never used before, so i have know idea how to use it i am new to C++ and I am just wondering if I can put an array into me code. If that is what u showed me than I had know idea because I have never seen that before. I tried to input a peace of your coe into mine and i could not figure it out thats why i said it didnt work.

>i am new to C++ and I am just wondering if I can put an array into me code.
The evidence suggests that you can't. Here:

#include <iostream>
#include <string>

using namespace std; 

const int MAX = 2;

struct Person {
  string Firstname, Lastname;
  int grade;

  Person()
  {
    Firstname = "No name assigned";
    Lastname = "No name assigned";
    grade = -1;
  }

  Person ( string l, string f, int g )
  {
    Firstname = f;
    Lastname = l;
    grade = g;
  }
};

int main()
{
  Person a[MAX];

  for ( int i = 0; i < MAX; i++ ) {
    string strFirstname, strLastname;
    int grade;

    cout <<"Enter person's first name: ";
    getline(cin, strFirstname);
    cout <<"Enter person's last name: ";
    getline(cin, strLastname);
    cout <<"Enter the grade: ";
    cin>>grade;
    cin.ignore();

    while ( grade < 0 || grade > 100 ) {
      cout << "Try againand enter a valid grade" <<endl;
      cin >> grade;
    }

    a[i] = Person ( strLastname, strFirstname, grade );
    cout <<"****************************************"<< endl;
  }

  cout<<"The grades are:\n";

  for ( int i = 0; i < MAX; i++ )
    cout<< a[i].Lastname <<", "<< a[i].Firstname <<": "<< a[i].grade <<'\n';
}

Now go read a book.

>i could not figure it out thats why i said it didnt work.
I don't care why you said it didn't work. I don't care if it doesn't work and you want help in making it work. What I care about is that "it doesn't work" is completely unhelpful in troubleshooting your problem. It's a waste of time, so when it doesn't work, you should be very specific about how, why, and what you expected instead of saying "it doesn't work".

one more ?. Say I wanted it to print out the grades from highest to lowest is that an easy thing to change. would i just change it it the for loop?

or say I just want to print out the highest grade could i make it do that. and if there was say to 100 would it print them both out?

It depends on what you would consider easy and if you're allowed (homework problems disallow the easy way) to take the most sensible approach:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std; 

const int MAX = 2;

struct Person {
  string Firstname, Lastname;
  int grade;

  Person()
  {
    Firstname = "No name assigned";
    Lastname = "No name assigned";
    grade = -1;
  }

  Person ( string l, string f, int g )
  {
    Firstname = f;
    Lastname = l;
    grade = g;
  }

  bool operator< ( const Person& b )
  {
    return grade < b.grade;
  }
};

int main()
{
  Person a[MAX];

  for ( int i = 0; i < MAX; i++ ) {
    string strFirstname, strLastname;
    int grade;

    cout <<"Enter person's first name: ";
    getline(cin, strFirstname);
    cout <<"Enter person's last name: ";
    getline(cin, strLastname);
    cout <<"Enter the grade: ";
    cin>>grade;
    cin.ignore();

    while ( grade < 0 || grade > 100 ) {
      cout << "Try againand enter a valid grade" <<endl;
      cin >> grade;
    }

    a[i] = Person ( strLastname, strFirstname, grade );
    cout <<"****************************************"<< endl;
  }

  sort ( a, a + MAX );

  cout<<"The grades are:\n";

  for ( int i = 0; i < MAX; i++ )
    cout<< a[i].Lastname <<", "<< a[i].Firstname <<": "<< a[i].grade <<'\n';
}

well the main thing i need to do is print the highest grade along with the name. so i was wondering if i could make it just print that instead of all of the grades.

could i just change the for loop to do that?

would i be able to to that by just changing the for loop up?

Member Avatar for iamthwee

>well the main thing i need to do is print the highest grade along with the name

If all you need to do is print the highest grade you don't even need to sort the array

>well the main thing i need to do is print the highest grade along with the name.
That's easier. Or harder depending on whether you liked the sorting option I posted. ;) When you're entering grades, keep a check for the largest grade. If the new grade is larger, save it. When the loop ends, you'll have the person with the highest grade:

Person *p = 0;

for ( int i = 0; i < MAX; i++ ) {
  string strFirstname, strLastname;
  int grade;

  cout <<"Enter person's first name: ";
  getline(cin, strFirstname);
  cout <<"Enter person's last name: ";
  getline(cin, strLastname);
  cout <<"Enter the grade: ";
  cin>>grade;
  cin.ignore();

  while ( grade < 0 || grade > 100 ) {
    cout << "Try againand enter a valid grade" <<endl;
    cin >> grade;
  }

  a[i] = Person ( strLastname, strFirstname, grade );

  if ( p == 0 || grade > p->grade )
    p = &a[i];

  cout <<"****************************************"<< endl;
}

cout<< p->Lastname <<", "<< p->Firstname <<": "<< p->grade <<'\n';

>would i be able to to that by just changing the for loop up?
You're hell bent on changing that loop, aren't you?

lol yeah i was set on changing that loop, but that because i thought it would be easier. But i see what u did and that solved my problems. Hey Thanks for all the help guys I really appreciate it.

o sorry one last thing. when i enter a name and then enter the a grade like 200 it says enter another grade, like it is suppose to. but then when u enter a grade that works it goes to the next line and it askes for the first and last name on the same line, so u can only put in the last name. if u run the program u will see. just put in a number that is not between 0 and 100.

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std; 
const int MAX = 4;
struct Person {
  string Firstname, Lastname;
  int grade;
  Person()
  {
    Firstname = "No name assigned";
    Lastname = "No name assigned";
    grade = -1;
  }
  Person ( string l, string f, int g )
  {
    Firstname = f;
    Lastname = l;
    grade = g;
  }
};

int main()
{
 Person a[MAX];
 Person *p = 0;
 for ( int i = 0; i < MAX; i++ ) 
  
  {
   string strFirstname, strLastname;
   int grade;
   cout <<"Enter person's first name: ";
   getline(cin, strFirstname);
   cout <<"Enter person's last name: ";
   getline(cin, strLastname);
   cout <<"Enter the grade: ";
   cin>>grade;
   cin.ignore();

 while ( grade < 0 || grade > 100 ) 
  {
   cout << "Try againand enter a valid grade" <<endl;
   cin >> grade;
  }

  a[i] = Person ( strLastname, strFirstname, grade );
  if ( p == 0 || grade > p->grade )
  p = &a[i];
   cout <<"****************************************"<< endl;
  }
   cout<< p->Lastname <<", "<< p->Firstname <<": "<< p->grade <<'\n';
  }

anyone got anything

One of these things, is not like the other:

cout <<"Enter the grade: ";
cin>>grade;
cin.ignore();
cout << "Try againand enter a valid grade" <<endl;
cin >> grade;
Member Avatar for iamthwee

What happens if you use getline on the grade.assume the grade is handled as a string, then convert the string to an integer using stringstream? Then you could also handle input that is invalid instead of it crashing.

o ok i fixed it, thanks. the last ? i swear. say there is 2 grades that are 100 i need them to print out both of the names or say it was 5 100 out of 20 students. i need it to print out all the names of that highest grade. can i make it store all of the highest grades?

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std; 
const int MAX = 2;
struct Person {
  string Firstname, Lastname;
  int grade;
  Person()
  {
    Firstname = "No name assigned";
    Lastname = "No name assigned";
    grade = -1;
  }
  Person ( string l, string f, int g )
  {
    Firstname = f;
    Lastname = l;
    grade = g;
  }
};

int main()
{
 Person a[MAX];
 Person *p = 0;
 for ( int i = 0; i < MAX; i++ ) 
  
  {
   string strFirstname, strLastname;
   int grade;
   cout <<"Enter person's first name: ";
   getline(cin, strFirstname);
   cout <<"Enter person's last name: ";
   getline(cin, strLastname);
   cout <<"Enter the grade: ";
   cin>>grade;
   cin.ignore();
   while ( grade < 0 || grade > 100 ) 
    {
     cout << "Try againand enter a valid grade" <<endl;
     cin >> grade;
     cin.ignore();
    }

   a[i] = Person ( strLastname, strFirstname, grade );
   if ( p == 0 || grade > p->grade )
   p = &a[i];
   cout <<"****************************************"<< endl;
  }
   cout << "The Highest Grade is:" << endl;
   cout<< p->Lastname <<", "<< p->Firstname <<": "<< p->grade <<'\n';
}

>can i make it store all of the highest grades?
Yes, provided you know the highest grade from the start. This is a case where you'll have an easier time saving everything and then sorting like before. Take the last numbers from the array that are identical and those are your highest grades.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.