473,414 Members | 1,596 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,414 software developers and data experts.

Dynamic array....probable memory allocation problem..need advise!

Baka otaku
The program I need to write must accept code no, name and total bill amount for "n" number of customers and then print all the things along with the grand total. I worked out the program and while building it shows no errors. while debugging, it takes the first set of values but after that it says - The instruction at "0x00402161" referenced memory at "0xcccccccc". The memory could not be "written". Is there something wrong with the program or memory...please help.

The code is :

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class customer
  5. { private:
  6. int ncust;
  7. int *codeno;
  8. char **name;
  9. int *tbill;
  10.  
  11. public: 
  12. void cust_accept(void);
  13. void cust_print(void);
  14. };
  15.  
  16. void customer::cust_accept(void)
  17. { cout << "Enter the total number of customers " << endl;
  18. cin >> ncust;
  19. codeno=(int*)new int[ncust]; 
  20. name=(char**)new char*[ncust];
  21. for (int i=0; i<ncust; i++)
  22. { name[i]=(char*)new char[20];
  23. }
  24. for (int j=0; j<ncust; j++)
  25. { cout << "Enter the customer code no " << endl;
  26. cin >> codeno[j];
  27. cout << "Enter customer name " << endl;
  28. cin >> name[j]; // insert name of customer
  29. cout << "Enter the total bill amount " << endl;
  30. cin >> tbill[j];
  31. }
  32. }
  33.  
  34. void customer::cust_print(void)
  35. { for(int i=0;  i<ncust; i++)
  36. { cout << "Customer" << endl; 
  37. cout << "Code no   Name   Total Bill amount " << endl;
  38. cout << codeno[i] ;
  39. cout << name[i] ;
  40. cout << tbill[i];
  41. }
  42. int gtotal;
  43. for (int j=0; j<ncust; j++)
  44. {gtotal+=tbill[j];
  45. cout << endl;
  46. cout << "Grand total" << gtotal;
  47. }
  48. }
  49.  
  50.  
  51. int main ()
  52. { customer srec;
  53. srec.cust_accept();
  54. srec.cust_print();
  55. return 0;
  56. }
  57.  
  58.  
Mar 6 '07 #1
1 2051
horace1
1,510 Expert 1GB
the memory you allocate for name is the number of customers
Expand|Select|Wrap|Line Numbers
  1. name=(char**)new char*[ncust];
  2.  
so if ncust is 3 each name is a maximum of 2 characters long
perhaps it should be
Expand|Select|Wrap|Line Numbers
  1. name=(char**)new char*[20];
  2.  
and the name can be up to 20 characters long.

would it not be better for class customer to hold data on one customer and then have a vector of customers?
Expand|Select|Wrap|Line Numbers
  1.     vector<customer> v;
  2.  
Mar 6 '07 #2

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

Similar topics

12
by: Tan Thuan Seah | last post by:
Hi all, I was told this in one of the university course I was doing. In C we may expect good performance for: double a, c, d; for (i=0; i<N; i++) for(j=0; j<N; j++) a = a + c *d;
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
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...
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.