Feb 15, 2013 at 12:21pm UTC
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 Feb 15, 2013 at 12:22pm UTC
Feb 15, 2013 at 12:25pm UTC
Remove the asterisk * from line 48.
Another problem is that you are using i
without initializing it.
Feb 15, 2013 at 12:34pm UTC
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?
Feb 15, 2013 at 12:37pm UTC
I mean that you are using i
without giving it a value.
Feb 15, 2013 at 12:46pm UTC
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?
Feb 15, 2013 at 12:54pm UTC
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 Feb 15, 2013 at 1:19pm UTC
Feb 15, 2013 at 1:07pm UTC
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 Feb 15, 2013 at 1:09pm UTC
Feb 15, 2013 at 1:15pm UTC
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.
Feb 15, 2013 at 1:23pm UTC
Check line 14 on your first post
Feb 15, 2013 at 1:24pm UTC
What are you trying to do there?
Feb 15, 2013 at 1:45pm UTC
This is a previous declaration of the future function at line 14. As I understood we are talking about void input(int , sStud*)
Feb 15, 2013 at 1:46pm UTC
What about my post? Could somebody help?
Feb 15, 2013 at 2:05pm UTC
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?
[co
de] put your code here [/co
de]
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 Feb 15, 2013 at 2:19pm UTC
Feb 15, 2013 at 2:39pm UTC
Peter87, thanks man, it really helped!