Can someone point me where did i do wrong on the coding below?

class Course {
private:
string nameCourse; <-- is this correct?
int totalOfCourses;
public:

   
   void registerCourses(string course[], int numberOfCourses)
  
   {
      totalOfCourses = numberOfCourses;
     
      for(int i=0; i<totalOfCourses; i++)
      {
         nameCourse = course[i];
         
      }
      
      
      
   }
  
   
   
   void displayCourses()
   {
      cout << "Number of Courses: "<< totalOfCourses << endl;
      for(int i = 0; i<totalOfCourses; i++)
         {
            cout << "Course taken: "<< nameCourse[i] << endl;
         }
   }  

   
};

void setCourses(Course&);

int main()
{
   
  
   
      
   Course c;
   
   setCourses(c);
   c.displayCourses();
   
   system("pause");
   return 0;
   
   
}

void setCourses(Course &crs)
{
   int numCourse;
   
   
   cout << "Enter number of courses: ";
   cin >>numCourse;
  

  
   string strCourse[j];
   cout << "Enter courses taken: ";
   
   for(int i=0; i<numCourse; i++)
      {
      //cin >> strCourse[i];
      
      getline(cin,strCourse[i]);
      }  
      
   crs.registerCourses(strCourse,numCourse);
   return; 
  
    
}

Recommended Answers

All 5 Replies

you never #include<iostream> or #include<string> or using namespace std;

you asked if line #3 is correct, it is correct; however, will continue to throw errors until you include the string library.

Ok I have tried include the following

#include <iostream>
#include <string>
using namespace std;

But still not working.
I have a question? How do we accept input array with type string from user and then pass the input to a function?

If you all see my coding, what i;m trying to do is:
1. enter the number of courses? int numCourse
2. then user will input a name of the courses according to number of courses input.
3. Then pass the value of the course (courseName) to function (void register(string course[], int numOfCourse)

Where does j come from on line 66?

In terms of what you are trying to do, at which step is it failing?

My mistake. Here is the coding explaining what j is

void setCourses(Course &crs)
{
   int numCourse;
   int j;
   
   cout << "Enter number of courses: ";
   cin >>numCourse;
  
   j = numCourse;
  
   string strCourse[j];
   cout << "Enter course taken: ";
   for(int i=0; i<j; i++)
   {
     
     getline(cin,strCourse[i]);
   }  
   
   crs.registerCourses(strCourse,numCourse);
  return; 
  
    
}

When I run the coding the output is as follow:

Enter number of courses: 2
Enter courses taken:abc
def
number of courses=2
courses taken:d
courses taken:e

Actually, you do need a string array on line 3 of your original code. This really should be a pointer to string which you initialize using new in the registerCourses method (or if you knew the number, in the constructor for the class).

string * namecourses; //in the private members and 

namecourses = new string[numberOfCourses];

If you're not stuck with this design, it might make more sense to have classes labeled Student and Schedule, where a Schedule object has an array of courses and a number of courses, and each Student object has a schedule.

Thanks for clarifying the j. I'm not sure why you needed an intermediate variable when you could use numCourse. That array on line 11 should be dynamic also (as fixed length arrays with size not known at compile time are non-standard).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.