Newbie back for more

So I've got a lab due tuesday and I am at the point where I'm just confused. It's a skeleton code and I have no idea really where to go next. I remember that its involving pointers and that they are .ptr. (I think) I honestly think the main problem is the code is like already half complete I just have to fill in the blanks I've already started on some and am going to post it in here, any tips on pointers would be alot of help aparently I'm supposed to point to an arry and use new then delete the pointer when I am finished with it. Kinda like ptr_int=array[number] I honestly want to use a vector and then jsut resize it to the number. Any help will be awesome!!! All of the tips //s are what we are ment to put there to make it work. I've started writing some code and am working on it right now also. I don't think we are suposed to change anything that was already put there. Thanks again for any help :)

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
using namespace std;
const int SLEN = 30;

struct student 
{
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};
// getinfo() has two arguments: a pointer to the first element of
// an array of student structures and an int representing the
// number of elements of the array. The function solicits and
// stores data about students. It terminates input upon filling
// the array or upon encountering a blank line for the student
// name. The function returns the actual number of array elements
// filled.
int getinfo(student pa[], int n);
{
cout << "Please enter the students name: " << endl;
cin >> char fullname[SLEN];
cout << "Pleaser enter the students hobby: " << endl;
cin >> char hobby[SLEN];

}


// display1() takes a student structure as an argument
// and displays its contents
void display1(student st);





// display2() takes the address of student structure as an
// argument and displays the structure’s contents
void display2(const student * ps);






// display3() takes the address of the first element of an array
// of student structures and the number of array elements as
// arguments and displays the contents of the structures
void display3(const student pa[], int n);






int main()
{
	cout << "Enter the number of students: ";
	int class_size;
	cin >> class_size;
	while (cin.get() != '\n')
	{
	continue;
	}
	student *ptr_stu = new student[class_size];
	int entered = getinfo(ptr_stu, class_size);

	for (int i = 0; i < entered; i++)
	{
		display1(ptr_stu[i]);
		display2(&ptr_stu[i]);
	}
	display3(ptr_stu, entered);
	delete [] ptr_stu;
	cout << "Done\n";

	return 0;
}


No, there's a whole bunch of stuff that's written here which you should edit.

- The first of which is the 'char' declarations on Lines 21 and 23.

- "getInfo(...)" should be a member function of the 'student' struct (IMO).

- The while loop on Lines 60 - 63 is actually making me angry. Does anybody know what the instructor is trying to do here? Is the call to "getinfo(...)" supposed to go inside here? That's the only guess I have.

So in short, you're stuck because you're assuming that you are not supposed to change the code that has been written when in fact the code that is written here does not work.
closed account (zb0S216C)
Multiple issues with your code. Here they are:

student needs a constructor to initialize its members.

getinfo( ) needs to return an integer.

Evilcise wrote:
int class_size; (sic)

Initialize this. It doesn't matter if the variable is assigned a value on the next line, initialization is good practice.

Evilcise wrote:
1
2
3
4
while (cin.get() != '\n')
{
continue;
}
(sic)

Pointless. This is a waste of CPU time.

student *ptr_stu = new student[class_size]; (sic)
class_size needs to be a constant.

Since you never check for null after the allocation of ptr_stu, you could be writing to unknown memory. An exception would be thrown anyway.

Wazzak
Last edited on
Yeah I know the while loop is a pain, we talked about it in class, aparently its there if you put in a space it still reads it as one so long as you dont hit enter. As for the code not working I have no idea really until its all coded to see if it works I can't just look at it and see. I belive we are supposed to write everything above int main. I'm not sure about anything below that. It could be a learning process for us I honestly don't know that while loop got alot of fuss in the class.
Topic archived. No new replies allowed.