这个变量是什么b?

时间:2016-12-06 16:53:32

标签: c++ variables

所以,我刚刚开始编程。得到了我的第二个任务,但有一些事情真的困扰我。 这个程序应该为杂货做笔记,当我意识到我不需要变量b并将其删除时,它就可以了。再次运行,然后jumlah[i]*harga[i]始终为0.

尝试恢复变量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;
}

1 个答案:

答案 0 :(得分:1)

我看到for(i=0; i<31; i++)使用harga[i=30]之后,但只有harga[0],...,harga[29]harga[30]实际指向属于某个其他变量的内存,这就是b)的奇怪行为。

(老实说,似乎这里存在更多问题,例如,for的正文。)