954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help with creating and calling a function

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 (
joelw
Light Poster
28 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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;
        }

}
joelw
Light Poster
28 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

and the count(2) in the for loop?

joelw
Light Poster
28 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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   

}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

>>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 >>.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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

joelw
Light Poster
28 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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

joelw
Light Poster
28 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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;
        }
}
joelw
Light Poster
28 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

>>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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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;
        }
}
joelw
Light Poster
28 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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();
     }
}
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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?

joelw
Light Poster
28 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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?

joelw
Light Poster
28 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

//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);

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

>>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[i] in displayArray() since studentArray is an array of type Student and studentArray[i] is an individual Student and GetStudent() is declared for type Student and not for type Student array.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

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

joelw
Light Poster
28 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You