#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
class array
{
private:
int A[20];
int ele;
public:
void input();
void display();
void bub_sort();
void insert();
void del();
};
void array :: input()
{
int i;char ch[20];
for(i=0; ch[0]!='x'; i++,ele++)
{
gets(ch);
A= atoi(ch);
}
ch[0]=0;
ele=(ele-1);
return;
}
//========================================================================
void array :: display()
{
for(int i=0; i<ele; i++)
{
cout<<"Element ("<<(i+1)<<") is -->        "<<A<<" \n\n";
}
}
//=========================================================================
void array :: bub_sort()
{
int i, j,temp;
for(i=0; i<ele; i++)
{
for (j=0; j<(ele-1); j++)
{
if (A[j+1] < A[j])
{
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
}
//=========================================================================
void array :: insert()
{
int i,x,j,temp;
for(i=0; i<ele; i++)
{
cout<<"Input the element to inserted";
cin>>x;
if(x<=A);
{
for(j=i;j<(ele+1);j++)
{
temp=A[j];
A[j]=x;
x=A[j+1];
A[j+1]=temp;
}
}
}
}
void main()
{
array B;
clrscr();
B.input();
B.bub_sort();
B.display();

getch();
}

When I execute the code.... the value of ele is set to a junk chracter, isn't the class function supposed to manipulate the value ??

Recommended Answers

All 2 Replies

ele is never initialized. You should probably add a constructor that sets the data fields to something before working with them:

class array {
private:
  int A[20];
  int ele;

public:
  array()
  {
    for (int i = 0; i < 20. ++i)
      A[i] = 0;

    ele = 0;
  }

  void input();
  void display();
  void bub_sort();
  void insert();
  void del();
};

LO silly mistake ... thanx dude.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.