473,513 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

please answer me,,

11 New Member
hi evryone..
i've couple quistions for you.. and i hope you answer it,,

1\ my dear computer dosen't run the c++ source file..even there is no error..
and that file is the only one i open..if there is any one know..please help me..

2\ i have an array of objects and i want the user to enter the size of the array ..
how am i going to do that..???
thats all...thank you
Nov 9 '06 #1
1 1212
Ganon11
3,652 Recognized Expert Specialist
I can help you with number 2:

In order to allow the user to input the size of the array, you have 2 options:

1) Create a very large array, such as int array[1000]. Ask the user for input into a variable size, and then perform operations only on array[0]...array[size-1]; For example:

Expand|Select|Wrap|Line Numbers
  1.  int array[1000]; 
  2. int size;
  3. cout << "How many values? ";
  4. cin >> size;
  5. for (int i = 0; i < size; i++) {
  6.    cout << "Enter value " << i + 1 << ": ";
  7.    cin >> array[i];
  8. }
However, there is a lot of wasted computer space, since a user may enter a small value (such as 10) for size and leave 990 integer values unused.

Therefore, the much better solution is

2) Use pointers. A pointer is a special type of variable most useful for this type of array and large data structures. It would be impossible for me to fully explain pointers to you, so I will limit my description to pointer arrays.

You know that you cannot execute the following statements:

Expand|Select|Wrap|Line Numbers
  1.  int size; 
  2. cout << "Please enter the size: ";
  3. cin >> size;
  4. int array[size];  // Error!  C++ requires a constant
However, by making array a pointer, you can do this. To make array a pointer, use this statement:

Expand|Select|Wrap|Line Numbers
  1.  int *array; // NOT int array[whatev];
Then you can prompt the user for input into size, and finally say

Expand|Select|Wrap|Line Numbers
  1.  array = new int[size];
For general purposes, these are the only changes you need to know about for pointers. You can now treat array the same as a regular integer array.

Your resulting code would be:

Expand|Select|Wrap|Line Numbers
  1.  int size; 
  2. int *theArray;
  3. cout << "Please input the size: ";
  4. cin >> size;
  5. theArray = new int[size];
  6.  
  7. for (int i = 0; i < size; i++) {
  8.    cout << "Please enter value " << i + 1 << ": ";
  9.    cin >> theArray[i];
  10. }
Nov 9 '06 #2

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

Similar topics

1
2151
by: Numberwhun | last post by:
Hello everyone! I am trying to learn java and have run into kind of a snag. Here is the code that I have so far: ------ <begin_code> ---------- import javax.swing.*; import...
2
1884
by: A. Wiebenga | last post by:
Hi all! I am currently involved in a project in which I am supposed to write a XSLT-transformation sheet for some XML data. I will outline the situation first: I've got one large XML file...
18
2477
by: free2cric | last post by:
Hi, I attanded an interview on C++ Question asked were and my answers to them.. 1. In a CPP program what does memory leak occure? -- i said.. In a constructor , the pointer variables were...
14
1632
by: HP | last post by:
Hi All i have confussion regarding given problem please help me out 4. What happens with the following program: void main(){ myclass* pmc = new myclass; pmc = 0; delete pmc;}
36
1622
by: amanayin | last post by:
As i am not at school for c programming can you please tell me if there is anything bad about this program /* EX4-9.C FUNCTION TO CALL OTHER FUNCTIONS */ #include<stdio.h> float div(float...
1
9588
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
1668
by: Tyla | last post by:
I have been teaching myself javascript and i am having a huge problem with this form. All it has to do is check that every field has something in it and then post, but as soon as it gets to the...
1
54456
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and...
7
1901
by: 511475 | last post by:
What is the best checker to use to find these probles with, besides an experienced set of eyes? This probram is to grade the response to input by user. Grades his marks. Thanks <?xml...
67
2616
by: gator | last post by:
I am quite new to XML and posted a request for example code yesterday. Unfortunately, I did not do a very good job in explaining what I was looking for. Here is an example of a small piece of the...
0
7267
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,...
0
7391
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
7553
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...
1
7120
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
5697
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,...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3247
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...
0
1609
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 ...
0
466
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...

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.