| | |
help with creating and calling a function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 28
Reputation:
Solved Threads: 0
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)?
C++ Syntax (Toggle Plain Text)
#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 (
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.
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Nov 2006
Posts: 28
Reputation:
Solved Threads: 0
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?
i add this to my code for the function but still stumped any suggestions?
C++ Syntax (Toggle Plain Text)
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.
Above is an example of how to create and pass an array of student structs, and how the GetStudents() function receives the array.
Student GetStudents(Student &s3). The parameter is not an array of Student structs, but only one single instance. C++ Syntax (Toggle Plain Text)
int main() { const int MAXSTUDENTS = 10; struct StudentArray[MAXSTUDENTS]; GetStudents(StudentArray,MAXSTUDENTS); .. <snip> } GetStudents(Student studentArray[] , int numberStudents) { }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
C++ Syntax (Toggle Plain Text)
int count; for( count = 0; count < 2; ++count) { // put your code here }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jul 2005
Posts: 1,675
Reputation:
Solved Threads: 261
>>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.
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 >>.
This is correct. However, the code you posted has several errors that that should be addressed first.
C++ Syntax (Toggle Plain Text)
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; }
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 >>.
![]() |
Similar Threads
- Helping with calling a function (C)
- Calling C function from MATLAB (C)
- Still need info on calling a function from bool! (C++)
Other Threads in the C++ Forum
- Previous Thread: Why doesn't g++ match operator>>??
- Next Thread: How to start
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






