473,779 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Char array problem

blackstormdragon
32 New Member
Im having trouble with my classList variable. It's a dynamic array of strings used to store the names of the classes(my program ask for students name, number of classes, then a list of the classes).

Expand|Select|Wrap|Line Numbers
  1. typedef char* CharPtr;
  2.  
  3. class Student
  4. {
  5. public:
  6.     Student& operator =(const Student& rightside);
  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(string);
  15.     char getName();
  16.     void reset();
  17.  
  18.  
  19. private:
  20.     string _name;
  21.     int _numClasses;
  22.     char *_classList;
  23. };
  24. void main()
  25. {
  26.     Student classInfo;
  27.  
  28.     classInfo.input();
  29.  
  30.  
  31. }
  32. void Student::input()
  33. {
  34.     CharPtr classList;
  35.     classList = new char[];
  36.     int numberClasses;
  37.     string name;
  38.  
  39.     cout<<"Enter students name ";
  40.     cin>>name;
  41.     setName(name);
  42.  
  43.     cout<<"Enter the number of classes student is taking ";
  44.     cin>>numberClasses;
  45.     setNumClasses(numberClasses);
  46.  
  47.     for(int index = 0; index <  numberClasses; index++)
  48.     {
  49.         cout<<"Enter name of class ";
  50.         cin>>classList[index];
  51.     }
  52.     setClassList(classList);
  53.  
  54. }
  55.  
  56.  
  57.  
Everythinng works till I type in a class. If I type one letter, such as "a", everything is fine. Yet, if I type say "Math" then hit enter. It will print "Enter name of class" and then "press any key to continue". It wont let me type another class. I figured my problem is in the input() finction. I mean I think the problem is that each letter takes up an index space, but I'm just now learning about strings and dynamic arrays so I dont know what a good solution could be.
Thanks.
Apr 29 '07 #1
12 3016
AdrianH
1,251 Recognized Expert Top Contributor
\r is carriage return
\n is carriage return + newline feed

Probably you want \n.
If you want to get technical, in reading from/writing to a text file you are correct... under Windoze. It is not portable though.

Different operating systems define it differently. IIRC, Unix defines \n as carriage return only, Mac defines it as line feed + carriage return (reverse of Windows), other OSs I haven't a clue. I no longer work with text files anymore because of this portability issue, unless I am sure that the file generated will never be shifted to another OS or I don't care ;).


Adrian
Im having trouble with my classList variable. It's a dynamic array of strings used to store the names of the classes(my program ask for students name, number of classes, then a list of the classes).

Expand|Select|Wrap|Line Numbers
  1. typedef char* CharPtr;
  2.  
  3. void Student::input()
  4. {
  5.     CharPtr classList;
  6.     classList = new char[];
  7.     int numberClasses;
  8.     string name;
  9.  
  10.     cout<<"Enter students name ";
  11.     cin>>name;
  12.     setName(name);
  13.  
  14.     cout<<"Enter the number of classes student is taking ";
  15.     cin>>numberClasses;
  16.     setNumClasses(numberClasses);
  17.  
  18.     for(int index = 0; index <  numberClasses; index++)
  19.     {
  20.         cout<<"Enter name of class ";
  21.         cin>>classList[index];
  22.     }
  23.     setClassList(classList);
  24.  
  25. }
  26.  
  27.  
  28.  
Everythinng works till I type in a class. If I type one letter, such as "a", everything is fine. Yet, if I type say "Math" then hit enter. It will print "Enter name of class" and then "press any key to continue". It wont let me type another class. I figured my problem is in the input() finction. I mean I think the problem is that each letter takes up an index space, but I'm just now learning about strings and dynamic arrays so I dont know what a good solution could be.
Thanks.
When you assigned classList = new char[], it should have given an error (my reasoning is you didn’t specify how many elements in the array, but I guess it figures you mean one element by default). Because it created a one element char array, you could do a single cin to it, which is the first letter.

I am guessing that you want the user to enter a number of classes, so classList should be a vector of strings.

Hope that helps,


Adrian
Apr 29 '07 #2
blackstormdragon
32 New Member
I'd use a vector, but this is a class assignment(whic h I should have mentioned.)
The assignment stated that "classList – A dynamic array of strings used to store the names of the classes that the student is enrolled in"

Now I did change classList = new char[], to classList = new char[numberClasses]; but that didnt help.
Apr 29 '07 #3
AdrianH
1,251 Recognized Expert Top Contributor
I'd use a vector, but this is a class assignment(whic h I should have mentioned.)
The assignment stated that "classList – A dynamic array of strings used to store the names of the classes that the student is enrolled in"

Now I did change classList = new char[], to classList = new char[numberClasses]; but that didnt help.
Tell me how you would do it with a vector. We'll move on from there.


Adrian
Apr 29 '07 #4
blackstormdragon
32 New Member
It would look something like this wouldnt it.

Expand|Select|Wrap|Line Numbers
  1. vector<char> classList;
  2.  
  3. for(int index = 0; index <  numberClasses; index++)
  4. {
  5.     cout<<"Enter name of class ";
  6.     cin>>className;
  7.                 classList.push_back(className);
  8.  
  9. }
  10.  
Apr 29 '07 #5
AdrianH
1,251 Recognized Expert Top Contributor
It would look something like this wouldnt it.

Expand|Select|Wrap|Line Numbers
  1. vector<char> classList;
  2.  
  3. for(int index = 0; index <  numberClasses; index++)
  4. {
  5.     cout<<"Enter name of class ";
  6.     cin>>className;
  7.                 classList.push_back(className);
  8.  
  9. }
  10.  
No, not quite. You told me:
The assignment stated that "classList – A dynamic array of strings used to store the names of the classes that the student is enrolled in"
Think about it and try again. Your almost have it. Once you see it, you'll be kicking yourself (like I have done on several assignments ;))


Adrian
Apr 30 '07 #6
blackstormdragon
32 New Member
Should I be using string instead of char??? If so, then I must of done something wrong before.
Apr 30 '07 #7
AdrianH
1,251 Recognized Expert Top Contributor
Should I be using string instead of char???
Yes, use string instead of char.
If so, then I must of done something wrong before.
Why do you think you did something wrong before?

You could use an array of an array of chars, but if you do so, you need to handle the buffer overflow problem.

I.e. If your array of chars is say 20 chars long and someone types in a word that is 20 or more characters long, your programme may do something unexpected as it will go past the end of the buffer running in to some other portion of memory.

Of course, your prof may not require this, but you should ask if that is the case and setup your code accordingly.


Adrian
Apr 30 '07 #8
blackstormdragon
32 New Member

Why do you think you did something wrong before?
When I tried string before this wierd screen popped up. Now it works though, so I presumed I typed something in wrong before.
Thank you so much, everything works now. I really appreciate your help.
Apr 30 '07 #9
AdrianH
1,251 Recognized Expert Top Contributor
When I tried string before this wierd screen popped up. Now it works though, so I presumed I typed something in wrong before.
Weird screen? Do you by chance remember what it said?

Thank you so much, everything works now. I really appreciate your help.
No prob, glad to help.


Adrian
Apr 30 '07 #10

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

Similar topics

4
7699
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something simple, but its just not popping into my mind at the moment. My little snippet of code is below. Basically, the studentID array is dynamic so it will fit any length of a Student's Name. What I'm trying to do is place this chunk of code into a...
7
1570
by: Andreas Lassmann | last post by:
hi there, i've got a problem: can i create a dynamic array like this? pMap = new char; gcc (my compiler) sais, it's wrong... i know that dynamic memory is more often used in this way: pMap = new char;
5
3409
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);
8
3687
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
6
2982
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
5
4144
by: Bill Carson | last post by:
I'm trying to dynamically allocate memory to an array of strings with the following (incomplete, for reference only) : int nLines, nChars, m, n, Cols.sTcolumn ; char ***sAtt; sAtt = (char ***)malloc(nLines * sizeof(char *)); for(m=0; m < nLines; m++) {
5
3760
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I believe the problem centers around my handling of strings, arrays, pointers and dynamic memory allocation. Here is the problem I'm trying to solve: I want to fill a list box with a list of Project Names from a database (in Palm this is more...
3
3998
blackstormdragon
by: blackstormdragon | last post by:
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...
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
9632
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
9471
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,...
0
9925
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
8958
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
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5372
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
4036
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
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
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.