Re: help-I'm new in c++
On Jun 3, 1:28 pm, memo <keo...@gmail.com> wrote:
hi im in a trouble with c++ i made a project with c but icant convert c
++ here is the code with c
#include<stdio.h>
#include<stdlib.h>
#include <conio.h>
static int i=0;
static int boyut=4;
static int*tur;
union myArray{
double doubleDizi;
int integer;
char charDizi[20];
};
typedef union myArray MyArray;
MyArray* eleman;
int menu(void);
int veriTipi(void);
void integerEkle(void);
void doubleEkle(void);
void charEkle(void);
void eklemeFonksiyonlari(int);
void menuIslemleri(int);
void buyut(MyArray*,int *);
void indeksOkuma(int);
void kapasite(void);
void elemanSayisi(void);
void doluluk(void);
void elemanlar(void);
void cikis(void);
int veriTipi(void)
{
int cevap;
system("cls");
printf("1\tinteger ekle\n2\tdouble ekle\n3\tstring ekle(maximum 20
uzunlugunda)\n4\tcancel\n");
scanf("%d",&cevap);
return cevap;
}
int menu(void){
int cevap;
printf("1\tyeni eleman ekle\n2\tgirilen indeksteki elemani gor
\n3\tkac eleman oldugunu gor\n4\tdizinin kapasitesi gor\n"
"5\tdizinin doluluk oranini gor\n6\tdizinin elemanlarini gor
\n7\tcikis\n");
scanf("%d",&cevap);
return cevap;
}
void eklemeFonksiyonlari(int number){
system("cls");
switch(number){
case 1:
integerEkle();
break;
case 2:
doubleEkle();
break;
case 3:
charEkle();
break;
case 4:
menu();
break;
}
}
void menuIslemleri(int secim){
switch(secim){
case 1:
int deger;
deger=veriTipi();
eklemeFonksiyonlari(deger);
break;
case 2:
int b;
printf("lutfen indeksi giriniz\n");
scanf("%d",&b);
indeksOkuma(b);
break;
case 3:
elemanSayisi();
break;
case 4:
kapasite();
break;
case 5:
doluluk();
break;
case 6:
elemanlar();
break;
case 7:
cikis();
break;
}
}
void integerEkle(void){
if(i<boyut){
printf("lutfen degeri giriniz\n");
scanf("%d",&eleman[i]);
tur[i]=1;
i++;
}
else{
buyut(eleman,tur);
printf("lutfen degeri giriniz\n");
scanf("%d",&eleman[i]);
tur[i]=1;
}
}
void doubleEkle(void){
if(i<boyut){
printf("lutfen degeri giriniz\n");
scanf("%lf",&eleman[i]);
tur[i]=2;
i++;
}
else{
buyut(eleman,tur);
printf("lutfen degeri giriniz\n");
scanf("%lf",&eleman[i]);
tur[i]=2;
i++;
}
}
void charEkle(void){
if(i<boyut){
printf("string giriniz\n");
scanf("%s",&eleman[i]);
tur[i]=3;
i++;
}
else
{
buyut(eleman,tur);
printf("string giriniz\n");
scanf("%s",&eleman[i]);
tur[i]=3;
i++;
}
}
void buyut(MyArray*p,int*a){
boyut=(int)(boyut*3/2);
p=(MyArray*)realloc(p,boyut);
a=(int *)realloc(a,boyut);
}
void indeksOkuma(int c){
int j=0;
switch(tur[c]){
case 1:
printf("%d\n",eleman[c]);
system("pause");
break;
case 2:
printf("%.2f\n",eleman[c]);
system("pause");
break;
case 3:
while((eleman[i-1].charDizi[j])!='\0')
printf("%c",eleman[i-1].charDizi[j++]);
printf("\n");
system("pause");
break;
}
}
void kapasite(void){
printf("dizimizin kapasitesi=%d\n",boyut);
system("pause");}
void elemanSayisi(void){
printf("dizimizin eleman sayisi=%d\n",i);
system("pause");}
void doluluk(void){
double k;
k=(double)((((double)i)/((double)boyut))*((double)100));
printf("dizimizin doluluk orani = %c",37);
printf(" %.2f\n",k);
system("pause");
}
void elemanlar(void){
int k=0;
do{
indeksOkuma(k);
k++;
}while((eleman[k].charDizi[0])!='\0' || (eleman[k].doubleDizi)!='\0'
||(eleman[k].integer)!='\0' );
}
void cikis(void){
exit(0);
}
int main(){
int secim;
eleman=(MyArray*)malloc(sizeof(MyArray)*boyut);
tur=(int *)malloc(sizeof(int)*boyut);
do{
system("CLS");
secim=menu();
menuIslemleri(secim);
}while(secim!=7);
return 0;
}
it works but also c++ isnt.:((
its here icant solve the problem
#include<stdio.h>
#include<stdlib.h>
#include <conio.h>
static int i=0;
static int boyut=4;
static int*tur;
class proje
{
private:
union myArray{
double doubleDizi;
int integer;
char charDizi[20];
};
typedef union myArray MyArray;
MyArray* eleman;
public:
int menu(void);
int veriTipi(void);
void integerEkle(void);
void doubleEkle(void);
void charEkle(void);
void eklemeFonksiyonlari(int);
void menuIslemleri(int);
void buyut(MyArray*,int *);
void indeksOkuma(int);
void kapasite(void);
void elemanSayisi(void);
void doluluk(void);
void elemanlar(void);
void cikis(void);
};
int proje::veriTipi(void){
int cevap;
system("cls");
printf("1\tinteger ekle\n2\tdouble ekle\n3\tstring ekle(maximum 20
uzunlugunda)\n4\tcancel\n");
scanf("%d",&cevap);
return cevap;}
int proje::menu(void){
int cevap;
printf("1\tyeni eleman ekle\n2\tgirilen indeksteki elemani gor
\n3\tkac eleman oldugunu gor\n4\tdizinin kapasitesi gor\n"
"5\tdizinin doluluk oranini gor\n6\tdizinin elemanlarini gor
\n7\tcikis\n");
scanf("%d",&cevap);
return cevap;}
void proje::eklemeFonksiyonlari(int number){
system("cls");
switch(number){
case 1:
integerEkle();
break;
case 2:
doubleEkle();
break;
case 3:
charEkle();
break;
case 4:
menu();
break;
}
}
void proje::menuIslemleri(int secim){
switch(secim){
case 1:
int deger;
deger=veriTipi();
eklemeFonksiyonlari(deger);
break;
case 2:
int b;
printf("lutfen indeksi giriniz\n");
scanf("%d",&b);
indeksOkuma(b);
break;
case 3:
elemanSayisi();
break;
case 4:
kapasite();
break;
case 5:
doluluk();
break;
case 6:
elemanlar();
break;
case 7:
cikis();
break;
}
}
void proje::integerEkle(void){
if(i<boyut){
printf("lutfen degeri giriniz\n");
scanf("%d",&eleman[i]);
tur[i]=1;
i++;
}
else{
buyut(eleman,tur);
printf("lutfen degeri giriniz\n");
scanf("%d",&eleman[i]);
tur[i]=1;
}
}
void proje::doubleEkle(void){
if(i<boyut){
printf("lutfen degeri giriniz\n");
scanf("%lf",&eleman[i]);
tur[i]=2;
i++;
}
else{
buyut(eleman,tur);
printf("lutfen degeri giriniz\n");
scanf("%lf",&eleman[i]);
tur[i]=2;
i++;
}
}
void proje::charEkle(void){
if(i<boyut){
printf("string giriniz\n");
scanf("%s",&eleman[i]);
tur[i]=3;
i++;
}
else
{
buyut(eleman,tur);
printf("string giriniz\n");
scanf("%s",&eleman[i]);
tur[i]=3;
i++;
}
}
void proje::buyut(MyArray*,int*){
boyut=(int)(boyut*3/2);
MyArray*p=(MyArray*)realloc(p,boyut);
int*a=(int *)realloc(a,boyut);}
void proje::indeksOkuma(int){
int j=0;
switch(tur[i]){
case 1:
printf("%d\n",eleman[i]);
system("pause");
break;
case 2:
printf("%.2f\n",eleman[i]);
system("pause");
break;
case 3:
while((eleman[i-1].charDizi[j])!='\0')
printf("%c",eleman[i-1].charDizi[j++]);
printf("\n");
system("pause");
break;}
void proje::kapasite(void){
printf("dizimizin kapasitesi=%d\n",boyut);
system("pause");}
void proje::elemanSayisi(void){
printf("dizimizin eleman sayisi=%d\n",i);
system("pause");}
void proje::doluluk(){
double k;
k=(double)((((double)i)/((double)boyut))*((double)100));
printf("dizimizin doluluk orani = %c",37);
printf(" %.2f\n",k);
system("pause");}
void proje::elemanlar(void){
int k=0;
do{
indeksOkuma(k);
k++;
}while((eleman[k].charDizi[0])!='\0' || (eleman[k].doubleDizi)!='\0'
||(eleman[k].integer)!='\0' );
};
void proje::cikis(void){
exit(0);
};
int main(){
int secim;
(MyArray*)malloc(sizeof(MyArray)*boyut)=eleman;
tur=(int *)malloc(sizeof(int)*boyut);
do{
system("CLS");
secim=menu();
menuIslemleri(int secim);
}while(secim!=7);
return 0;
}
}- Hide quoted text -
- Show quoted text -
There seems to be a couple of things wrong with this program
1. There seems to be a few misplaced brackets.
2. In main you are trying to access MyArray directly. Its a private
datamember of clas proje. So you need to create a method within proje
to return you this structure.
3. Menu is a member function of proje, you cannot access it directly.
You need to do something like
proje p;
p.menu();
4. The do loop should look like
do
{
system("CLS");
secim=p.menu();
p.menuIslemleri(secim);
}while(secim!=7);
5. use cin and cout instead of scanf and printf
Once you are done with these changes maybe we can help you further.