473,320 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dynamic Char array problem

blackstormdragon
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 2988
AdrianH
1,251 Expert 1GB
\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
I'd use a vector, but this is a class assignment(which 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 Expert 1GB
I'd use a vector, but this is a class assignment(which 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
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 Expert 1GB
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
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 Expert 1GB
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

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 Expert 1GB
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
Something about a new instance of visual studio or something. It keep popping up and doing a weird debugging thing. (Hope this makes sense.)
Apr 30 '07 #11
AdrianH
1,251 Expert 1GB
Something about a new instance of visual studio or something. It keep popping up and doing a weird debugging thing. (Hope this makes sense.)
No, not enough info. Well, I guess it doesn't matter right now since it works, and right now that is what counts. :)


Adrian
Apr 30 '07 #12
No, not enough info. Well, I guess it doesn't matter right now since it works, and right now that is what counts. :)


Adrian
Yeah, your right it works now and thats all that matters.
Apr 30 '07 #13

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

Similar topics

4
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...
7
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...
5
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...
8
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...
6
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...
5
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...
5
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...
3
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...
20
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()...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.