PLZ HELP

Can someone help me with this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>

pembatas()
{
cout<<"=========================================="<<endl;
}
main ()
{
clrscr();
char kode [23],jenis [5][10];
int i,banyaksekali[5],banyak_beli[5],banyak_jenis,harga[5],jumhar[100],jumbay,kembali,byr;
/*Display*/

cout<<"\tPROGRAM APLIKASI ICE CREAM CILOLE"<<endl;
pembatas();
cout<<"Kode Ice   Nama Ice Cream   Harga  "<<endl;
pembatas();
cout<<"    SM      Sweet Matcha    Rp.8000 "<<endl;
cout<<"    FY     Frozen Yoghurt   Rp.8500"<<endl;
cout<<"    MI       Mochi Ice      Rp.6500"<<endl;
pembatas();
/*Inputting*/
awal:
cout<<"Banyak Jenis : ";cin>>banyak_jenis;
if (banyak_jenis <=3)
{
   for (i=1;i<=banyak_jenis;i++)
   {
     cout<<"Jenis ke-"<<i<<endl;
     cout<<"Kode Ice : ";cin>>kode[i];
     if (kode== "SM"||kode=="sm")
     {
      strcpy(jenis[i],"SweetMatcha");
      harga[i]=8000;
     }

     else if (kode== "FY"||kode== "fy")
     {
      strcpy(jenis[i],"FrozenYoghurt");
      harga[i]=8500;
     }
     else if (kode== "MI"||kode== "mi")
     {
      strcpy(jenis[i],"MochiIce");
      harga[i]=6500;
     }
     else
     {
     strcpy(jenis[i]," ");
     harga[i]=0;
     }
     cout<<"Banyak Beli : ";
     cin>>banyak_beli[i];
     cout<<endl;


   }
clrscr();
cout<<"========================================="<<endl;
cout<<"No Jenis      Harga   Banyak  Jumlah "<<endl;
cout<<"   Ice Cream  Satuan  Beli    Harga"<<endl;
cout<<"========================================="<<endl;
for(i=1;i<=banyak_jenis;i++)
{
 cout<<setiosflags(ios::left)<<setw(6)<<1;
 cout<<setiosflags(ios::left)<<setw(8)<<jenis[i];
 cout<<setiosflags(ios::left)<<setw(10)<<harga[i];
 cout<<setiosflags(ios::left)<<setw(7)<<banyak_beli[i];
 jumhar[i]=harga[i]*banyaksekali[i];
 cout<<setiosflags(ios::right)<<setw(5)<<jumhar[i];
 cout<<endl;
}
pembatas();
jumbay=jumbay+jumhar[i];
cout<<"\t Jumlah Bayar : "<<jumbay<<endl;

cout<<"\t Uang Bayar: "<<endl;
cin>>byr;
   kembali=byr-jumbay;
cout<<"\t kembalian : "<<kembali;
}

else
{
 cout<<"MAAF HIDUP ANDA TIDAK BERGUNA"<<endl<<endl;
 goto awal;
}

/*output*/





/*SELESAI*/
getch();
}
What is the issue? We cant help you unless you explain what the problem is.
Last edited on
Hello MYGaid,

As Ch1156 has pointed out it is hard to help you when one does not know what the problem is.

Some things I do see:

The header files <iostream.h> and <iomanip.h> are 20+ years out of date. I believe that it was the C++ 1998 standards the did away with the ".h" extension for C++ header files. For this reason to start with I can not compile the program or run it until i made some changes.

You start with the pembatas() function. Leaving off the return type, even if it is "void" became a problem. I had to change this to void pembatas() to get the error to go away. The same applies to main(). This may work with your very old compiler, but not today with the newer compilers. Along the way, history is not my best subject, the standards said the "main" should return an "int". When I updated your code I had to change this to int main() to eliminate the error.

Inside "main" you define your variables as:
1
2
3
char kode[23], jenis[5][10];
int i, banyaksekali[5], banyak_beli[5], banyak_jenis;
int harga[5], jumhar[100], jumbay, kembali, byr;

This leaves you variables uninitialized and containing garbage that is useless. For most of this it was not a problem except for "jumbay" which you tried to use in jumbay = jumbay + jumhar[i];. Garbage + something is still garbage and not what you want. On my computer an uninitialized "int" mostly has the value of "-858993460". Occasionally this will change. Adding something to this number would take a long time to become useful.

When defining variables and using C++11 standards this becomes much easier.
1
2
3
char kode[23]{}, jenis[5][10]{};
int i, banyaksekali[5]{}, banyak_beli[5]{}, banyak_jenis{};
int harga[5]{}, jumhar[100]{}, jumbay{}, kembali{}, byr{};

From C++11 on the {}s or (uniform initializer) makes it very to initialize variables. I along with others feel it is a good idea to initialize your variables when defined. If for no other reason to know that they do not contain a garbage value.

Try not to use "goto" statements. They are bad form and can make a mess of your code because they are unconditional jumps. This can be replaced with a do/while or while loop.

I noticed that all your for loops start at one (1). C and C++ along with other languages are zero based meaning that arrays start at element zero and go to defined size - 1. An example: int kode[23] is accessed as kode[0] to kode[22] for a 23 element array.

Also by starting your for loops at one (1) you are leaving out element (0) zero of the array. This may not be a problem, but could be, and with the 2D array you are missing row (0) zero. This is just wasted space.

There are some occasions when skipping element (0) zero is useful like:
std::string monthNames[13]{"", "January", .... This way when (1) one is entered and used as a subscript to the array it will return "January" for (1) one and "December" for 12. This technique can also be used for numeric arrays where the number entered, either from the keyboard or a file, can be used as the subscript.

In your code:
1
2
3
std::cout << "Jenis ke-" << i << '\n'; // <--- Type to
std::cout << "Kode Ice : ";  // <--- Ice Code
std::cin >> kode[i];

The translations may not be 100% accurate.

on line 3 you are trying to enter a string into a single character array and only one element of that array. This doe not work. Entering "SM" only stores the "S" and leaves anything left in the input buffer for the next "cin".

And you are doing something like this in more than one place.

Then there is the if statement: if (kode == "SM" || kode == "sm"). This will not work. Had you included the "<string>" header file and defined "kobe" as a "std::string" the overloaded "==" of the string class would work. But since "kobe" is a character array you would need to use "strcmp()" to compare the two. And even this will not work because the first element od "kobe" is garbage and what you want starts at element (1). "strcmp()" will start with element (0) zero of "kobe" and try to compare that to the string "SM" which will return something that is not considered "true". So when you look at it you are comparing element[0] garbage and element[1] to "SM" which will never work. Even initializing the array as I did earlier you would be starting with element[0] being "\0" which means you are at the end and there is nothing more to check.

When I changed the if statement to if (strcmp(kode, "SM") || strcmp(kode, "sm")) it worked as it should. This does exclude the possibilities of "Sm" and "sM", but does cover the most likely possibilities.

Your last for loop can be written as:
1
2
3
4
5
6
7
8
9
10
11
for (i = 1; i <= banyak_jenis; i++)
{
	std::cout << std::left << std::setw(6) << 1
		<< std::setw(8) << jenis[i]
		<< std::setw(10) << harga[i]
		<< std::setw(7) << banyak_beli[i];

	jumhar[i] = harga[i] * banyaksekali[i];

	std::cout << std::right << std::setw(5) << jumhar[i] << std::endl;
}

Although the spacing needs some work.
Using C++11 and "<iomanip>" you only need one "std::left" and it will affect every "setw" until it is changed. As it is in line 10.

"std::left" is used more for text output where "std::right" is used more for numbers so they align properly.

At this point I still have no idea what your problemis, but this might help.

Hope that helps,

Andy
Topic archived. No new replies allowed.