| | |
garbage value ?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
an array is essentially a variable which can store a number of values, all stored one after another in memory and all having a certain index;
eg
numbers2 is the variable name.
*numbers2 is a pointer to the first in the list (therefore the variable name refers to the head of the array)
Each item can be retreived using the [] operator;
numbers2[0] = 0 // note, the first element is index 0!!
numbers2[1] = 1
numbers2[2] = 14
ect...
arrays can be initialised after they are declared and the best method is a loop eg:
this code will ask the user to input 5 values, which are inserted into the array.
eg
C++ Syntax (Toggle Plain Text)
int numbers[10]; // array of 10 integers, unitialised int morenumbers[3] = { 27, 4, 1 }; // array of 3 numbers, initialised int numbers2 [] = { 0, 1, 14, 25 }; // array of 4 numbers, initialised (the [] means the compiler determines the size, in this case 4 as we have asked for 4 numbers
numbers2 is the variable name.
*numbers2 is a pointer to the first in the list (therefore the variable name refers to the head of the array)
Each item can be retreived using the [] operator;
numbers2[0] = 0 // note, the first element is index 0!!
numbers2[1] = 1
numbers2[2] = 14
ect...
arrays can be initialised after they are declared and the best method is a loop eg:
C++ Syntax (Toggle Plain Text)
int numbers[5]; // array char *str; // input string for(int i = 0; i < 5; i++) // goes from 0 to 4 (as arrays start at 0) { cout << "Enter number " << i << " for the array"; cin >> str; data = atoi(str); numbers[i] = data; }
this code will ask the user to input 5 values, which are inserted into the array.
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
int numbers[5]; // array char *str; // input string for(int i = 0; i < 5; i++) // goes from 0 to 4 (as arrays start at 0) { cout << "Enter number " << i << " for the array"; cin >> str; data = atoi(str); numbers[i] = data; }
u get garbage values when u have not initialised your variables or arrays inthis case. because when your compiler aloocates a memory space to your array that space may already contain values- hence garbage values
:lol: I am not one of those who wait for things to happen, :p but one of those who make things happen ;)
or even easier...
needs error checking though
C++ Syntax (Toggle Plain Text)
int numbers[5]; // array for(int i = 0; i < 5; i++) // goes from 0 to 4 (as arrays start at 0) { cout << "Enter number " << i << " for the array"; cin >> numbers[i] = data; }
needs error checking though
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
Similar Threads
- Getting garbage value (C)
- the garbage collector... (Java)
- why is double data type giving an garbage value (Java)
- Getting garbage value (C)
- Detecting garbage inputs (C++)
Other Threads in the C++ Forum
- Previous Thread: CAn any1 tell me watz wrong in da code
- Next Thread: Help w/ dynamic list funtction definitions
Views: 4709 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






