Jump to content

[HELP]C++ - Functions

infernowheels
Go to solution Solved by as96,
void find(studentType p[], int highest, int lowest, int d, int e)void print(studentType p[], int highest, int lowest, int d, int e)      find(p, highest, lowest, d ,e);      print(p, highest, lowest, d ,e);

You are not passing those variables by reference, they are just copies, when you print them you just print un-initialized variables, only arrays get passed automatically via reference for every other type you need to use &

Hey guys,  my instructor requires us to split the main up into separate functions so after doing so, I'm kinda confused about which variables to pass onto where, specially when combined to structs...

 

Can anyone help me out as to which variables are to be passed to each function?

#include<iostream>#define MAX 20using namespace std;struct studentType{       char studentName[50];       char studentLastName[50];       int testScore;       char grade;};int read(){      for(int a = 0; a < MAX; a++)      {      cout << "Enter your FIRST Name: ";      cin >> p[a].studentName;      cout << "Enter your LAST Name: ";      cin >> p[a].studentLastName;       A:      cout << "Enter a score: ";      cin >> p[a].testScore;          if(p[a].testScore < 0 || p[a].testScore > 100)          {           cout << "Invalid input." << endl;           goto A;          }      }}int assign(){      cout << endl;            for(int b = 0; b < MAX; b++)      {      cout << "Student's name: " << p[b].studentLastName << ", " << p[b].studentName << endl;      cout << "His/her score is: " << p[b].testScore << endl;      }}int find(){      for(int c = 0; c < MAX; c++)      {              if(p[c].testScore > highest)              {                          highest = p[c].testScore;                          d = c;                                        }              if(p[c].testScore < lowest)              {                          lowest = p[c].testScore;                          e = c;              }      }}int print(){      cout << "The student with the highest score is: " << p[d].studentLastName << ", " << p[d].studentName << "with a score of: " << highest << endl;      cout << "The student with the lowest score is: " << p[e].studentLastName << ", " << p[e].studentName << "with a score of: " << lowest << endl;}main(){      studentType p[MAX];      int highest, lowest;      int d, e;      read();      assign();      find();      print();  system("pause");return 0;}      

This program basically asks for the names of 20 students and their testScores. Afterwards outputting the student with both the highest and lowest score.

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

Okay, so i need to leave soon, just quote my reply and i'll get a notification and i'll get back to you pretty soon. Else you can just private chat with me later, through Steam if you use it.

 

Right off the bat, you should use std::string to get the name of a person, rather than an array of characters. It is much easier that way.

 

Second,  why are all your functions of type int? If you're not going to return something from the function that you will use in main, use void. 

 

Third, in the function read(), you're getting user input for the object p[] that you declared in main. The function read() does not know what the heck p[] is, because you declared it in main and did not tell function read() what p[] is. So you need to pass it into read() by reference (don't make a copy of the object, make a reference to the memory location). Same with assign(), find(), and print(), none of those functions know what the heck p[] is.

                                                                                                                                            Praise Duarte!

Link to comment
Share on other sites

Link to post
Share on other sites

Okay, so i need to leave soon, just quote my reply and i'll get a notification and i'll get back to you pretty soon. Else you can just private chat with me later, through Steam if you use it.

 

Right off the bat, you should use std::string to get the name of a person, rather than an array of characters. It is much easier that way.

 

Second,  why are all your functions of type int? If you're not going to return something from the function that you will use in main, use void. 

 

Third, in the function read(), you're getting user input for the object p[] that you declared in main. The function read() does not know what the heck p[] is, because you declared it in main and did not tell function read() what p[] is. So you need to pass it into read() by reference (don't make a copy of the object, make a reference to the memory location). Same with assign(), find(), and print(), none of those functions know what the heck p[] is.

Thanks for the correction, however we haven't really went through the lesson of pointers and references so I'm having a hard time grasping your last few statements.

 

Can you show me the parameters which I should put into void read()?

 

I could probably figure out from that how it applies to the other functions as well.

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for the correction, however we haven't really went through the lesson of pointers and references so I'm having a hard time grasping your last few statements.

 

Can you show me the parameters which I should put into void read()?

 

I could probably figure out from that how it applies to the other functions as well.

 

Let's just focus on the read() function for now. Comment out assign(), find(), and print().

 

Now compile it. What errors do you get?

                                                                                                                                            Praise Duarte!

Link to comment
Share on other sites

Link to post
Share on other sites

Let's just focus on the read() function for now. Comment out assign(), find(), and print().

 

Now compile it. What errors do you get?

Just 'p' undeclared

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

K, change the type of read() from int to void, since you're not returning anything from that function. You created the object p[] in main and didn't tell read() what it was, so you need to pass p[] to read(). Do you know how to do that?

                                                                                                                                            Praise Duarte!

Link to comment
Share on other sites

Link to post
Share on other sites

K, change the type of read() from int to void, since you're not returning anything from that function. You created the object p[] in main and didn't tell read() what it was, so you need to pass p[] to read(). Do you know how to do that?

Is this correct?

void read(struct studentType p[MAX])

 

I'm getting a "too few arguments" error though .-.

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

Is this correct?

void read(struct studentType p[MAX])

 

I'm getting a "too few arguments" error though .-.

Almost. The variable type is studentType and the name of the  studentType variable is called p[], so you don't need to include struct into the parameter.

 

You can write it as void read( studentType p[] ) too, it works the same way. Just as a reminder, whenever you pass an array into another function, you're technically passing it in by reference.

                                                                                                                                            Praise Duarte!

Link to comment
Share on other sites

Link to post
Share on other sites

Almost. The variable type is studentType and the name of the  studentType variable is called p[], so you don't need to include struct into the parameter.

 

You can write it as void read( studentType p[] ) too, it works the same way.

...still "too few arguments" :(

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

...still "too few arguments" :(

You probably forgot to actually pass p into the function inside your main.

                                                                                                                                            Praise Duarte!

Link to comment
Share on other sites

Link to post
Share on other sites

You probably forgot to actually pass p into the function inside your main.

 

read(studentType p[]);

 Like that?

 

Error is now " expected primary-expression before "p" "

 

I also tried read(studentType p[MAX]);

Same error.

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

read(studentType p[]);

 Like that?

 

Error is now " expected primary-expression before "p" "

 

I also tried read(studentType p[MAX]);

Same error.

When you pass in a variable to a function, you don't need to declare what type of variable you're passing in because you've already included the variable type in your function name:

void read( studentType p[] ){    //read function here}

 so when you pass in p[] inside your main, you just need to pass in the variable name p

 

So it would just be 

#include <iostream>using namespace std;struct studentType{    //    //};void read( studentType p[] ) {    //read function here}int main(){    studentType p[MAX];    read(p);    return 0;}

                                                                                                                                            Praise Duarte!

Link to comment
Share on other sites

Link to post
Share on other sites

 

When you pass in a variable to a function, you don't need to declare what type of variable you're passing in because you've already included the variable type in your function name:

void read( studentType p[] ){    //read function here}

 so when you pass in p[] inside your main, you just need to pass in the variable name p

 

So it would just be 

#include <iostream>using namespace std;struct studentType{    //    //};void read( studentType p[] ) {    //read function here}int main(){    studentType p[MAX];    read(p);    return 0;}

Sorry for the late reply, had to do go to school. Thanks for your reply, it's working now.

I'll be doing the other functions by myself now, I'll update you if I any more problems occur/if I have more questions/if I'm already done.

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for the correction, however we haven't really went through the lesson of pointers and references so I'm having a hard time grasping your last few statements.

 

Can you show me the parameters which I should put into void read()?

 

I could probably figure out from that how it applies to the other functions as well.

To pass an argument by reference you need to write YourFunction(type &name); or using pointers YourFunction(type *name);

But since arrays in C/C++ are passed by reference automatically you don't need to do that.

Also since you are writing in C++ why not take advantage of the standard library and use std::string? you just need to include <string>.

Link to comment
Share on other sites

Link to post
Share on other sites

To pass an argument by reference you need to write YourFunction(type &name); or using pointers YourFunction(type *name);

But since arrays in C/C++ are passed by reference automatically you don't need to do that.

Also since you are writing in C++ why not take advantage of the standard library and use std::string? you just need to include <string>.

Understood.

 

 

-cut-

I've done what I could and now there is no problem when compiling the program.. However, when ran, it crashes at the point when it has to output the person with the lowest score...

Also, it seems that the program fails in getting the correct highest score, not with the lowest score though... Seems odd :<

 

Current source code:

#include<iostream>#include<string>#define MAX 3using namespace std;struct studentType{       string studentName;       string studentLastName;       int testScore;       char grade;};void read(studentType p[]){      for(int a = 0; a < MAX; a++)      {      cout << "Enter your FIRST Name: ";      cin >> p[a].studentName;      cout << "Enter your LAST Name: ";      cin >> p[a].studentLastName;       A:      cout << "Enter a score: ";      cin >> p[a].testScore;          if(p[a].testScore < 0 || p[a].testScore > 100)          {           cout << "Invalid input." << endl;           goto A;          }      cout << endl;      }}void assign(studentType p[]){      cout << endl;            for(int b = 0; b < MAX; b++)      {      cout << "Student's name: " << p[b].studentLastName << ", " << p[b].studentName << endl;      cout << "His/her score is: " << p[b].testScore << endl;      cout << endl;      }}void find(studentType p[], int highest, int lowest, int d, int e){      for(int c = 0; c < MAX; c++)      {              if(p[c].testScore > highest)              {                          highest = p[c].testScore;                          d = c;                                        }              if(p[c].testScore < lowest)              {                          lowest = p[c].testScore;                          e = c;              }      }}void print(studentType p[], int highest, int lowest, int d, int e){      cout << "The student with the highest score is: " << p[d].studentLastName << ", " << p[d].studentName << " with a score of: " << highest << endl;      cout << "The student with the lowest score is: " << p[e].studentLastName << ", " << p[e].studentName << " with a score of: " << lowest << endl;}main(){      studentType p[MAX];      int highest, lowest;      int d, e;      read(p);      assign(p);      find(p, highest, lowest, d ,e);      print(p, highest, lowest, d ,e);      system("pause");return 0;}      

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

void find(studentType p[], int highest, int lowest, int d, int e)void print(studentType p[], int highest, int lowest, int d, int e)      find(p, highest, lowest, d ,e);      print(p, highest, lowest, d ,e);

You are not passing those variables by reference, they are just copies, when you print them you just print un-initialized variables, only arrays get passed automatically via reference for every other type you need to use &

Link to comment
Share on other sites

Link to post
Share on other sites

Also try to avoid using goto it could lead you to make mistakes, and in complex projects can be really hard to read.

Link to comment
Share on other sites

Link to post
Share on other sites

In the main function set highest to 0 and lowest to 100.

Thanks~!

 

 

You are not passing those variables by reference, they are just copies, when you print them you just print un-initialized variables, only arrays get passed automatically via reference for every other type you need to use &

Got it, much appreciated! Program is now doing what it was intended to do~

Marking as solved.

 

 

Also try to avoid using goto it could lead you to make mistakes, and in complex projects can be really hard to read.

Noted, I know I could've just used a different process to get the same result but goto just seemed more convenient for me..

Anyways, I guess I'll find out how it can lead to mistakes once I do get to code more complicated programs.

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

You are not passing those variables by reference, they are just copies, when you print them you just print un-initialized variables, only arrays get passed automatically via reference for every other type you need to use &

 

FYI, arrays in C++ are always passed by pointer.

Link to comment
Share on other sites

Link to post
Share on other sites

FYI, arrays in C++ are always passed by pointer.

It's what I said

Link to comment
Share on other sites

Link to post
Share on other sites

It's what I said

 

No, pass by pointer is different from pass by reference.

Link to comment
Share on other sites

Link to post
Share on other sites

FYI, arrays in C++ are always passed by pointer.

 

A pointer can be passed either by value or by reference

 

by value

 

void foo(int* bar);

 

by reference

 

void foo(int* & bar);

 

Arrays generally decay to a pointer, but not always.

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×