943,993 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7039
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 13th, 2006
-1

help with creating and calling a function

Expand Post »
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)
  1. #include<iostream>
  2. using namespace std;
  3. struct Student
  4. {
  5. char Name[30];
  6. float GPA;
  7. int Major;
  8. };
  9. //Function Prototype
  10. Student studentdata(Student&);
  11. //Function Prototype
  12. Student changeData(Student&);
  13. //Function Prototype
  14. Student GetStudents(Student&);
  15. int main()
  16. {
  17. struct Student s1;
  18. struct Student s2;
  19. struct Student s3;
  20.  
  21. s2 = studentdata(s1);
  22. s2 = changeData(s2);
  23. s2 = GetStudent(s3);
  24. cout << "GPA: " << s2.GPA <<endl;
  25. cout << "Major: " << s2.Major << endl;
  26. cout << "Students Name: " << s2.Name << endl;
  27.  
  28. return 0;
  29. }
  30. Student studentdata(Student &s1)
  31. {
  32. cout << "please enter your name: ";
  33. cin >> s1.Name;
  34. cout << "please enter your GPA: ";
  35. cin >> s1.GPA;
  36. cout << "please enter your major: ";
  37. cin >> s1.Major;
  38. return s1;
  39. }
  40. Student changeData(Student &s2)
  41. {
  42. s2.GPA = 3;
  43. s2.Major = 1;
  44. return s2;
  45. }
  46. Student GetStudents(Student &s3)
  47. {
  48. for (
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
joelw is offline Offline
28 posts
since Nov 2006
Dec 13th, 2006
0

Re: help with creating and calling a function

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 13th, 2006
0

Re: help with creating and calling a function

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?
C++ Syntax (Toggle Plain Text)
  1. Student GetStudents(Student &s3)
  2. {
  3. for (
  4. {
  5. cout << "Please enter your name: ";
  6. cin.getline Student[index].Name);
  7. cout << "Please enter your GPA: ";
  8. cin.getline Student[index].GPA;
  9. cout << "Please enter your Major: ";
  10. cin.ignore Student[index].Major;
  11. }
  12.  
  13. }
Reputation Points: 10
Solved Threads: 0
Light Poster
joelw is offline Offline
28 posts
since Nov 2006
Dec 13th, 2006
0

Re: help with creating and calling a function

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.
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. const int MAXSTUDENTS = 10;
  4. struct StudentArray[MAXSTUDENTS];
  5. GetStudents(StudentArray,MAXSTUDENTS);
  6. ..
  7. <snip>
  8. }
  9.  
  10. GetStudents(Student studentArray[] , int numberStudents)
  11. {
  12.  
  13.  
  14.  
  15. }
Above is an example of how to create and pass an array of student structs, and how the GetStudents() function receives the array.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 13th, 2006
0

Re: help with creating and calling a function

and the count(2) in the for loop?
Reputation Points: 10
Solved Threads: 0
Light Poster
joelw is offline Offline
28 posts
since Nov 2006
Dec 13th, 2006
0

Re: help with creating and calling a function

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)
  1. int count;
  2. for( count = 0; count < 2; ++count)
  3. {
  4. // put your code here
  5.  
  6. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 13th, 2006
0

Re: help with creating and calling a function

>>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.
C++ Syntax (Toggle Plain Text)
  1. for (
  2. {
  3. cout << "Please enter your name: ";
  4. cin.getline Student[index].Name);
  5. cout << "Please enter your GPA: ";
  6. cin.getline Student[index].GPA;
  7. cout << "Please enter your Major: ";
  8. cin.ignore Student[index].Major;
  9. }
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 >>.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Dec 13th, 2006
0

Re: help with creating and calling a function

i tried that and im getting an error overloaded function with no contextual type information?
Reputation Points: 10
Solved Threads: 0
Light Poster
joelw is offline Offline
28 posts
since Nov 2006
Dec 13th, 2006
0

Re: help with creating and calling a function

Post your entire code, only then can we pinpoint errors.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Dec 13th, 2006
0

Re: help with creating and calling a function

ok learner so if i use for( count = 0; count < 2; ++count) what then?
Reputation Points: 10
Solved Threads: 0
Light Poster
joelw is offline Offline
28 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Why doesn't g++ match operator>>??
Next Thread in C++ Forum Timeline: How to start





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC