473,789 Members | 2,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic array problems

blackstormdragon
32 New Member
It seems pointers and dynamic arrays are giving me a hard time.
Heres part of the assignment. We have to create a class named Student that has three member variables. One of the variables is called classList – "A dynamic array of strings used to store the names of the classes that the student is enrolled in"

My problem is when Im trying to send classList from my input function to my setClassList function. I keep getting the error "subscript requires array or pointer type' and I have no clue what that means. Heres some pieces of my code.
Expand|Select|Wrap|Line Numbers
  1. typedef char* CharPtr;
  2.  
  3. class Student
  4.  
  5. {
  6. public:
  7.     ~Student();
  8.     void input();
  9.     void output();
  10.     void setNumClasses(int);
  11.     int getNumClasses();
  12.     void setClassList(char);
  13.     char getClassList();
  14.     void setName(char);
  15.     char getName();
  16.     void reset();
  17.  
  18.  
  19. private:
  20.     char _name;
  21.     int _numClasses;
  22.     char _classList;
  23. };
  24. void Student::input()
  25. {
  26.     CharPtr classList;
  27.     classList = new char[];
  28.     int numberClasses;
  29.     char name;
  30.  
  31.     cout<<"Enter students name ";
  32.     cin>>name;
  33.     setName(name);
  34.  
  35.     cout<<"Enter the number of classes student is taking ";
  36.     cin>>numberClasses;
  37.     setNumClasses(numberClasses);
  38.  
  39.     for(int counter = 0; counter < numberClasses; counter++)
  40.     {
  41.         cout<<"Enter name of class ";
  42.         cin>>classList[counter];
  43.         setClassList(classList[counter]);
  44.     }    
  45.  
  46. void Student::setClassList(char classList)
  47. {
  48.     int index = 0;
  49.     _classList = classList[index];
  50. }
Now this isnt all my code, these are just the snipets Im having problems with. Oh and the classList is a char because we havent gone over strings yet.
Thanks.
Apr 27 '07 #1
3 3998
Savage
1,764 Recognized Expert Top Contributor

void Student::setCla ssList(char classList)
{
int index = 0;
_classList = classList[index];
}
In ur typedef u have char* CharPtr which is pointer.

later in main u have CharPtr classList so this also will be a pointer.

In ur function u are calling char classList which is simple char not pointer to char.

Think about this

Savage
Apr 27 '07 #2
blackstormdragon
32 New Member
In ur typedef u have char* CharPtr which is pointer.

later in main u have CharPtr classList so this also will be a pointer.

In ur function u are calling char classList which is simple char not pointer to char.

Think about this

Savage
Thanks, you hint sent me in the right direction. I think I figured it out.
Apr 29 '07 #3
Savage
1,764 Recognized Expert Top Contributor
Thanks, you hint sent me in the right direction. I think I figured it out.
Always a pleasure......

Savage
Apr 29 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3410
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to be done using dynamic arrays. I don't know the size of the text file ahead of time, though, so I created a class that includes a method to resize the array. Here is that class: class Data { public: Data(int initialsize);
6
2984
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of strings'. I already know that no individual string will be longer than 50 characters. I just don't know before run time how many elements of the array will be needed. I have heard it is possible to dynamically allocate memory for a 2
10
1708
by: Russell Mangel | last post by:
I have written the following program using VS2005. The program is a Dynamic Array similar to System.Collections.ArrayList in .NET. The program works okay until I reach 65536, I can't seem to figure out why, as it seems my logic is working okay. I am a .NET programmer so I am not used to dealing with un-managed C++ code. Please criticize my code if you think it is poorly written. This is the loop from main() that will blow up the program...
13
14622
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and Nicolai M. Josuttis. Chapter 14.) How to implement analogue of this technique via static polymorphism? Perhaps there is special design pattern for this purpose...
23
7420
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
2
2023
by: ayan4u | last post by:
ok i have two problems... firstly in strict C enviornment is it possible to have a true dynamic charecter array with no predefined length...i mean to say... suppose.. char *array =NULL; /* actually i dnt know what the exact size of my string is so i aquire sace for 1 charecter from heap /*
1
4418
by: Tinku | last post by:
Hi friends I know Static Hashing and i know about Dynamic Hashing, still i have problem to make program with Dynamic Hashing I am new in "C" world, please help me, my problem is: i have to make program in Dynamic hashing i have to store int value in nodes user only enter int value by this value i have to find hash key and make symbol table my struct are
13
8952
by: kwikius | last post by:
Does anyone know what a C99 dynamic array is, and if it will be useable in C++? regards Andy Little
20
6554
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE() function in C.. But if I try to use a single linked list with a static char array fields I can free the memory allocated with out any problems using the FREE(). So, why freeing a single linked list with dynamic char* is hard and why the FREE() function is...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.