2

The program send me -858993460 value on a[ch] an b[nech] why ?? i cant understand that ... if i use cout<<m[i] on place of cout<<a[ch]<<endl; it works fine ... shows me the even and odds but when i try to send them to another array the value goes wrong ( I need those another two arrays a[ch] and b[nech])

#include <iostream>
#include <conio.h>
#include <string>
#include <cstdlib>
#include <stdlib.h>
using namespace std;
int main()

 {
int i,n;
int m[3], a[3]={0}, b[3]={0};
int ch=0, nech=0;
for(i=0; i<3; ++i)
{
    cout<<i+1<<". Studenski fark. nomer: ";
    do
    {
        cin>>m[i];
    }
    while(!(m[i]>=1e7 && m[i]<1e8));
    if(m[i]%2==0)
    {
        m[i]==a[ch]; ch++;
        //cout<<m[i]<<endl; "without the down 'fors' it works"
    }
    else 
    {
        m[i]==b[nech]; nech++;
        //cout<<m[i]<<endl; "without the down 'fors' it works"
    }
}
cout<<"\nVsichki fakultetni nomera: "<<endl;
for(i=0; i<3; ++i)
cout<<m[i]<<"\t";
cout<<"Chetni fark nomera: \n";
if(ch!=0)
    for(i=0;i<ch;i++)
        cout<<a[ch]<<"\t"; //"with {0} on 'a' I receive 0 as result"
else 
    cout<<"nqma chetni fak nomera";
if(nech!=0)
    for(i=0;i<nech;i++)
        cout<<b[nech]<<"\t"; //"same here 0 as result with {b}"
else
    cout<<"Nqma nechetni fak nomera";

getch();
 }
casperOne
  • 73,706
  • 19
  • 184
  • 253

3 Answers3

3

You need to initialize your arrays

int m[3] = {0}
int a[3] = {0};
int b[3] = {0};

Without initializing the array, the value is random value from stack.

billz
  • 44,644
  • 9
  • 83
  • 100
1

m[i]=a[ch] is undefined behavior because a is an array of uninitialized ints.

You're never reading into a. What would you expect the behavior to be?

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

You're not initialising the elements of a and b anywhere, so they have pretty much random initial value (e.g. the -858993460 you're getting).

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455