Help with Strings & Functions

I am still rather new to programming, so if you could keep your explanations as “English” as possible, I would appreciate it!

What I’m attempting to do with this program is using the function “studentInformation()” under the class “student” is as the functions name suggests, gather student information. When attempting to do this Visual Studio says, “Error: declaration is incompatible with “void student::studentInformation()” (declared at line 14). I’ve only just started on this program, and in the end I’m assuming I’m going to have to put all the information (First name, last name, A number, grade) into an array so I can use a search tool.

1. How can I use a string, int, and double in one function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>

using namespace std;

class student
{
	string firstName;
	string lastName;
	int aNumber;
	double grade;

public:
	void studentInformation()
	{	
		cout << "--Student information--" << endl;
		cout << "First Name: " << endl;
		getline(cin,firstName);
		cout << "Last Name: " << endl;
		getline(cin,lastName);
		cout << "A-Number: A" << endl;
		cin >> aNumber;
		cout << "Grade: " << endl;
		cin >> grade;
	}
};

void student::studentInformation(string firstName2, string lastName2, int aNumber2, double grade2)
{
	firstName = firstName2;
	lastName = lastName2;
	aNumber = aNumber2;
	grade = grade2;
}

int main()
{

	student studentInfo;
	studentInfo.studentInformation();


	return 0;
}

Last edited on
well your biggest problem here is that you've declared your function student::studentInformation twice. Creating the class, with the function in it creates the function once. Then you created another function(void) outside of your class with the exact same name, and in the same place(memory wise) with all sorts of parameters.


Also, you did not declare firstName2, lastName2, aNumber2, or grade2. I'm not really sure what you were going for with the void student::studentInformation... function. This might help you get started.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//your class code here

void someFunction(string name, string surname, int number, double grade); 
//prototype your function

int main()
{
       string firstName, lastName;
       int number = 0; //initialize variables
       double grade = 0;

       student::studentInformation();//calls the function from its class
       
       someFunction(firstName, lastName, number, grade); // calls a function with the four types of variables you asked for
}
void someFunction(string name, string surname, int number, double grade)
{

   //some code
}
       



A big thing you need to remember here is that you can pass the values like the variables received from your cin statements from your main to your class... You don't need to get that information from your class, you should however, be passing variables TO your class... for example: student::studentInformation(); Should look more like the void function I created(hint, hint!);

Last thing, when you stated:
I’m assuming I’m going to have to put all the information (First name, last name, A number, grade) into an array so I can use a search tool.

Strings are useful for this. They are already arrays. It depends on how many of these variables you are going to get... so if this pops into your head again, try doing a bit of research on how strings are useful as search tools. You may even need to create a text file with #include<fstream> header(I challenge you to do a bit of research on that one too, if you will need to make a lot of things to search through)
Last edited on
Topic archived. No new replies allowed.