Skip to content
View in the app

A better way to browse. Learn more.

Komuniti @PuTeRA

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Tentang soalan C++

Featured Replies

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...
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";
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::
  • Author
HUhu...error la...maaf la...x pandai nak fix sendiri...ada satu error...dia kata too many intialize..
Apa eh??
  • 3 months later...
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:
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..
#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..
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]
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.. <_<
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 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.. ;)
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 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.. ;)
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 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
[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?
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
setahu aku mmg C++ ada retun int untuk main() function.
hamster91 tersalah tulis kot.
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
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++)
[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]

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...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.