help with creating and calling a function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 28
Reputation: joelw is an unknown quantity at this point 
Solved Threads: 0
joelw joelw is offline Offline
Light Poster

help with creating and calling a function

 
0
  #1
Dec 13th, 2006
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)?

  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 (
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with creating and calling a function

 
0
  #2
Dec 13th, 2006
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 28
Reputation: joelw is an unknown quantity at this point 
Solved Threads: 0
joelw joelw is offline Offline
Light Poster

Re: help with creating and calling a function

 
0
  #3
Dec 13th, 2006
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?
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with creating and calling a function

 
0
  #4
Dec 13th, 2006
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.
  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.
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 28
Reputation: joelw is an unknown quantity at this point 
Solved Threads: 0
joelw joelw is offline Offline
Light Poster

Re: help with creating and calling a function

 
0
  #5
Dec 13th, 2006
and the count(2) in the for loop?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with creating and calling a function

 
0
  #6
Dec 13th, 2006
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.
  1. int count;
  2. for( count = 0; count < 2; ++count)
  3. {
  4. // put your code here
  5.  
  6. }
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,675
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: help with creating and calling a function

 
0
  #7
Dec 13th, 2006
>>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. 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 >>.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 28
Reputation: joelw is an unknown quantity at this point 
Solved Threads: 0
joelw joelw is offline Offline
Light Poster

Re: help with creating and calling a function

 
0
  #8
Dec 13th, 2006
i tried that and im getting an error overloaded function with no contextual type information?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: help with creating and calling a function

 
0
  #9
Dec 13th, 2006
Post your entire code, only then can we pinpoint errors.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 28
Reputation: joelw is an unknown quantity at this point 
Solved Threads: 0
joelw joelw is offline Offline
Light Poster

Re: help with creating and calling a function

 
0
  #10
Dec 13th, 2006
ok learner so if i use for( count = 0; count < 2; ++count) what then?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC