#include<iostream>;
using namespace std;
int main(){
    
    char arr[8]={};
    
    for(int count(0); count<=7; ++count){
            
            
            arr[count]='*';
            
            }
    cout<<arr;
    
    
    cin.get();
   return 0; }
I really do not understand how some garbage values get stored into the array; even after I have initialized it properly; and how it can display MORE values than what I have stored into it; like if my array size for character array is 10; it will show more than that !! 


I use Dev C++ to write, compile my code.

I have attached the code; that I run. 


In the below code; 3-4 EXTRA values get displayed as well; they are useless characters; I am not sure where they come from !!

Edited 7 Years Ago by uzimuzi: - i am new here; new to the interface of forum

Attachments
#include<iostream>;
using namespace std;
int main(){
    
    char arr[8]={};
    
    for(int count(0); count<=7; ++count){
            
            
            arr[count]='*';
            
            }
    cout<<arr;
    
    
    cin.get();
   return 0; }

For displaying arrays you do not just call the array its self you have to make a for() loop to output.

for(int i = 0; i < 8; i++)
{
	cout << arr[i];
}

also you have not terminated the array with a newline value so cout does not know where the end oh the char array is.

Thank you; ^ and ^^ for your prompt reply :)

I have another problem similar problem; i.e.

now ONCE i have declared a char array of a particular size; if I add more values into other indexes of it; it stores them; its like the char array is EXPANDING automatically; while in int array; it gives an error !! Why does this happen ?

PS - after i can understand the reason for this problem; how can i go back to this thread and mark it as solved; and access it; ( i am new; and i really do not understand how to access the threads that i have begun )

my code as follows:

#include <iostream>

using namespace std;

int main()

{
char arraY[1]={};
//only one char allowed in it; with index name 0
arraY[0]='b';

for(int count(0); count<=2; ++count)
{
        arraY[count]='t';
        }

for(int count(0); count<=2; ++count)
{
        cout<<arraY[count];  }
    
    
    
    cin.get();
return 0;}

Edited 7 Years Ago by uzimuzi: n/a

You log in and click your name on the top left hand corner then click statistics >> Find all threads started by name. Then just select the thread that you want to go to. To mark as solved scroll to the bottom where it says reply to thread in a yellow box and just to the right of that is blue text that says mark as solved.

sure, thanks. I understand, could also please help me understand why the size of the array expands ?

-uzi

I'm not sure because when I was doing some testing I made a char array size 2 and the size of it said it was 2 bytes in size but there were 5 characters in the array.

Thank you; ^ and ^^ for your prompt reply :)

I have another problem similar problem; i.e.

now ONCE i have declared a char array of a particular size; if I add more values into other indexes of it; it stores them; its like the char array is EXPANDING automatically; while in int array; it gives an error !! Why does this happen ?

Bottom line, array does not resize itself. Your problem has to
do with memory. Your code has a bug; You declared char arraY[1],
but in your for loop you are looping it from 0 to 2, which has 3 index.
Again, arrays does not resize its self, nor does it check for bound
errors. Don't worry about whats happening in the memory right
now. Just remember that when you have a code similar to this :

data_type  dataVariableArray[ someConstantNumber ] ;

That array has elements from 0 to someConstantNumber - 1,
anything else then its invalid.

the reason that you can go beyond the array size is that in c++ it is leagal to increment the pointer past the size of the array. its a through back to c. you cant start before the array like index = -1 but if the size of the array is 2 it is legal to go the index = 2.

char temp[1];
temp[-1]  // cannot be done
temp[1]  // okay even though the data is only in temp[0]

thank you for your reply, nathan.

i just wanted to ask something similar as well.

i am attaching the file of the code(along with my txt file from whch data is read), now what i donot; understand is; that at i=8190; the value once is output and it is exactly different, once again after i output; why does it automatically change. i know that changing the size of the columns in the longitude array make it to 11 will make it fine; but i donot understand why does the array at 8190 row automatically CHANGE; itselfs data, and show something else.

airports_clean.txt

Untitled1.cpp

Attachments
BIN,Bamiyan,Afghanistan,34.800000,67.816667
BST,Bost,Afghanistan,31.550000,64.366667
CCN,Chakcharan,Afghanistan,34.533333,65.266667
DAZ,Darwaz,Afghanistan,38.466667,70.883333
FBD,Faizabad,Afghanistan,37.083333,70.500000
FAH,Farah,Afghanistan,32.366667,62.166667
GRG,Gardez,Afghanistan,33.616667,69.116667
GZI,Ghazni,Afghanistan,33.000000,68.416667
HEA,Herat,Afghanistan,34.183333,62.233333
JAA,Jalalabad,Afghanistan,34.400000,70.500000
KDH,Kandahar,Afghanistan,31.500000,65.850000
KHT,Khost,Afghanistan,33.333333,69.950000
KWH,Khwahan,Afghanistan,37.883333,70.200000
KBL,Kabul,Afghanistan,34.565833,69.212222
UND,Kunduz,Afghanistan,36.650000,68.883333
KUR,Kuran-O-Munjan,Afghanistan,38.400000,71.116667
MMZ,Maimana,Afghanistan,35.916667,64.733333
MZR,Mazar-i-sharif,Afghanistan,36.706944,67.209722
IMZ,Nimroz,Afghanistan,31.100000,61.933333
LQN,Qala Nau,Afghanistan,34.950000,63.666667
SBF,Sardeh Band,Afghanistan,33.297778,68.631667
SGA,Sheghnan,Afghanistan,38.033333,71.716667
TQN,Taluqan,Afghanistan,36.500000,69.500000
TII,Tirinkot,Afghanistan,32.628611,65.877500
URZ,Uruzgan,Afghanistan,32.900000,66.616667
ZAJ,Zaranj,Afghanistan,31.100000,61.883333
RDB,Red Dog,Alaska,68.032222,-162.899167
TIA,Tirana,Albania,41.414722,19.720556
AZR,Adrar,Algeria,27.883333,-0.283333
TMR,Tamanrasset,Algeria,37.063611,6.527500
OGX,Ouargla,Algeria,31.916667,5.400000
BUJ,Boussaada,Algeria,35.332500,4.206389
CZL,Constantine,Algeria,36.285278,6.619167
BLJ,Batna,Algeria,35.550000,6.166667
BJA,Bejaia,Algeria,36.750000,5.083333
BSK,Biskra,Algeria,34.800000,5.733333
BMW,Bordj Badji Mokhtar,Algeria,21.133333,1.066667
TID,Tiaret,Algeria,14.100000,-16.483333
ELG,El Golea,Algeria,30.571389,2.859722
ORN,Oran,Algeria,35.616667,-0.616667
GBB,Gara Djebilet,Algeria,26.150000,-7.350000
ELU,El Oued,Algeria,33.516667,6.783333
ALG,Algiers,Algeria,36.783333,3.050000
VVZ,Illizi,Algeria,26.483333,8.466667
IAM,In Amenas,Algeria,28.051667,9.642778
INZ,In Salah,Algeria,27.250000,2.516667
DJG,Djanet,Algeria,24.450000,9.500000
GJL,Jijel,Algeria,36.800000,5.883333
KDF,Kouba,Algeria,7.800000,15.650000
CBH,Bechar,Algeria,31.650000,-2.250000
AAE,Annaba,Algeria,36.833333,8.000000
MUW,Mascara,Algeria,35.750000,0.016667
MZW,Mechria,Algeria,33.550000,-0.283333
MQV,Mostaganem,Algeria,43.247778,17.785556
INF,In Guezzam,Algeria,19.566667,5.750000
GHA,Ghardaia,Algeria,32.383333,3.816667
HME,Hassi Messaoud,Algeria,31.666667,6.150000
QSF,Setif,Algeria,36.191389,5.409444
SKI,Skikda,Algeria,36.869444,6.950000
TAF,Oran,Algeria,35.716667,-0.716667
TMX,Timimoun,Algeria,29.250000,-0.250000
TIN,Tindouf,Algeria,27.674167,-8.147778
TGR,Touggourt,Algeria,33.100000,6.066667
TLM,Tlemcen,Algeria,34.878333,-1.315000
FTI,Fitiuta,American Samoa,-14.216667,-169.450000
PPG,Pago Pago,American Samoa,-14.333333,-170.716667
OFU,Ofu,American Samoa,-14.183333,-169.700000
TAV,Tau,American Samoa,-14.229167,-169.511111
ALV,Andorra La Vella,Andorra,42.500000,1.516667
LAD,Luanda,Angola,-8.850000,13.233333
AZZ,Ambriz,Angola,-7.883333,13.150000
AAZ,Ambriz,Angola,-7.862222,13.116111
ANL,Andulo,Angola,-11.500000,16.750000
CAB,Cabinda,Angola,-5.550000,12.183333
CFF,Cafunfo,Angola,-8.766667,18.000000
CNZ,Cangamba,Angola,-13.683333,19.883333
KNP,Capanda,Angola,-9.766944,15.450278
CBT,Catumbela,Angola,-12.483333,13.483333
CAV,Cazombo,Angola,-11.883333,22.933333
PGI,Chitato,Angola,-7.350000,20.950000
CTI,Cuito Cuanavale,Angola,-15.166667,19.166667
DRC,Dirico,Angola,-17.966667,20.783333
DUE,Dundo,Angola,-7.400000,20.816667
BUG,Benguela,Angola,-12.608889,13.403611
NOV,Huambo,Angola,-12.783333,15.766667
JMB,Jamba,Angola,-14.700278,16.083333
SVP,Kuito,Angola,-12.404722,16.947500
UAL,Luau,Angola,-10.783333,20.500000
SDD,Lubango,Angola,-14.916667,13.483333
LUO,Luena,Angola,-11.768056,19.897778
LBZ,Lukapa,Angola,-8.416667,20.750000
GGC,Lumbala,Angola,-14.116667,21.433333
LZM,Luzamba,Angola,-4.966667,23.450000
MEG,Malange,Angola,-9.533333,16.333333
SPP,Menongue,Angola,-14.633333,17.733333
MSZ,Namibe,Angola,-15.208611,12.160833
NDF,Ndalatandos,Angola,-13.783333,16.716667
GXG,Negage,Angola,-7.516667,15.833333
NGV,Ngiva,Angola,23.076111,34.374444
VPE,Ongiva,Angola,-17.050000,15.600000
PBN,Porto Amboim,Angola,-10.700000,13.750000
VHC,Saurimo,Angola,-9.750000,20.550000
SZA,Soyo,Angola,-6.146389,12.363889
NDD,Sumbe,Angola,-11.166667,13.866667
UGO,Uige,Angola,-7.583333,14.983333
CEO,Waco Kungo,Angola,-13.333333,14.500000
XGN,Xangongo,Angola,-16.750000,14.967222
AXA,Anguilla,Anguilla,18.204722,-63.055000
TNM,Teniente R. Marsh Martin,Antarctica,62.190833,-58.986667
BBQ,Barbuda,Antigua and Barbuda,17.600000,-61.783333
ANU,St. John's,Antigua And Barbuda,17.136667,-61.792778
REL,Trelew,Argentina,-43.210556,-65.270278
ARR,Alto Rio Senguerr,Argentina,-45.166667,-71.000000
AEP,Buenos Aires,Argentina,-34.559167,-58.415556
TUC,Tucuman,Argentina,-26.816667,-65.216667
CSZ,Coronel Suarez,Argentina,-37.466667,-61.916667
CVI,Caleta Olivia,Argentina,-46.433333,-67.516667
CNQ,Corrientes,Argentina,-27.450000,-58.766667
IRJ,La Rioja,Argentina,-29.381667,-66.795833
CPG,Carmen De Patagones,Argentina,-40.800000,-62.983333
CTC,Catamarca,Argentina,-28.448333,-65.779722
IGR,Iguazu,Argentina,-25.583333,-54.333333
CVH,Caviahue,Argentina,-37.916667,-71.013889
CRR,Ceres,Argentina,-29.866667,-61.833333
CPC,San Martin DeLos Andes,Argentina,-40.083333,-71.133333
CNT,Charata,Argentina,-27.216667,-61.216667
CLX,Clorinda,Argentina,-25.333333,-57.666667
CCT,Colonia Catriel,Argentina,-37.850000,-67.866667
OLN,Colonia Sarmiento,Argentina,-45.583333,-69.000000
BHI,Bahia Blanca,Argentina,-38.716667,-62.183333
CRD,Comodoro Rivadavia,Argentina,-45.783333,-67.466667
COC,Concordia,Argentina,-31.300000,-58.016667
UZU,Curuzu Cuatia,Argentina,-29.783333,-58.016667
CUT,Cutral,Argentina,-38.966667,-69.250000
EHL,El Bolson,Argentina,-41.966667,-71.516667
JUJ,Jujuy,Argentina,-24.233333,-65.266667
FTE,El Calafate,Argentina,-50.280278,-72.053056
EMX,El Maiten,Argentina,-42.068056,-71.155556
EPA,El Palomar,Argentina,-28.083333,-64.283333
MDZ,Mendoza,Argentina,-32.833333,-68.783333
FMA,Formosa,Argentina,-26.212778,-58.228056
PMY,Puerto Madryn,Argentina,-42.733333,-65.066667
ELO,Eldorado,Argentina,-26.400000,-54.633333
EQS,Esquel,Argentina,-42.916667,-71.150000
EZE,Buenos Aires,Argentina,-34.822222,-58.535833
ROS,Rosario,Argentina,-32.833333,-60.716667
SLA,Salta,Argentina,-24.850000,-65.483333
GPO,General Pico,Argentina,-35.700000,-63.750000
GNR,General Roca,Argentina,-39.033333,-67.583333
VGS,General Villegas,Argentina,-35.033333,-63.016667
GGS,Gobernador Gregores,Argentina,-48.766667,-70.200000
OYA,Goya,Argentina,-29.100000,-59.250000
GHU,Gualeguaychu,Argentina,-33.016667,-58.583333
IGB,Ingeniero Jacobacci,Argentina,-41.300000,-69.583333
RGL,Rio Gallegos,Argentina,-51.650000,-69.200000
BRC,San Carlos DeBariloche,Argentina,-41.133333,-71.166667
USH,Ushuaia,Argentina,-54.843333,-68.295833
JSM,Jose De San Martin,Argentina,-44.033333,-70.466667
JNI,Junin,Argentina,-34.583333,-60.966667
LCM,La Cumbre,Argentina,-31.000000,-64.533333
LPG,La Plata,Argentina,-34.916667,-57.950000
ING,Lago Argentino,Argentina,-50.333333,-72.250000
LHS,Las Heras,Argentina,-46.533333,-68.950000
LLS,Las Lomitas,Argentina,-24.700000,-60.600000
LCP,Loncopue,Argentina,-38.066667,-70.616667
LMD,Los Menucos,Argentina,-40.833333,-68.133333
LGS,Malargue,Argentina,-35.493611,-69.574167
MQD,Maquinchao,Argentina,-41.250000,-68.733333
MDQ,Mar Del Plata,Argentina,-37.934167,-57.573333
MDX,Mercedes,Argentina,-29.216944,-58.083333
MJR,Miramar,Argentina,-38.216667,-57.866667
MCS,Monte Caseros,Argentina,-30.266667,-57.633333
NEC,Necochea,Argentina,-38.666667,-58.750000
NQN,Neuquen,Argentina,-38.950000,-68.133333
OVR,Olavarria,Argentina,-35.700000,-63.000000
ORA,Oran,Argentina,-23.150000,-64.316667
COR,Cordoba,Argentina,-31.316667,-64.216667
PRA,Parana,Argentina,-31.783333,-60.483333
AOL,Paso De Los Libres,Argentina,-29.683333,-57.150000
PEH,Pehuajo,Argentina,-35.800000,-61.883333
PMQ,Perito Moreno,Argentina,-46.535000,-70.983333
PSS,Posadas,Argentina,-27.416667,-55.950000
PRQ,Pres. Roque Saenz Pena,Argentina,-26.766667,-60.266667
PUD,Puerto Deseado,Argentina,-47.716667,-65.916667
RCQ,Reconquista,Argentina,-29.183333,-59.700000
RES,Resistencia,Argentina,-27.466667,-58.983333
RCU,Rio Cuarto,Argentina,-33.100000,-64.266667
RGA,Rio Grande,Argentina,-53.783333,-67.766667
RHD,Rio Hondo,Argentina,-27.466667,-64.916667
ROY,Rio Mayo,Argentina,-45.719444,-70.251389
RYO,Rio Turbio,Argentina,-51.600000,-72.216667
SZQ,Saenz Pena,Argentina,-34.616667,-58.533333
OES,San Antonio Oeste,Argentina,-40.733333,-64.933333
UAQ,San Juan,Argentina,-31.600000,-68.550000
ULA,San Julian,Argentina,-49.300000,-67.750000
LUQ,San Luis,Argentina,-33.266667,-66.333333
AFA,San Rafael,Argentina,-34.583333,-68.400000
RZA,Santa Cruz,Argentina,-50.033333,-68.583333
SFN,Santa Fe,Argentina,-31.716667,-60.816667
RSA,Santa Rosa,Argentina,-36.616667,-64.283333
SST,Santa Teresita,Argentina,-35.266667,-59.633333
SDE,Santiago Del Estero,Argentina,-27.766667,-64.283333
SGV,Sierra Grande,Argentina,-41.500000,-65.350000
TDL,Tandil,Argentina,-37.233333,-59.216667
TTG,Tartagal,Argentina,-26.216667,-64.150000
VCF,Valcheta,Argentina,-40.700000,-66.150000
RLO,Merlo,Argentina,-32.350000,-65.033333
VDM,Viedma,Argentina,-40.850000,-63.016667
VDR,Villa Dolores,Argentina,-31.950000,-65.150000
VLG,Villa Gesell,Argentina,-38.016667,-57.583333
VME,Villa Mercedes,Argentina,-33.633333,-65.416667
APZ,Zapala,Argentina,-38.916667,-70.083333
LWN,Gyoumri,Armenia,40.716667,43.833333
EVN,Yerevan,Armenia,40.147222,44.395833
AUA,Aruba,Aruba,12.500000,-70.016667
VCD,Victoria River Downs,Australia,-16.405000,131.003333
ABG,Abingdon,Australia,-17.666667,143.200000
ADL,Adelaide,Australia,-34.945000,138.530556
XHX,Adelaide,Australia,-33.137500,136.303056
AGW,Agnew,Australia,-12.150000,142.150000
ALH,Albany,Australia,-34.950000,117.800000
ABX,Albury,Australia,-36.066667,146.950000
AXL,Alexandria,Australia,-19.116667,136.766667
ASP,Alice Springs,Australia,-23.800000,133.883333
XHW,Alice Springs,Australia,-23.566667,145.800000
A
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstring>
#define pi 3.14159265358979323846
using namespace std;

int main ()
{
ifstream fin;
fin.open("airports_clean.txt");

char airport_code[9187][4];char airport_city[9187][50];char airport_country[9187][40];
char latitude[9187][10]; char longitude[9187][10];

for (int i=0; i<=9186; i++)
{
int comma_check=0;char data_input[200];
    fin.getline(data_input,200);
    
for (int j=0; j<=200; j++)
{
if (j<=2)
        {airport_code[i][j]=data_input[j];}
        airport_code[i][3]='\0'; 

if (data_input[j]==',')
{
comma_check++;
if (comma_check==2)
{
int k=0;
while(1)
{
airport_country[i][k]=data_input[j+1+k];
if (airport_country[i][k]==',')
{
airport_country[i][k]='\0';
break;
}
k++;
}
}
if (comma_check==3)
{
int k=0;
while(1)
{
latitude[i][k]=data_input[j+1+k];
if (latitude[i][k]==',')
{
latitude[i][k]='\0';
break;
}
k++;
}
}
if (comma_check==4)
{ int k=0;
while(1)
{
longitude[i][k]=data_input[j+1+k];
if (longitude[i][k]=='\0')
{
longitude[i][k]='\0';

if(i==8190){cout<<longitude[i]<<endl;}

break;
}
k++;
}
}
}
}
}
cout<<longitude[8190]<<endl;
cin.get();
return 0;
}
This article has been dead for over six months. Start a new discussion instead.