Jump to content
Sign in to follow this  
azri90_1

Tentang soalan C++

Recommended Posts

Salam...nie baru nak jinak2 dlm dunai aturcara nie...ok jadi soalannya...
Libray aku ialah iostream..

Macam mane nak tukar int kepada char??:Contohnya ada nombor 1-12 utk mewakili bulan..tetapi output dia mestila dalam huruf..
Salah satu cara ialah mengunakan switch.

Code:

int month;
cout<<"Please input current month:";
   
cin>>month;
   
                switch(month){
      case 1:cout<<"Month=January"<<endl;
         break;
      
      case 2:cout<<"Month=Febuary"<<endl;
         break;
      
      case 3:cout<<"Month=March"<<endl;
         break;
      
      case 4:cout<<"Month=April"<<endl;
         break;
      
      case 5:cout<<"Month=May"<<endl;
         break;
      
      case 6:cout<<"Month=Jun"<<endl;
         break;
      
      case 7:cout<<"Month=July"<<endl;
         break;
      
      case 8:cout<<"Month=August"<<endl;
         break;
      
      case 9:cout<<"Month=September"<<endl;
         break;
      
      case 10:cout<<"Month=October"<<endl;
         break;
      
      case 11:cout<<"Month=November"<<endl;
         break;
      
      case 12:cout<<"Month=December"<<endl;
         break;
      default:cout<<"ERROR"<<endl;
         break;
}


contoh la kalau output mcm nie,mcm mane nak buat??

Code:
int month;
cout<<"Please input current month"<<endl;
cin>>month
//input adalah angka
cout<<"This month is: "<<month<<;
//output ini mestilah ditukar kepada huruf


Ada caranye x??
Itu saje soalan saye...

Share this post


Link to post
Share on other sites
Sorry aku dah lama tak program.
Jadi aku rasa macam ni boleh jugak kot:

Code:
int month;
cout<<"Please input current month"<<endl;
cin>>month;
//input adalah angka
cout<<"This month is:";

if (month == 1) cout<<"January";
else if (month == 2) cout<<"February";
else if (month == 3) cout..............;
else if (month......................;
else cout<<"Error";

Share this post


Link to post
Share on other sites
ko try gune array..

Code:

char* bulan[12] = {"Error","January","February","March","April","May","June","July","August","September","October","November","December"};
int month;

cout<<"Please enter the month";
cin>>month;

cout<<"Month : "<<bulan[month]<<;


*aku xsure syntax utk cout>> ni btol ke x..agak2 je ni..kalo salah..harap maaf ::icon_rolleyes::

Share this post


Link to post
Share on other sites
Hai..sy new mber kat cny..sy kne wat esemen tok c++..sy da try wat tp xpat output y sepatutnye..sape2 kat cni y terer wat coding c++,,tlg la sy..buntu pale sy nyh..da la kene submit rabu ny.. soklan dy ceny:

the maju car rental company charges $0.25/mile if the total mileage does not exceeds 100. if the total mileage exceeds 100, the company charges $0.25/mile for the first 100 mile, then it charges $0.15 for any additional mileage over 100. write the program so that of the clerk enters the numbers of miles, the program would display the total price owed.

- ny gune if-else statement
-nak tyer..gune looping jgk x wat progrm ny sbb skg sy da msk bab looping.
- sy gune header file <iostream.h>

tmksh byk2 sape y tlg..hehe :lol:

Share this post


Link to post
Share on other sites
boleh tak post dulu coding yang dah buat? and then baru kita diskusi dekat mana silap and so on.. :)

EDIT:
kalau ikut soalan tu takda pula kata kena ada looping.. dia nak 1 saja input dari user iaitu [b]Numbers of miles[/b].. and then display total price based on input..

Share this post


Link to post
Share on other sites
#include <iostream.h>
main ()

{
double mile, total_price ;

cout<< "please the number of miles : "<<endl ;
cin >> mile ;

if (mile <= 100 )
total_price = mile * 0.25
cout<< endl ;

else if (mile =>100 )
total_price = (mile * 0.15)+25
cout<< endl ;
else
cout<< "the mile which you enter does not exist! "<<endl ;

return 0 ;


}



ny sy men try2 je wat..tlg la betoikan coding sy ny..thx..

Share this post


Link to post
Share on other sites
Aku dah lama tak program dalam C++.. so maybe ada salah dalam nie.. tunggu forumer lain beri respond pulak.. :)

anyway boleh try coding bawah ni.. aku takda compiler nak compile.. insyaAllah betul rasa..ada pape nanti post lagi soalan dekat sini..good luck.. ;)

[code]
#include <iostream.h>
void main ()

{
double mile, total_price ;

cout<< "Please enter the number of miles : "<<endl ;
cin >> mile ;

if (mile <= 100 ){
total_price = mile * 0.25;
}
else if(mile > 100 ){
total_price = (mile * 0.25) + ((mile-100) * 0.15);
}
else{
cout<< "Invalid input!"<<endl;
}

cout<<"The total price is "<<total_price<<endl;


}
[/code]

Share this post


Link to post
Share on other sites
thanx ahakzs28 cz cube tlg..sy da run coding awk 2 tp jumlah dy xbpe tepat kalo kite kire gune clculator..2 je mslahny..awiny sy try tye lect..
duhai, da le ad kuis c++ lak ari ny..hesh.. <_<

Share this post


Link to post
Share on other sites
total_price = [b](mile * 0.25)[/b] + ((mile-100) * 0.15); << salah kat sini
sepatutnya fixkan 100 * 0.25 ataupun fixkan kepada 25

total_price = 25 + ((mile-100) * 0.15);
or
total_price = (100 * 0.25) + ((mile-100) * 0.15);

Share this post


Link to post
Share on other sites
[quote name='MatchMaker' date='23 March 2010 - 02:58 PM' timestamp='1269327481' post='1022612']
total_price = [b](mile * 0.25)[/b] + ((mile-100) * 0.15); << salah kat sini
sepatutnya fixkan 100 * 0.25 ataupun fixkan kepada 25

total_price = 25 + ((mile-100) * 0.15);
or
total_price = (100 * 0.25) + ((mile-100) * 0.15);
[/quote]

hoho.. terima kasih kerana betulkan.. tak perasan pulak kat formula tu.. ^_^

anyway good luck.. ;)

Share this post


Link to post
Share on other sites
[quote name='hamster91' date='23 March 2010 - 08:41 PM' timestamp='1269348092' post='1022653']
sy da dpt da coding y btoi..cume edit formula je..

else if(mile > 100 ){
total_price = 100*0.25 + (mile-100)*0.15;

2 saje..hehe..bebaloi jgk join forum ny.. :D
[/quote]

yap.. sorry for the mistakes.. :blush: bro MatchMaker pun dah betulkan kat atas tu..

anyway memang berbaloi join forum..especially Putera.. selamat berforum di Putera.. ;)

Share this post


Link to post
Share on other sites
eyh guys...korang terer wat looping kan..meh cny kite share ilmu same2 tlg mber ak solve soklan esemen dy..gune c++ jgk.. soklan 2 nak output y camni :

Number of value : 1
Number of value : 2
Number of value : 3
stop looping, Number of value : 4

coding y dye wat nanti ak soh dy paste kat FORUM NI..

toi2 2 ahaksz28..nk jd regular forumer kat putera ny la..huhu ..ase besh lak.. :wub:

Share this post


Link to post
Share on other sites
[quote name='hamster91' date='23 March 2010 - 09:30 PM' timestamp='1269351041' post='1022660']
eyh guys...korang terer wat looping kan..meh cny kite share ilmu same2 tlg mber ak solve soklan esemen dy..gune c++ jgk.. soklan 2 nak output y camni :

Number of value : 1
Number of value : 2
Number of value : 3
stop looping, Number of value : 4

coding y dye wat nanti ak soh dy paste kat FORUM NI..

toi2 2 ahaksz28..nk jd regular forumer kat putera ny la..huhu ..ase besh lak.. :wub:
[/quote]

eyh,mbe sy dpt da jwwpn dy..hehe

#include <iostream.h>

main()
{
int i=1;

do
{

cout << "Number of value: " << i<<endl;
i = i + 1;
}
while (i<=3);
cout<<"Stop looping,Number of value:4"<<endl;
return 0;


}


ny coding y dy da bejaye wat..n selangkah ke arah mnjdi programmer y superb..hehe

Share this post


Link to post
Share on other sites
[quote name='hamster91' date='24 March 2010 - 02:14 AM' timestamp='1269368068' post='1022698']
eyh,mbe sy dpt da jwwpn dy..hehe

#include <iostream.h>

main()
{
int i=1;

do
{

cout << "Number of value: " << i<<endl;
i = i + 1;
}
while (i<=3);
cout<<"Stop looping,Number of value:4"<<endl;
return 0;


}


ny coding y dy da bejaye wat..n selangkah ke arah mnjdi programmer y superb..hehe
[/quote]

good.. tapi dekat sini

[code]while (i<=3);
cout<<"Stop looping,Number of value:4"<<endl;
return 0;[/code]

[b]Number of value:4[/b] tu should be [b]Number of value:<<i<<endl;[/b] kan

one more thing memang kena ada [b]return 0[/b] ke?

Share this post


Link to post
Share on other sites
biasanya aku akan elak dari guna <= or >= guna < and > lagi baik.
sebabnya array/list start with 0, so untuk kurangkan kesilapan, counter bolehlah start ngan 0 (int i = 0;)
int main() mmg patut return something, biasanya return 0

Share this post


Link to post
Share on other sites
tp lect ak kate kalo gune main() last2 kene wat return 0;

kalo void main() lak xyah wat return 0;

wat int main() kene ad return 0;

weyh kengkawan..ak ad cte baru! huhu..td ak bru belaja topik array..dapat lab sheet..agak penin la jgk ble cube nk phm coding 2..ni antare coding y memeningkan ak :

#include <iostream.h>
int main ()

{

int J, score[5], max ;
cout <<"enter 5 scores :\n" ;
cin >>score[0] ;
max = score[0] ;

for (J=1 ; J<5 ; J++)
{
cin>>score[J] ;

if (score[J] > max )
max = score[J] ;
}

cout<< "\n\nThe highest score is"<<max<<endl ;

return 0;

}

cube korang explen kat ak tntang coding ny n 'J' 2 amende?utk ape? sori la byk tye..tp ak toi2 nk belaja...bru bjinak2 lam dunia kaye ny..huk2

Share this post


Link to post
Share on other sites
biasanya orang panggil ni for loop.
digunakan untuk baca array.

j tu sebagai counter.
dalam code ni for loop tu akan buat ulangan sebanyak 4 kali.
score[1] //j = 1
score[2] //j = 2 (bila dah j++)
score[3] //j = 3 (bila dah j++)
score[4] //j = 4 (bila dah j++)

Share this post


Link to post
Share on other sites
[quote name='hamster91' date='24 March 2010 - 06:25 PM' timestamp='1269426336' post='1022835']
erm..tp bile run program dy loop sbanyak 5 kali..cane 2..kalo lam RAM mcm mne kdudukan element tu?
[/quote]

sebenarnya dia loop 4 kali saja tu.. yang dalam nie saja looping..

[code]for (J=1 ; J<5 ; J++)
{
cin>>score[J] ;

if (score[J] > max )
max = score[J] ;
}[/code]

yang nampak lima tu sebab before for loop ni ada coding ni

[code]cout <<"enter 5 scores :\n" ;
cin >>score[0] ;
max = score[0] ;[/code]

kedudukan element ek.. aku tak berapa nak paham..aky try explain tapi nanti tunggu advice bro MatchMaker.. :P

andai kata input kita [b]1 , 3 , 2 , 5 , 4[/b]

element dalam array score tu jadi macam ni

[code]score[] = {1,3,2,5,4}; 1 = index 0
3 = index 1
2 = index 2
5 = index 3
4 = index 4[/code]

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...