Script C++

Pengertian C++ : 

Preprosessor :
  • #include
  • #define 
Tipe Data :
  • char
  • int
  • Short
  • long
  • float
  • double
  • long double
Tipe Data Dasar :
  • Integer(int)
  • Float
  • Char
  • Tipe tak bertipe(void)

Script

Input Standart :                                                                           

#include<iostream.h>
#include<conio.h>

void main()
{
char nama[50];
char nim[10];

cout<<"masukan nama anda"<<endl;
cin.getline(nama,50);
cout<<"masukan nim anda"<<endl;
cin>>nim;
cout<<"nama saya : "<<nama<<endl;
cout<<"nim saya :  "<<nim<<endl;
getch();
}

Kalkulator :

#include<stdio.h>
#include<conio.h>
#include<iostream.h>

main()
{
int a,b,c=0;

printf("Masukan Nilai A = "); scanf ("%d",&a);
printf("Masukan Nilai B = "); scanf ("%d",&b);

c=a+b;
printf("Hasil Penjumlahan = %d",c);
cout<<endl;

printf("Masukan Nilai A = "); scanf ("%d",&a);
printf("Masukan Nilai B = "); scanf ("%d",&b);

c=a-b;
printf("Hasil Pengurangan = %d",c);
cout<<endl;

printf("Masukan Nilai A = "); scanf ("%d",&a);
printf("Masukan Nilai B = "); scanf ("%d",&b);

c=a*b;
printf("Hasil Perkalian = %d",c);
cout<<endl;

printf("Masukan Nilai A = "); scanf ("%d",&a);
printf("Masukan Nilai B = "); scanf ("%d",&b);

c=a/b;
printf("Hasil Pembagian = %d",c);
cout<<endl;

getch();
}


Perulangan (For) :

#include <iostream>
#include <conio>

int main()
{
int min, max;
int i, banyak, jml;
float rata;

cout<<"NAMA    :  xxx"<<endl;
cout<<"NIM    :  xx.xx.xxxx"<<endl;
cout<<endl;
cout << "Penampilan Bilangan Genap atau Ganjil" << endl;
cout << "Masukan Angka Awal    : ";
cin >> min;
cout << "Masukan Angka Akhir    : ";
cin >> max;
cout << "\nBilangan ganjil: ";

banyak=0;
jml=0;

for (i=min;i<=max;i+=1)
{
if (i % 2 == 1)
{
cout << i << "\t";
banyak+=1;
jml+=i;
}

}

rata=float(jml)/banyak;

cout << "\nJAdi Banyaknya Bilangan Ganjil adalah " << banyak;
cout << "\nDengan total jumlah Bilangan " << jml;
cout << "\nRata-ratanya adalah " << rata;

cout << "\nBilangan genap: ";

banyak=0;
jml=0;

for (i=min;i<=max;i+=1)
{
if (i % 2 == 0)
{
cout << i << "\t";
banyak+=1;
jml+=i;
}

}

rata=float(jml)/banyak;

cout << "\nJAdi Banyaknya Bilangan Genap adalah " << banyak;
cout << "\nDengan total jumlah Bilangan " << jml;
cout << "\nRata-ratanya adalah " << rata;

cout << endl;

getch();
}



Game :                                                                                                          

Script C++ "Who Wants To Be A Millionaire program.

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char sure;
    char answer;
    char name[10];
    
    cout << "Welcome to Who wants to be a millionaire!\n\n";
    cout << "Keys: \n\n";
    cout << "e = 50/50            f = phone a friend\n";
    cout << "g = ask the audience\n\n";
    cout << "Enter your name: ";
    cin >> name;
    cout << name << ", let's play Who wants to be a millionaire\n";
    
    cout << "\n\nQuestion 1:\n\n"; 
    cout << "Who wrote this little program?\n\n";
    cout << "a. Geeth    b. You\n";
    cout << "c. Jim      d. Tom\n";
    
    cout << "Please enter your answer (a., b., c., or d.)\n";
    cout << "or you could use either 50/50 (e.), phone a friend (f.)\n";
    cout << "or ask the audience (g.)\n";
    cin >> answer;
    cout << name << ",You entered " << answer;
    cout << ", are you sure (y or n)?\n";
    cin >> sure;
    
    switch (answer)
     {
      case 'a':
            
      cout << "You are correct, you won $100!\n";
      system("PAUSE");
      return 0;
            
      break;
            
      case 'b':
            
      cout << "You are wrong, please try again!\n";
      system("PAUSE");
      return 0;
            
      break;
            
      case 'c':
            
      cout << "You are wrong, please try again!\n";
      system("PAUSE");
      return 0;
            
      case 'd':
            
      cout << "You are wrong, please try again!\n";
      system("PAUSE");
      return 0;
            
      case 'e':
            
      cout << name << ", you selected 50/50!\n";
      cout << "Computer, take away two wrong answers leaving, " << name;
      cout << " with the right answer and one remaining wrong ";        
      cout << "answer\n\n";
      cout << "a. Geeth    b. You\n";
      system("PAUSE");
      return 0;
            
      default:
            
      cout << "Invalid command, please try again!\n";
      system("PAUSE");
      return 0;
        }                                                                    
 
system("PAUSE");
return 0;       
}
  • keterangan :

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>

should be replaced by
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std; //only if in a .cpp file. else use explicit std::


char name[10];

should be
string name;


             
:       case 'b':
:             
:       cout << "You are wrong, please try again!\n";
:       system("PAUSE");
:       return 0;
:             
:       break;
:             
:       case 'c':
:             
:       cout << "You are wrong, please try again!\n";
:       system("PAUSE");
:       return 0;
:             
:       case 'd':
:             
:       cout << "You are wrong, please try again!\n";
:       system("PAUSE");
:       return 0;
:             

should be
             
:       case 'b':
:       case 'c':
:       case 'd':
:             
:       cout << "You are wrong, please try again!\n";
:       system("PAUSE");
:       return 0;
:             



Tidak ada komentar:

Posting Komentar