ok so far this is what ive got for my code im trying to figure out how to start my for loop that will receive the array and the int that represents the count(2)?

#include<iostream>
using namespace std;
struct Student 
{
       char Name[30]; 
       float GPA;
       int Major; 
}; 
//Function Prototype
Student studentdata(Student&);
//Function Prototype
Student changeData(Student&);
//Function Prototype
Student GetStudents(Student&);
int main()
{
 struct Student s1;
 struct Student s2;
 struct Student s3;
 
 s2 = studentdata(s1);
 s2 = changeData(s2);
 s2 = GetStudent(s3);
    cout << "GPA: " << s2.GPA <<endl;
 cout << "Major: " << s2.Major << endl;
 cout << "Students Name: " << s2.Name << endl;
      
     return 0;
}
Student studentdata(Student &s1)
{
 cout << "please enter your name: ";
 cin >> s1.Name;
 cout << "please enter your GPA: ";
 cin >> s1.GPA;
 cout << "please enter your major: ";
 cin >> s1.Major;
 return s1;
}
Student changeData(Student &s2)
{
   s2.GPA = 3;
   s2.Major = 1;
   return s2;   
}
Student GetStudents(Student &s3)
{
        for (

Recommended Answers

All 36 Replies

what is GetStudents() supposed to do ?

>>will receive the array
an array of what? It certainly is not an array of struct Student because the function does not take an array but a single object.

i need to figure out to Create a function, GetStudents, which will receive the array and an int representing the count(2). In the function loop thru the data and get all three fields from the user using cin, cin.getline and cout statements. Organize like this: for (...........) { cout and cin.getline for name cout and cin for GPA cout and cin for Major cin.ignore(1); } The problem is that a cin for a numeric will leave the ENTER key in the keyboard buffer and that is OK with cin and other numbers but not with strings, thus we must remove it on our own.
i add this to my code for the function but still stumped any suggestions?

Student GetStudents(Student &s3)
{
        for (
        {
            cout << "Please enter your name: ";
            cin.getline Student[index].Name);
            cout << "Please enter your GPA: ";
            cin.getline Student[index].GPA;
            cout << "Please enter your Major: ";
            cin.ignore Student[index].Major;
        }

}

Narue posted a good explaination of functions just yesterday. You may want to review it and see how you can apply it to yuor program. Student GetStudents(Student &s3) . The parameter is not an array of Student structs, but only one single instance.

int main()
{
    const int MAXSTUDENTS = 10;
    struct StudentArray[MAXSTUDENTS];
    GetStudents(StudentArray,MAXSTUDENTS);
..
<snip>
}

GetStudents(Student studentArray[] , int numberStudents)
{



}

Above is an example of how to create and pass an array of student structs, and how the GetStudents() function receives the array.

and the count(2) in the for loop?

there is no such thing as count(2) count is an integer that acts as a loop counter. What was probably meant was for the loop to count from 0 up to (but not including) 2.

int count;
for( count = 0; count < 2; ++count)
{
   // put your code here   

}

>>The problem is that a cin for a numeric will leave the ENTER key in the keyboard buffer and that is OK with cin and other numbers but not with strings, thus we must remove it on our own.

This is correct. However, the code you posted has several errors that that should be addressed first.

for (
        {
            cout << "Please enter your name: ";
            cin.getline Student[index].Name);
            cout << "Please enter your GPA: ";
            cin.getline Student[index].GPA;
            cout << "Please enter your Major: ";
            cin.ignore Student[index].Major;
        }

1) To be kind I'll assume you left out the parameters of the for loop on purpose. If not, you either need to complete the for() statement by adding the appropriate parameters, or remove it and the curly brackets and call the function repeatedly from another location if you want to enter more than one students information.

2) You also need to use an argument list with the getline() and and ignore() functions.

getline() takes three arguments, but there are two different versions of getline() depending on which type of string you are using. If you are using a Cstyle string as implied by your post, then the first argument is the name of the string, the second is the maximum number of elements to be entered into the string, and the third is which char acts as a terminating char if EOF isn't found or the maximum number of char isn't entered before the terminating char is found. The third parameter can be any legal char but defaults to the newline char. So it would be something like:

cin.getline(myString, x); //if you use the default newline char or
cin.getline(myString, x, '*'); //if you want to terminate input after x number of char or with finding an asterix or if finding EOF.

ignore() takes two char, the first being how many char to ignore, and the latter being what char to terminate ignoring on. You don't have to indicate any argument if you only want to ignore one character as the first argument defaults to 1 and the second to EOF.

3) Once though errors have been corrected, then you will want to move the call to ignore() to before the call to getline(), not after, to deal with any lingering char in the stream after a call to >>.

i tried that and im getting an error overloaded function with no contextual type information?

Post your entire code, only then can we pinpoint errors.

ok learner so if i use for( count = 0; count < 2; ++count) what then?

ok i switch the int count to put it in the function and now im getting different error message that says: expected `;' before "Student"

#include<iostream>
using namespace std;
struct Student 
{
       char Name[30]; 
       float GPA;
       int Major; 
}; 
//Function Prototype
Student studentdata(Student&);
//Function Prototype
Student changeData(Student&);
//Function Prototype
Student GetStudent(Student&);
int main()
{
 struct Student s1;
 struct Student s2;
 struct Student s3;
 int index;
 
 
 s2 = studentdata(s1);
 s2 = changeData(s2);
 s2 = GetStudent(s3);
    cout << "GPA: " << s2.GPA <<endl;
 cout << "Major: " << s2.Major << endl;
 cout << "Students Name: " << s2.Name << endl;
 
      system("PAUSE");
    return EXIT_SUCCESS;
}
Student studentdata(Student &s1)
{
 cout << "please enter your name: ";
 cin >> s1.Name;
 cout << "please enter your GPA: ";
 cin >> s1.GPA;
 cout << "please enter your Major: ";
 cin >> s1.Major;
 return s1;
}
Student changeData(Student &s2)
{
   s2.GPA = 3;
   s2.Major = 1;
   return s2;   
}
Student GetStudent(Student &s3)
{
        int count;
        for( count = 0; count < 2; ++count)
        {
            cout << "Please enter your name: ";
            cin.getline Student[index]s3.Name);
            cout << "Please enter your GPA: ";
            cin.getline Student[index]s3.GPA;
            cout << "Please enter your Major: ";
            cin.ignore Student[index]s3.Major;
        }
}

>>Student GetStudent(Student &s3)
That line is wrong. see my previous post for the correct line.

>> cin.getline Student[index]s3.Name);
this line is also incorrect. use the count loop counter instead of unknown variable index

you mean?
GetStudents(Student studentArray[] , int numberStudents)

and as far as count like this

{
        int count;
        for( count = 0; count < 2; ++count)
        {
            cout << "Please enter your name: ";
            cin.getline Student[count]s3.Name);
            cout << "Please enter your GPA: ";
            cin.getline Student[count]s3.GPA;
            cout << "Please enter your Major: ";
            cin.ignore Student[count]s3.Major;
        }
}

Here's a review that may be helpful. Since I don't have a compiler to debug the code there may well be errors. If you don't understand what's going on, ask.

This is a program to deal with a struct in C++ that acts like a struct in C.
It deals with a singe Student object by getting user input, overriding user input, and outputting data.

#include<iostream>
using namespace std;
struct Student 
{
       char Name[30]; 
       float GPA;
       int Major; 
}; 

void studentdata(Student&);
void changeData(Student&);
void GetStudent(Student&);

int main()
{
   Student s1;
 
   studentdata(s1);
   changeData(s1);
   GetStudent(s1);

   cout << "GPA: " << s1.GPA <<endl;
   cout << "Major: " << s1.Major << endl;
   cout << "Students Name: " << s1.Name << endl;
 
   cin.get();
    return EXIT_SUCCESS;
}

//obtain user input 
void studentdata(Student &s1)
{
   cout << "please enter your name: ";
   cin >> s1.Name;
   cout << "please enter your GPA: ";
   cin >> s1.GPA;
   cout << "please enter your Major: ";
   cin >> s1.Major;
}

//override user input
void changeData(Student &s2)
{
   s2.GPA = 3;
   s2.Major = 1;  
}

//output Student data
void GetStudent(Student &s3)
{            
    cout << s3.Name << ' ' << s3.GPA << ' ' << s3.Major << endl;
}

This is a program that does the same as above except it uses structs as C++ struct. Again, it does the same stuff as above.

#include<iostream>
using namespace std;

struct Student 
{
       char Name[30]; 
       float GPA;
       int Major; 
      
       void studentdata();
       void changeData();
       void GetStudent();
}; 

//obtain user input 
void Student::studentdata()
{
   cout << "please enter your name: ";
   cin >> Name;
   cout << "please enter your GPA: ";
   cin >> GPA;
   cout << "please enter your Major: ";
   cin >> Major;
}

//override user input
void Student::changeData()
{
   GPA = 3;
   Major = 1;   
}

//output Student data
void Student::GetStudent()
{            
    cout << Name << ' ' << GPA << ' ' << Major;
}

int main()
{
   Student s1;
 
   s1.studentdata();
   s1.changeData();
   s1.GetStudent();

   cout << "GPA: " << s1.GPA <<endl;
   cout << "Major: " << s1.Major << endl;
   cout << "Students Name: " << s1.Name << endl;
 
   cin.get();
    return EXIT_SUCCESS;
}

Now using an array of Students

#include<iostream>
using namespace std;

struct Student 
{
       char Name[30]; 
       float GPA;
       int Major; 
      
       void studentdata();
       void changeData();
       void GetStudent();
}; 

//obtain user input 
void Student::studentdata()
{
   cout << "please enter your name: ";
   cin >> Name;
   cout << "please enter your GPA: ";
   cin >> GPA;
   cout << "please enter your Major: ";
   cin >> Major;
}

//override user input
void Student::changeData()
{
   GPA = 3;
   Major = 1;   
}

//output Student data
void Student::GetStudent()
{            
    cout << Name << ' ' << GPA << ' ' << Major;
}

int main()
{
   int i;

   //declare the array of students
   Student studentArray[3];
 
   //fill the array of Students
  for(i = 0; i < 3; ++i)
  {
     studentArray[i].studentdata();
   }

    //change the array of Students
    for(i = 0; i < 3; ++i)
    {
       studentArray[i].changeData();
     }

     //display the array of Students
     for(i = 0; i < 3; ++i)
     {
         s1.GetStudent();
     }

   //display the information for a given Student in the array
   cout << "GPA: " << studentArray[1].GPA <<endl;
   cout << "Major: " << studentArray[1].Major << endl;
   cout << "Students Name: " << studentArray[1].Name << endl;
 
   cin.get();
    return EXIT_SUCCESS;
}

And now using functions to deal with the arrays of Students

#include<iostream>
using namespace std;

struct Student 
{
       char Name[30]; 
       float GPA;
       int Major; 
      
       void studentdata();
       void changeData();
       void GetStudent();
}; 

//obtain user input 
void Student::studentdata()
{
   cout << "please enter your name: ";
   cin >> Name;
   cout << "please enter your GPA: ";
   cin >> GPA;
   cout << "please enter your Major: ";
   cin >> Major;
}

//override user input
void Student::changeData()
{
   GPA = 3;
   Major = 1;   
}

//output Student data
void Student::GetStudent()
{            
    cout << Name << ' ' << GPA << ' ' << Major;
}

void fillArray(Student [], int);
void changeArray(Student [], int);
void displayArray(Student [], int);

int main()
{
   int i;

   //declare the array of students
   Student studentArray[3];
 
   //fill the array of Students
   fillArray(studentArray, 3);

   //change the array of Students
   changeArray(studentArray, 3);

   //display the array of Students
   displayArray(studentArray, 3);
  
   //display the information for a given Student in the array
   cout << "GPA: " << studentArray[1].GPA <<endl;
   cout << "Major: " << studentArray[1].Major << endl;
   cout << "Students Name: " << studentArray[1].Name << endl;
 
   cin.get();
    return EXIT_SUCCESS;
}

void fillArray(Student studentArray[], int size)
{
   int i;
   for(i = 0; i < size; ++i)
  {
     studentArray[i].studentdata();
   }
}
 
void changeArray(Student studentArray[], int size)
{
    for(int i = 0; i < size; ++i)
    {
       studentArray[i].changeData();
     }
}

void displayArray(Student studentArray [], int size)
{
     for(int i = 0; i < size; ++i)
     {
         s1.GetStudent();
     }
}

ok i tried the last code s1.GetStudent() flagged a error stating that it wasnt declared plus in my GetStudent i need to put cin.getline/cin.ignore and cout that will output that displays ie):please enter your name...ect. you know what i mean?

s1.GetStudent() flagged a error stating that it wasnt declared

void displayArray(Student studentArray [], int size)
{
for(int i = 0; i < size; ++i)
{
s1.GetStudent();
}
}

Replace s1 with studentArray to solve the error.

plus in my GetStudent i need to put cin.getline/cin.ignore and cout that will output that display

your studentdata(Student) function does that allready or am I misunderstanding you?

Regards Niek

i replaced s1 with studentarray and now it say my GetStudent isnt declared yet. Second, regarding your other question i need to put the cin.getline and cin.ignores into a GetStudent function any help with this?

//clear preceding newline char. This may or may not be adequate, but it's a start
cin.ignore();
cout << "please enter your name: " << endl;
cin.getline(Name, 29);

>>i replaced s1 with studentarray and now it say my GetStudent isnt declared yet.

If you copied and pasted the code I wrote, then you probably should replace s1 with studentArray in displayArray() since studentArray is an array of type Student and studentArray is an individual Student and GetStudent() is declared for type Student and not for type Student array.

ok is it suppose to keep asking for the name gpa and major like 3 times or so should it do that?

now this is my code

#include<iostream>
using namespace std;
struct Student 
{
       char Name[30]; 
       float GPA;
       int Major; 
}; 
//Function Prototype
Student studentdata(Student&);
//Function Prototype
Student changeData(Student&);
//Function Prototype
Student GetStudent(Student&);
int main()
{
 struct Student s1;
 struct Student s2;
 struct Student s3;
 
 s2 = studentdata(s1);
 s2 = changeData(s2);
 s2 = GetStudent(s3);
    cout << "GPA: " << s2.GPA <<endl;
 cout << "Major: " << s2.Major << endl;
 cout << "Students Name: " << s2.Name << endl;
      
     return 0;
}
Student studentdata(Student &s1)
{
 cout << "please enter your name: ";
 cin >> s1.Name;
 cout << "please enter your GPA: ";
 cin >> s1.GPA;
 cout << "please enter your major: ";
 cin >> s1.Major;
 return s1;
}
Student changeData(Student &s2)
{
   s2.GPA = 3;
   s2.Major = 1;
   return s2;   
}
Student GetStudent(Student &s3)
{
        int count;
        for( count = 0; count < 2; ++count)
        {
            cout << "please enter your name: " << endl;
            cin.getline(Name, 30);
            cout << "Please enter your GPA: ";
            cin.getline Student[index].GPA;
            cout << "Please enter your Major: ";
            cin.ignore Student[index].Major;
        }
}

my problem is now cin.ignore (Name,30);
now the error says that Name is undeclaired?

"Name" is undefined becauase the name which you want to access is a member of the structure student whose reference you passed to the function. So in order to change it you must access the "Name" variable via the struct instance or variable.

cout << "please enter your name: " << endl;
cin.getline(Name, 30);

// correct usage...atleast in your case
cin.ignore( 30, '\n' ) ;

cout << "Please enter your GPA: ";

// wrong way of accepting a floating point data, GPA is not a string 
// but a float so just use normal cin
cin.getline Student[index].GPA;

cout << "Please enter your Major: ";

//syntax error, see above for current use
cin.ignore Student[index].Major;

Also your logic seems to be incorrect and you are messing up between accepting data for one and multiple student variables. So you either:

1. Create an array of student variables and call get_student( ) method only once.
2.Create three seperate variables and call get_student( ) method three times.

ok well i think this is what you meant

Student GetStudent(Student &s3)
{
        int count;
        char Name[30];
        float GPA;
        for( count = 0; count < 2; ++count)
        {
            cout << "please enter your name: " << endl;
            cin.getline(Name, 30);
            cin.ignore( 30, '\n' ) ;
            cout << "Please enter your GPA: ";
            cin.getline(GPA);
            cout << "Please enter your Major: ";
            cin.ignore Student[index].Major;
        }

second i need to make sure that the GPA even though its a float still needs to be a cin.ignore and major needs to be cin.ignore.
also i need to figure out how to put a throw in each function which gets user input and throw a string "Bad Major" if a Major of 0 is entered.
i also have add a error trapping with a try and a catch.

ok well i think this is what you meant

No..I meant this..

Student GetStudent(Student &s3)
{
     for( count = 0; count < 2; ++count)
     {
          cout << "please enter your name: " << endl;
          cin.getline(s3.Name, 30);
          cin.ignore( 30, '\n' ) ;
          cout << "Please enter your GPA: ";
           //cin.getline(GPA);
          cin >> s3.GPA ;
          cout << "Please enter your Major: ";
          cin >> s3.Major ;
      }
}

cin.ignore() is basically used for cleaning the junk from the stream, which is basically left there when string input is accepted from the user. Since accepting GPA and Major involves accepting non string values, you don't need a ignore since there is nothing to ignore.

For exceptions you can look here.

cool but now getting a overloaded function with no contextual type information error when i try to compile.

ok for the tryblock would this work?

try 
{ 
   cin >> Student[index].Major; 
   if (Student[index].Major == 0) 
   { 
      throw "Bad Major"; 
   } 
} 
catch (const char *err) 
{ 
   // Do whatever you need to 
}

The call to ignore() should come before the call to getline(), not after, if you are going to use it at all. The for loop in GetStudent() in you most recent post just overwrites the information in s3 each time through the loop. The final content of s3 will be the input into s3 entered during the last iteration of the loop, and the other 2 sets of information will be lost. It's not a syntax error, but it is a logic error.

I hate to say it, but I think the ins and outs of using throw/catch statements are somewhat beyond your level of knowledge at this time. I think you will be better served by trying to work out the stuff you've been working on so far before venturing into totally new waters.

The call to ignore() should come before the call to getline(), not after, if you are going to use it at all.

It should come after the first time you are accepting input from the user and expect the stream to be corrupted. What reason does it serve to place the ignore before getline when there is no attempt to read from the user and hence no chance of stream coruption.

Read this and this. In each case, ignore is used after accepting user input.

>>It should come after the first time you are accepting input from the user and expect the stream to be corrupted.

I won't argue with either of the examples presented. However, how can you be absolutely certain that the stream is empty or that >> hasn't been called before getline(), particularly in a modular program where the exact sequence of calls may not be predictable? I won't argue that ignore() can be used in different settings, and different people have different styles, but I prefer to use it before getline() to be sure the stream is emptied, and not take my chances that I remembered to use it after the last >> call before the next call to getline(). I know that if I'm calling getline() I'm probably better off making sure I've cleared the buffer, particularly if I'm using whitespace as a delimiter.

ok i reversed them and i am still getting overloaded function with no contextual type information error when i try to compile thats under the for loop

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.