Program problem

Ok so I am making a program that should take the number of people in the class and then ask some questions about each student until the number of people in the class is reached or you click enter without entering any information. It should then show the information using different methods.
It doesn't generate any error but when you run it and type in information it messes up information and make a line with ===================== which is anoying. I am pretty sure it has something to do with the array, please have a look, compiling it would maybe help you see what i am talking about. Small adjustments make the output different and I have tried many things.

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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
const int slen = 30;
struct student {
	char fullname[slen];
	char hobby[slen];
	int ooplevel;
};
int getinfo(student pt[], int size);
void display1(student st);
void display2(const student * ps);
void display3(const student pa[], int size);
int main()
{
	cout << "Enter class size: ";
	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";
	cin.get();
	cin.get();
	return 0;
}

int getinfo(student pt[], int size)
{
	char ch;
	int i = 0;
	for(;i < size;i++)
	{
		cout << "Enter fullname: ";
		int o = 0;
		while (cin.get(pt->fullname[o]))
		{
			if(pt->fullname[o] == '\n')
			{
				pt->fullname[o] = '\0';
				break;
			}
			o++;
		}
		o = 0;
		cout << "Enter a hobby: ";
		while (cin.get(pt->hobby[o]))
		{
			if(pt->hobby[o] == '\n')
			{
				pt->fullname[o] = '\0';
				break;
			}
			o++;
		}

		
		if(pt->fullname[0] == '\n' || pt->hobby[0] == '\n')
			break;
		cout << "Enter oop level (number): ";
		cin >> pt->ooplevel;
		cin.ignore(1);
		++pt;

	}
	return i;
}
void display1(student st)
{
	cout << "Student name: " << st.fullname << endl;
	cout << "Hobby: " << st.hobby << endl;
	cout << "OOP, level: " << st.ooplevel << endl << endl;
}

void display2(const student * ps)
{
	cout << "Student name: " << ps->fullname << endl;
	cout << "Hobby: " << ps->hobby << endl;
	cout << "OOP, level: " << ps->ooplevel << endl << endl;
}
void display3(const student pa[], int size)
{
	for(int i = 0;i < size;i++)
	{
		cout << "Student name: " << pa->fullname << endl;
		cout << "Hobby: " << pa->hobby << endl;
		cout << "OOP, level: " << pa->ooplevel << endl << endl;
		pa++;
	}
}
Line 61 :
pt->fullname[o] = '\0';
-> pt->hobby[o] = '\0';
Ty, but doesn't fix the problem with the array
updated 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
const int slen = 30;
struct student {
	char fullname[slen];
	char hobby[slen];
	int ooplevel;
};
int getinfo(student pt[], int size);
void display1(student st);
void display2(const student * ps);
void display3(const student pa[], int size);
int main()
{
	cout << "Enter class size: ";
	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";
	cin.get();
	cin.get();
	return 0;
}

int getinfo(student pt[], int size)
{
	char ch;
	int i = 0;
	for(;i < size;i++)
	{
		cout << "Enter fullname: ";
		int o = 0;
		while (cin.get(pt->fullname[o]))
		{
			if(pt->fullname[o] == '\n')
			{
				
				break;
			}
			o++;
		}
		pt->fullname[o] = '\0';
		o = 0;
		cout << "Enter a hobby: ";
		while (cin.get(pt->hobby[o]))
		{
			if(pt->hobby[o] == '\n')
			{
				
				break;
			}
			o++;
		}
		pt->fullname[o] = '\0';

		
		if(pt->fullname[0] == '\n' || pt->hobby[0] == '\n')
			break;
		cout << "Enter oop level (number): ";
		cin >> pt->ooplevel;
		cin.ignore(1);
		++pt;

	}
	return i;
}
void display1(student st)
{
	cout << "Student name: " << st.fullname << endl;
	cout << "Hobby: " << st.hobby << endl;
	cout << "OOP, level: " << st.ooplevel << endl << endl;
}

void display2(const student * ps)
{
	cout << "Student name: " << ps->fullname << endl;
	cout << "Hobby: " << ps->hobby << endl;
	cout << "OOP, level: " << ps->ooplevel << endl << endl;
}
void display3(const student pa[], int size)
{
	for(int i = 0;i < size;i++)
	{
		cout << "Student name: " << pa->fullname << endl;
		cout << "Hobby: " << pa->hobby << endl;
		cout << "OOP, level: " << pa->ooplevel << endl << endl;
		pa++;
	}
}
Line 67 :
pt->fullname[o] = '\0';
->pt->hobby[o] = '\0';
Edit : Do you know the hand-checking skill? <:)
Last edited on
I don't think so what is it? And have you found out why it messes up the array? I am pretty sure it has something to do with the input.
while (cin.get(pt->fullname[o]))
{
if(pt->fullname[o] == '\n')
{

break;
}
o++;
}
pt->fullname[o] = '\0';


You copied this whole code for "hobby" section but you haven't made any change for "hobby" string yet.

And : pt->hobby[o] = '\0';
This is very necessary. Because the string will look like :


String len : 30

Original : d+$%^83D63-:sv$kflr5lbx{Or68g4+

Input : MyHouse\n
Variable : MyHouseD63-:sv$kflr5lbx{Or68g4+_)_... (Prints all characters (30))

Because the string is allocated (random) so if you forget to set the null value at the end of the string -> the problem will cause.
Last edited on
I got it to work now :) Thank you! Moving on to the next exercise in c++ primer plus :P Expect me!
Topic archived. No new replies allowed.