2016-12-06 3 views
0

Also, ich fange gerade an zu programmieren. Hab meine zweite Anweisung bekommen, aber da ist etwas, was mich wirklich stört. Dieses Programm sollte notieren für Lebensmittel, okay, wenn ich merke, ich brauche Variable b nicht und entfernen Sie es. Führe es erneut aus, dann ist jumlah [i] * harga [i] immer 0. Versuche die Variable b zu ersetzen und das Programm ist wieder normal. Also, wo liegt das Problem?C++: Was macht diese Variable b?

#include <iostream> 
 
#include <iomanip> 
 
#include <windows.h> 
 
#include <ctime> 
 
BOOL gotoxy(const WORD x, const WORD y) { 
 
    COORD xy; 
 
    xy.X = x; 
 
    xy.Y = y; 
 
    return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy); 
 
} 
 
using namespace std; 
 
string nonota, namabrg; 
 
char yorno, lanjut; 
 
int jumlah[30], a, b, i; //problem 
 
float diskon, Tharga, pembayaran, kembalian, harga[30], stlhdiskon; 
 

 
int main() 
 
{ 
 
    cout<<fixed<<setprecision(0); 
 
    Tharga=0; 
 
    a=3; 
 
    do 
 
    { 
 
    cout<<"Nomor Nota   : "; 
 
    cin>>nonota; 
 

 

 
     time_t now = time(0); 
 
    cout<< "Tanggal    : "; 
 
     tm* ltm= localtime(&now); 
 
      cout<<ltm->tm_mday<<"-"; 
 
      cout<<1 + ltm->tm_mon<<"-"; 
 
      cout<<1900 + ltm->tm_year<<endl; 
 
      cout<<" --------------------------------------------------------------------------"; 
 
    //cout tabel 
 
    gotoxy(5,a); cout<<"Nama Barang"; 
 
    gotoxy(22,a); cout<<"|"; 
 
    gotoxy(25,a); cout<<"Harga Satuan"; 
 
    gotoxy(43,a); cout<<"|"; 
 
    gotoxy(45,a); cout<<"Jumlah"; 
 
    gotoxy(54,a); cout<<"|"; 
 
    gotoxy(62,a); cout<<"Total Harga"<<endl; 
 
    cout<<" --------------------------------------------------------------------------"; 
 
    //data 
 
     do{ 
 
      a=a+2; 
 
      for(i=0; i<31; i++) 
 
      gotoxy(5,a); cin>>namabrg; 
 
      gotoxy(25,a); cin>>harga[i]; 
 
      gotoxy(45,a); cin>>jumlah[i]; 
 
      gotoxy(62,a); cout<<jumlah[i]*harga[i]<<endl; 
 
      Tharga+=jumlah[i]*harga[i]; 
 
      cout<<"barang berikutnya y/n?"; 
 
      gotoxy(5,a+2); cin>>lanjut; 
 
      if(lanjut=='n' or lanjut=='N') {i=31;} 
 
      gotoxy(0,a+1); cout<<"         "; 
 
     } 
 
     while(lanjut=='y' or lanjut=='Y'); 
 
     gotoxy(0,a+2); cout<<"         "; 
 
      gotoxy(45,a+2); cout<<"Total Harga : Rp. "<<Tharga<<endl; 
 
      gotoxy(45,a+3); cout<<"Diskon  : Rp. "; 
 
      diskon=0; 
 
       if(Tharga>=1000000){diskon=0.2;} 
 
       if(Tharga>=500000 and Tharga<1000000){diskon=0.15;} 
 
       if(Tharga>=100000 and Tharga<500000) {diskon=0.1;} 
 
      cout<<diskon*Tharga<<endl; 
 
      gotoxy(45,a+4); cout<<"Harga  : Rp. "<<Tharga-(diskon*Tharga)<<endl; 
 
      gotoxy(45,a+5); cout<<"Pembayaran : Rp. "; cin>>pembayaran; 
 
      gotoxy(45,a+6); cout<<"Kembalian : Rp. "<<pembayaran-(Tharga-(diskon*Tharga))<<endl; 
 
      cout<<"buat nota lagi y/n? "; cin>>lanjut; 
 
     a=a+11; 
 
     } 
 
     while (lanjut=='y' or lanjut=='Y'); 
 
     system("pause"); 
 
     return 0; 
 
}

+0

Sie viele Fehler im Code haben. Einer Ihrer "für" Zyklen macht nichts. -> für (i = 0; i <31; i ++) hat kein {} – CyberClaw

Antwort

0

Ich sehe, dass nach for(i=0; i<31; i++) Sie harga[i=30] verwenden, aber es gibt nur harga[0], ..., harga[29] (harga[30] tatsächlich auf den Speicher verweist auf eine andere Variable gehören, das ist, warum die seltsames Verhalten mit b).

(Ehrlich gesagt, scheint es, dass es viel mehr Probleme hier, zum Beispiel mit dem Körper der for.)