| | |
Array Troubles
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
If I were to ask the user to enter the number of employee. Could I put that user entered number into an array? If so how?
for example
this code doesn't actually work because I already tried it and it gives me an error. Any help would be great. Thanks
for example
C++ Syntax (Toggle Plain Text)
int num; cout << "enter number of employees: " ; cin >> num; array[num];
this code doesn't actually work because I already tried it and it gives me an error. Any help would be great. Thanks
You want a user to enter the number of employees and an array with that number of elements? That should be easy enough.
There are a number of options. You could use traditional dynamic memory allocation or alternatively a container class like vector. I'm going to assume you've not learnt the latter so you know anything about dynamic memory allocation (new/delete)? If the size of an array is not determinable when writing the program you can use new to allocate memory. The only thing is (unless you're using a pointer class like auto_ptr or something), you're going to have to delete the memory at the end of your program:
Or did I completely misinterpret your question?
>> Could I put that user entered number into an array? If so how?
I think I might have.
There are a number of options. You could use traditional dynamic memory allocation or alternatively a container class like vector. I'm going to assume you've not learnt the latter so you know anything about dynamic memory allocation (new/delete)? If the size of an array is not determinable when writing the program you can use new to allocate memory. The only thing is (unless you're using a pointer class like auto_ptr or something), you're going to have to delete the memory at the end of your program:
cpp Syntax (Toggle Plain Text)
int *employee_numbers; int num_employees; std::cout<< "How many employees? "; std::cin >> num_employees; employee_numbers = new int[num_employees]; // fill the array delete [] employee_numbers;
Or did I completely misinterpret your question?
>> Could I put that user entered number into an array? If so how?
I think I might have.
cpp Syntax (Toggle Plain Text)
int nums[5]; int num; std::cout<< "Enter num: "; std::cin >> num; nums[0] = num; nums[1] = 42; // etc std::cout<< nums[0];
Last edited by twomers; Nov 19th, 2007 at 6:31 pm.
![]() |
Similar Threads
- Array troubles? (C++)
Other Threads in the C++ Forum
- Previous Thread: Remove newline character
- Next Thread: Looping error
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






