Cannot convert

The mistake is error: cannot convert 'sStud' to 'sStud*' for argument '2' to 'void input(int, sStud*)'
I don't know what shoud I do with this mistake.

Here is the code:
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
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <sstream>

#define Mark 5
using namespace std;

struct sStud{
	string Name;
	int Group;
	int Ses[Mark];
};

void input(int, sStud*);


void input(int i, sStud *p)
{
	int n, t;
	string mystr;	
		
	cout<<"Enter number of student: ";
	cin>>i;	
	
	sStud stud[i];
	
	p=&stud[i];
	for(n=0; n<i; n++){
		cin.get();
		cout<<"Enter student's name: ";
		getline(cin,p->Name);
		cout<<"Enter student's group: ";
		getline(cin,mystr);
		stringstream(mystr)>>p->Group;
		cout<<"Enter 5 student's marks: ";
		for(t=0; t<Mark; t++){
			cin>>p->Ses[t];
		}
	}
}
		
int main()
{
	int i;
	sStud *p, st[i];	
		
	p=&st[i];
	input(i,*p);   //mistake here


	return 0;
}
Last edited on
Remove the asterisk * from line 48.

Another problem is that you are using i without initializing it.
Thanks about * from line 48. I removed it and program compiled. But after execution compiler wrote: segmentation fault
In main variable i was previously initialized at line 44 int i;. Or maybe I missed something. Can you help once more?
I mean that you are using i without giving it a value.
Aint i set to 0 if you dont set a value for it?
And may i ask, you have used two i integers, do you have to initialize them two times too?
I did that program before. But I did it all in main(). Yet now I want do it by few functions.
Sorry for may silly. I changed program in that way:
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
#include <iostream>
...
void input(int i, sStud *p)
{
	int n, t;
	string mystr;	
		
	for(n=0; n<i; n++){
		cin.get();
		cout<<"Enter student's name: ";
		getline(cin,p->Name);
		cout<<"Enter student's group: ";
		getline(cin,mystr);
		stringstream(mystr)>>p->Group;
		cout<<"Enter 5 student's marks: ";
		for(t=0; t<Mark; t++){
			cin>>p->Ses[t];
		}
	}
}

int main()
{
	int i;
	cout<<"Enter number of students: ";
        cin>>i;

        sStud *p, st[i];	
		
	p=&st[i];
	input(i,*p);  

	return 0;
}


And after execution compiler wrote:
Enter number of students: 3
Enter student's name: segmentation fault

Previously thanks!
Last edited on
You could use printf and scanf. It would be really easier...

*Edit: by the way, how could i add my scripts in script lines like you do?
Last edited on
Doesn't metter cout, cin or printf, scanf...The program isn't compiling.

I didn't understand what script. Could you write down number of lines.
Check line 14 on your first post
What are you trying to do there?
This is a previous declaration of the future function at line 14. As I understood we are talking about void input(int, sStud*)
What about my post? Could somebody help?
CountVlad wrote:
Aint i set to 0 if you dont set a value for it?
No. If you use the uninitialized value the behaviour is undefined, which means anything could happen. In practice what happens is that i gets the garbage value that happened to be stored at that memory position.

CountVlad wrote:
*Edit: by the way, how could i add my scripts in script lines like you do?
[code] put your code here [/code]
You can also select the code and press the <> button to the right.
And by the way, C++ code is usually not called "scripts".

p=&st[i];
Accessing st[i] is out of bounds if the size of st is i. If you want p to point to the first element in st you can do p=&st[0];, or p=st; because arrays implicitly decay to a pointer to the first element. As you see there is not much point having the variable p. You can just pass st to the function directly.
input(i, st);
Last edited on
Peter87, thanks man, it really helped!
Topic archived. No new replies allowed.