help-I'm new in c++

M

memo

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);
tur=1;
i++;
}
else{
buyut(eleman,tur);
printf("lutfen degeri giriniz\n");
scanf("%d",&eleman);
tur=1;


}


}

void doubleEkle(void){
if(i<boyut){
printf("lutfen degeri giriniz\n");
scanf("%lf",&eleman);
tur=2;
i++;

}
else{
buyut(eleman,tur);
printf("lutfen degeri giriniz\n");
scanf("%lf",&eleman);
tur=2;
i++;


}



}

void charEkle(void){

if(i<boyut){
printf("string giriniz\n");
scanf("%s",&eleman);
tur=3;
i++;


}
else
{
buyut(eleman,tur);
printf("string giriniz\n");
scanf("%s",&eleman);
tur=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);
tur=1;
i++;
}
else{
buyut(eleman,tur);
printf("lutfen degeri giriniz\n");
scanf("%d",&eleman);
tur=1;


}


}
void proje::doubleEkle(void){
if(i<boyut){
printf("lutfen degeri giriniz\n");
scanf("%lf",&eleman);
tur=2;
i++;

}
else{
buyut(eleman,tur);
printf("lutfen degeri giriniz\n");
scanf("%lf",&eleman);
tur=2;
i++;


}



}
void proje::charEkle(void){
if(i<boyut){
printf("string giriniz\n");
scanf("%s",&eleman);
tur=3;
i++;


}
else
{
buyut(eleman,tur);
printf("string giriniz\n");
scanf("%s",&eleman);
tur=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){

case 1:
printf("%d\n",eleman);
system("pause");
break;
case 2:
printf("%.2f\n",eleman);
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;
}
}
 
E

edd

hi im in a trouble with c++ i made a project with c but icant convert c
++ here is the code with c

[ 8< --- massive amounts of code snipped ]

Comment out parts of the program until it compiles cleanly. When it
compiles successfully for the first time, you know that the last part
you commented out has an error as far as your C++ compiler is
concerned. Fix it.

Un-comment a little more. Repeat the fixing process as necessary.

Of course, using C-style code in a C++ program just isn't a good idea,
anyway. malloc and company are used very little indeed in C++.
std::vector<> should be used for dynamically sized arrays.

You should also look at sprinkling in some polymorphism too, perhaps,
if you're up to it, rather than littering the code with switch
statements.

Edd
 
N

Naresh Rautela

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);
tur=1;
i++;
}
else{
buyut(eleman,tur);
printf("lutfen degeri giriniz\n");
scanf("%d",&eleman);
tur=1;

}

}

void doubleEkle(void){
if(i<boyut){
printf("lutfen degeri giriniz\n");
scanf("%lf",&eleman);
tur=2;
i++;

}
else{
buyut(eleman,tur);
printf("lutfen degeri giriniz\n");
scanf("%lf",&eleman);
tur=2;
i++;

}

}

void charEkle(void){

if(i<boyut){
printf("string giriniz\n");
scanf("%s",&eleman);
tur=3;
i++;

}
else
{
buyut(eleman,tur);
printf("string giriniz\n");
scanf("%s",&eleman);
tur=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);
tur=1;
i++;
}
else{
buyut(eleman,tur);
printf("lutfen degeri giriniz\n");
scanf("%d",&eleman);
tur=1;

}

}

void proje::doubleEkle(void){
if(i<boyut){
printf("lutfen degeri giriniz\n");
scanf("%lf",&eleman);
tur=2;
i++;

}
else{
buyut(eleman,tur);
printf("lutfen degeri giriniz\n");
scanf("%lf",&eleman);
tur=2;
i++;

}

}

void proje::charEkle(void){
if(i<boyut){
printf("string giriniz\n");
scanf("%s",&eleman);
tur=3;
i++;

}
else
{
buyut(eleman,tur);
printf("string giriniz\n");
scanf("%s",&eleman);
tur=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){

case 1:
printf("%d\n",eleman);
system("pause");
break;
case 2:
printf("%.2f\n",eleman);
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top