class name student

Not able to enter more than one letter in for loop for class list: classList - An array of strings for the names of the classes the student is enrolled in.

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  //Reads data and displays student name, class credits, and name of classes
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class Student
{
	public: //public class of student 
		Student(string first_name,string last_name, int num, string list);
		Student();
		
		void input();//input from user
		void output();
		
		
		
		//accessors 
	const	string get_first_name();
	const   string get_last_name();
	const	int get_numClasses();
	const	string get_classList();
		
		//mutators
		void set_first_name(string first);
		void set_last_name(string last);
		void set_numClasses(int num);
		void set_classList(string list);
		
   private: //cannot be accessed 
   	string first_name;
	string last_name; 
    int numClasses;
   	string classList;
		
	
	
};
	Student::Student()   //constructor variable 
	{
	first_name;
	last_name;
	numClasses = 0;	
	classList;	
	}

	string const Student::get_first_name() //accessor get called from class student public
	{
		return first_name;
	}
	string const Student::get_last_name()
	{
		return last_name;
	}
	int const Student::get_numClasses()
	  {
	  	return numClasses;
	  }
	  string const Student::get_classList()
	  {
	  	return classList;
	  }						//accessor get called from class student public 
	
	
	
void Student::set_first_name(string first) //mutator set called from class student public
	{
		first_name = first;
	}
	void Student::set_last_name(string last)
	{
		last_name = last; 
	}
	void Student::set_numClasses(int num)
	{
		numClasses = num;
	}
	void Student::set_classList(string list)
	{
		classList = list;
	}							  
	void Student::input() 			//accessor 
	{
		int i;
										
	 cout<<"Enter first name and last name:"<<endl;
	 cin>>first_name>>last_name;
	 cin.ignore();
         cout<< "Get number of classes taking this semester: "<<endl;
	 cin >> numClasses;
	 cin.ignore();
	    
	   ;										
			if(numClasses>	0)
    		{
    			 classList = new char[numClasses];
    			 for(i=0;i<numClasses;i++)
    		{
                  									
		cout<<"Enter name of classes " <<(i+1)<<endl;
    		cin>>classList[i];
    														
    		}
    		}
    	
    	cout<< '\n'<<endl;
	}	//accessor 
    
    void Student::output()//output what user entered
    {
    	cout<< "Student first name: " << first_name <<endl;
    	cout<< "Student last name: " << last_name <<endl;
    	cout<< "Number of classes: " << numClasses <<endl;	
    	cout<< "Class List: " << classList<<endl;
    
    }

	int main()
	{
	 char play_again = 'Y'; 
     while (play_again == 'y' || play_again == 'Y'){
     
	
	Student s1;	//student class s1
	s1.input(); //input from student 1

	s1.output(); //output from student 1
	
	cout<<endl;
	
	
	
	 cout<<"Enter the data again (press Y/N)\n "<<endl;
           cin>>play_again;
}

		
	if(play_again == 'n')
	{
		return 0;
		
	}
	system("pause");
		
	return 0;
		
	}
	
Last edited on
closed account (o3hC5Di1)
Hi there,

Please consider this code:

94
95
96
97
98
99
100
101
102
103
	if(numClasses>	0)
    		{
    			 classList = new char[numClasses];
    			 for(i=0;i<numClasses;i++)
    		{
                  									
		cout<<"Enter name of classes " <<(i+1)<<endl;
    		cin>>classList[i];
    														
    		}


You declare classList as an array of char's. Char is a datatype which holds only one character. What you mean to do is classList = new std::string[numClasses];

For the record, I would advise you to use std::vector or std::array rather than "raw" arrays. They take care of a lot of possible memory pitfalls for you and are thus much safer to use.

All the best,
NwN

Thanks,using dev c++ so I also had to use a pointer to compile the program. I works fine, I just have to code a output for the class list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

if(numClasses>	0)
   {
    	string*	classList = new std::string[numClasses];
    	for(i=0;i<numClasses;i++)
    	{
    	cout<<"Enter name of classes " <<(i+1)<<endl;
        cin>>classList[i];
    	}
  }														
    															
    														
    														}
Topic archived. No new replies allowed.