Jump to content
nurul_h5

Bleh Tolong Tengokkan X?

Recommended Posts

ni saya buat program c++ pakai software turbo c++.....

#include<iostream.h>

void main()

{

float price;

char A,B,C,D,e,f,h,x,y;

void choose();

cout<<" WELCOME \n";

cout<<"To KHALIL Resort Service";

for(int g=y;g==y&x;y)

cout<<"Press a for chips\n b for jus\nC for coconut\nd for ice cream";

cin>>e;

cout<<"Enter the ammount";

cin>>h;

choose();

cout<<"Enter Y if you want to continue or x to cancel";

cin>>f;

cout<<"Here your receipt.....\n please go to counter.......\nThank You";

}

choose()

{

float price;

char A,B,C,D,h,x,y,f,e;

if( e==A )

price=2.00*h;

else if( e==B )

price=3.50*h;

else if( e==C )

price=5.00*h;

else if( e==D )

price=3.00*h;

}

ni error die n warning......saya x paham la

Compiling NONAME00.CPP:

Warning NONAME00.CPP 9: Possible use of 'y' before definition in function main()

Warning NONAME00.CPP 9: Ambiguous operators need parentheses in function main()

Warning NONAME00.CPP 9: Possible use of 'x' before definition in function main()

Warning NONAME00.CPP 18: 'D' is declared but never used in function main()

Warning NONAME00.CPP 18: 'C' is declared but never used in function main()

Warning NONAME00.CPP 18: 'B' is declared but never used in function main()

Warning NONAME00.CPP 18: 'A' is declared but never used in function main()

Warning NONAME00.CPP 18: 'price' is declared but never used in function main()

Error NONAME00.CPP 20: Type mismatch in redeclaration of 'choose()'

Warning NONAME00.CPP 23: Possible use of 'e' before definition in function choose()

Warning NONAME00.CPP 23: Possible use of 'A' before definition in function choose()

Warning NONAME00.CPP 24: Possible use of 'h' before definition in function choose()

Warning NONAME00.CPP 25: Possible use of 'B' before definition in function choose()

Warning NONAME00.CPP 27: Possible use of 'C' before definition in function choose()

Warning NONAME00.CPP 29: Possible use of 'D' before definition in function choose()

Warning NONAME00.CPP 31: 'f' is declared but never used in function choose()

Warning NONAME00.CPP 31: 'y' is declared but never used in function choose()

Warning NONAME00.CPP 31: 'x' is declared but never used in function choose()

Warning NONAME00.CPP 31: 'price' is assigned a value that is never used in function choose()

:wacko:

Edited by nurul_h5

Share this post


Link to post
Share on other sites

antara kesilapan2 yg dpt dilihat :

function void choose(); patut declare b4/luar main.

condition for anda tu x bermakna -> for(int g=y;g==y&x;y) .x tau apa syarat dikehendaki.

amount variable sepatutnya int bukan char.data bg amount sepatutnya nombor mcm 1,2,3 dan seterusnya..

ni cth yg sy betulkan.100% free error kalo compile guna VC.

#include <iostream.h>

float choose(char, int);

void main()
{
    char e, f;
    int h;
    
    cout<<"\nWELCOME \n";
    cout<<"To KHALIL Resort Service";
        
    cout<<"\nPress \nA for chips\nB for jus\nC for coconut\nD for ice cream ";
    cin>>e;
    cout<<"Enter the ammount ";
    cin>>h;

    //choose(e, h);
    cout<<"Enter Y if you want to continue or X to cancel ";
    cin>>f;

    if (f == 'Y') {
        cout<<"Here your receipt.....\nplease go to counter.......\nThank You ";
        cout<<"\nPrice is : "<<choose(e, h)<<endl;
    }    
    else 
        main();    
}


float choose(char i, int j)
{
    double price = 0;
        
    if ( i == 'A' )    price=2.00*j;

    else if ( i == 'B' )    price=3.50*j;

    else if ( i == 'C' )    price=5.00*j;

    else if ( i == 'D' )    price=3.00*j;

    return price;
}

Edited by otai_g

Share this post


Link to post
Share on other sites

saya pakai c++ turbo version 4.5......n still ada error.......1 error.....ada cara lain x????

ni error die....

Compiling NONAME01.CPP:

Error NONAME01.CPP 27: Cannot call 'main' from within the program in function main()

Share this post


Link to post
Share on other sites

buang

 else
    main();
alternatively, gantikan dengan
return 1;

dan main tu kena return integer...so main tu jadi ----> int main (){ //rest of codes}

kalau nak wat program tu akan kembali kepada awal2 main selepas setiap pembelian atau selapas pembatalan, gunakan while loop dan sebagainya

Share this post


Link to post
Share on other sites

buang
 else
    main();
alternatively, gantikan dengan
return 1;
dan main tu kena return integer...so main tu jadi ----> int main (){ //rest of codes} kalau nak wat program tu akan kembali kepada awal2 main selepas setiap pembelian atau selapas pembatalan, gunakan while loop dan sebagainya
Hampir betul, tapi pengisytiharan fungsi main dia guna
void main()
Jadi tak perlu
return 1;
samada biarkan buang semua atau pun guna "return;" shj. Sepatutnya mengikut standard C/C++ pengisytiharan fungsi 'main' mestilah dgn salah satu dari yang berikut. Tetapi semuanya memulangkan 'int' supaya OS tahu samada tamat program dengan error atau tidak. "return 0" bermaksud tamat program sempurna.
int main(void);

int main();

int main(int argc, char *argv[]);

Share this post


Link to post
Share on other sites

huhu..sebab dia guna C++, jadi disyorkan guna c0ding standard...tu yang ada ayat 'alternatively' :D..

nak tulis 'return 0' tapi tertulis 'return 1' lak...sory kerana meyebabkan kekeliruan..huhu :D..

Share this post


Link to post
Share on other sites

ok......saya wt camni....xde error....tp x dpt ape yg saye nkla......output die camni......

WELCOME

To KHALIL Resort Service

Press

A for chips

B for jus

C for coconut

D for ice cream :a

Enter the ammount 2

Here your receipt.....

please go to counter.......

Thank You

Price is : 0

program die camni plak.....

#include <iostream.h>

float choose(char, int);

void main()

{

char e, f;

int h;

cout<<"\nWELCOME \n";

cout<<"To KHALIL Resort Service";

cout<<"\nPress \nA for chips\nB for jus\nC for coconut\nD for ice cream :";

cin>>e;

cout<<"Enter the ammount ";

cin>>h;

cout<<"Here your receipt.....\nplease go to counter.......\nThank You ";

cout<<"\nPrice is : "<<choose(e, h)<<endl;

}

float choose(char i, int j)

{

double price = 0;

if ( i == 'A' ) price=2.00*j;

else if ( i == 'B' ) price=3.50*j;

else if ( i == 'C' ) price=5.00*j;

else if ( i == 'D' ) price=3.00*j;

return 0;

}

program tu rasenye leh access tp kenapa ek price die kosong.....padahal saya dh bg formula :o

Share this post


Link to post
Share on other sites

sebab ko return 0 la kot.... hehehe aku tak reti c tambah tambah ni...

mmg betul jawapan ko.

kan function float choose return 0.so logiknya kalo funtion tu terima apa nilai pun dia tetap akan memulangkan nilai 0 kat main() disebabkan ada statement 'return 0'.sepatutnya kena guna 'return price'.

nak bg senang aku rs better x payah guna function float choose.semua program buat dlm function main() jerk.maybe ni 1 jln penyelesaian yg senang dan mudah tp x mencabar.

Share this post


Link to post
Share on other sites

float choose(char i, int j)

{

double price = 0;

if ( i == 'A' ) price=2.00*j;

else if ( i == 'B' ) price=3.50*j;

else if ( i == 'C' ) price=5.00*j;

else if ( i == 'D' ) price=3.00*j;

return price; <<<<< ------ adjust ni..

}

Share this post


Link to post
Share on other sites

float choose(char i, int j)

{

double price = 0;

if ( i == 'A' ) price=2.00*j;

else if ( i == 'B' ) price=3.50*j;

else if ( i == 'C' ) price=5.00*j;

else if ( i == 'D' ) price=3.00*j;

return price; <<<<< ------ adjust ni..

}

ok.....dh try tp output still same......cadangan lain????

Share this post


Link to post
Share on other sites

yang "return price" tu da betul...

aku rasa, sbb ko compare lower case...sedangkan ko nak upper case...

bole guna "toupper" untuk tukarkan setiap char yg dimasukkan kepada upper case....cth:

#include <iostream>
#include <cctype>

using namespace std;

// kat dalam function main tu...
cin>>e;
cin.ignore();
if(isalpha(e)){
    cout<<"Enter the ammount ";
    if(cin>>h){  //cek samada input tu integer atau bukan..
        cout<<"Here your receipt.....\nplease go to counter.......\nThank You ";
        cout<<"\nPrice is : "<<choose(toupper(e), h)<<endl;  //tukau "e" kepada upper case dulu
    }else { 
        cout << "input must be integer\n";
    }
}else {
    cout << "input must be character\n";
}

Share this post


Link to post
Share on other sites

setelah berkali2 aku mencuba.........akhirnye......aku dh berjaya.........sukenye..... :wub: ..........coding aku camni........

coding:

#include <iostream.h>

class resort

{

public:

float price;

void choose();

};

void resort::choose()

{

int h;

float *s=&price;

char e;

cout<<"\nWELCOME \n";

cout<<"To KHALIL Resort Service";

cout<<"\nPress \nA for chips\nB for jus\nC for coconut\nD for ice cream :";

cin>>e;

cout<<"Enter the ammount ";

cin>>h;

switch(e)

{

case 'A':case 'a':price=2.00;

break;

case 'B':case 'b':price=3.50;

break;

case 'C':case 'c':price=5.00;

break;

case 'D':case 'd':price=3.00;

}

price=price*h;

cout<<"\nPrice is : RM"<<*s<<endl;

cout<<"Here your receipt.....\nplease go to counter.......\nThank You ";

}

void main()

{

resort obj;

obj.choose();

}

output

WELCOME

To KHALIL Resort Service

Press

A for chips

B for jus

C for coconut

D for ice cream :a

Enter the ammount 2

Price is : RM4

Here your receipt.....

please go to counter.......

Thank You

so tq 2 otai2.......cuma aku confius yg ni je.........#include <cctype> utk apenye?????slalunye guna utk coding camner?????? :wacko:

Share this post


Link to post
Share on other sites

cctype digunakan untuk mengenalpasti dan menukar single character(macam isalpa(cek samada char tu alpahbet atau bukan), toupper(tukar char tu kpd upper case), etc)...

tahniah sbb dpt buat...

cuma ada skit pandangan...apsal nak guna ni?

float *s=&price;
cout<<"\nPrice is : RM"<<*s<<endl;
choose() tu adalah member function utk class resort...so boleh access data utk member variable "float price" tu directly... x perlukan pointer lain...kalu choose() tu bkn member funtion utk class resort pon still x perlu guna pointer lain sebab "price" tu adalah public... 2)
switch(e)
{
case 'A':case 'a':price=2.00;
break;
case 'B':case 'b':price=3.50;
break;
case 'C':case 'c':price=5.00;
break;
case 'D':case 'd':price=3.00;
}
price=price*h;
cout<<"\nPrice is : RM"<<*s<<endl;
yang ni plak menyebabkan fungsi swtich tu x bermakna :ph34r:... kalau setakat nak assignkan price tu untuk didarab dgn sesuatu yang dah pasti, elok guna const je...c0de readability pon meningkat... cth: bole wat
const float CHIPS_PRICE = 2.00;
const float JUICE_PRICE = 3.50;
const float COCONUT_PRICE = 5.00;
const float ICECREAM_PRICE = 3.00;

switch(e)
{
    case 'A':case 'a':cout<<"\nPrice is : RM"<<CHIPS_PRICE*h<<endl;
                      break;
    case 'B':case 'b':cout<<"\nPrice is : RM"<<JUICE_PRICE*h<<endl;
                      break;
    case 'C':case 'c':cout<<"\nPrice is : RM"<<COCONUT_PRICE*h<<endl;
                      break;
    case 'D':case 'd':cout<<"\nPrice is : RM"<<ICECREAM_PRICE*h<<endl;
                      break;
    default: cout<<"\nInvalid input\n";
             return;
}
3) oleh sebab guna C++, #include <iostream.h> tu perlu tukar kpd #include <iostream>....iostream.h utk C... 4) matawang selalunya ada 2 decimal point...so bole guna <iomanip> utk setkan kepada 2 decimal point... cth:
#include <iomanip>

//dalam funtion choose()
cout.setf(ios::fixed); //buang scientific notation
switch(e)
{
    case 'A':case 'a':cout<<setprecision(2)<<"\nPrice is : RM"<<CHIPS_PRICE*h<<endl;
//...rest of c0des...

5) sila guna code tags bila post...susah nak baca c0ding nanti :D

Share this post


Link to post
Share on other sites

Aku convert kedalam VB C++ amacam?

hehehe..... tapi aku buat sket jer. xde masa nak coder. Sibuk dgn Assignment nih...

kodnya.......... korang kena paste kat VB C++ (Source file). dgn extension .cpp

Selamat Mencuba.........

#include<stdio.h.>
void main()
{
int Chip, coconut, Ais_C;
float price;
char A,B,C,D,e,f,h,x,y;

printf (" WELCOME \n");
printf ("To KHALIL Resort Service\n\n");
printf("********************************************************
**\n");

//Input kat sini..............................

printf ("Press A for chips\nC for jus\nC for coconut\nd for ice cream\n");
scanf ("%d\n", Chip, coconut, Ais_C);
printf ("Enter amount ?\n");


}

Share this post


Link to post
Share on other sites

sebab ingat xleh nk access, tu yg guna pointer.......psl pode tags sorry yek.......br tau camner nk guna.........yg lain tq psl bg tips

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...