0

I am right now learning about making a program using Borland, so this is my first time on use it. I got confused by the result, cause the result is not what I expected.

Below is my code:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
        char nama[25];
      char kelas[5];
      char jurusan[30];
      char universitas[30];
      char alamat[30];

      cout<<"Masukkan Nama Anda\t : ";
      gets(nama);
      cout<<endl;
      cout<<"Kelas\t\t\t : ";
      gets(kelas);
      cout<<endl;
      cout<<"Jurusan\t\t\t : ";
      gets(jurusan);
      cout<<endl;
      cout<<"Universitas\t\t : ";
      gets(universitas);
      cout<<endl;
      cout<<"Alamat\t\t\t : ";
      gets(alamat);
      cout<<endl;

      cout<<"\n\tBIODATA ANDA SEBAGAI MAHASISWA ADALAH SEBAGAI BERIKUT:"<<endl;
      cout<<"\n\nNama\t\t\t : "<<nama<<endl;
      cout<<"Kelas\t\t\t : "<<kelas<<endl;
      cout<<"Jurusan\t\t\t : "<<jurusan<<endl;
      cout<<"Universitas\t\t : "<<universitas<<endl;
      cout<<"Alamat\t\t\t : "<<alamat<<endl;
      cout<<"\n\nSilahkan tekan tombol ENTER untuk keluar dari program biodata singkat ini!";
      getch();

}

The result I got is kinda fine, but there is still one problem that I got here. Which there is one variable that don't show what the value of the user already gave, it did not show the value, it even just gave the word "u" which I don't understand from where this word was coming. I sent it with my picture, so you can see it. This is the image

I hope you would help me, and thank you very much for reading my problem.

theduck
  • 2,589
  • 13
  • 17
  • 23
aldo ls
  • 55
  • 10
  • Why you are preferring array of chars (e.g. `char kelas[5]`) to `std::string`? – TonySalimi Dec 04 '19 at 13:39
  • 1
    `#include` -- This is not a standard header. The correct header is `` – PaulMcKenzie Dec 04 '19 at 13:42
  • 3
    Borland C++ 5 came out in 1997. It predates C++ standardization. So I have to wonder, why is this question tagged `c++11` and `c++-cli`? – Eljay Dec 04 '19 at 13:42
  • @aldo Some arrays have too small size to store the entered strings. – Vlad from Moscow Dec 04 '19 at 13:43
  • @Gupta i just found it on Google code, so i don't know which one is better, and this is my first time on using c++ – aldo ls Dec 04 '19 at 13:48
  • @VladfromMoscow how can i solve it sir? i still don't get it – aldo ls Dec 04 '19 at 13:48
  • @aldo *i just found it on Google code* -- Visual Studio 2019 Community -- g++ 8.x or above using any good IDE. All these are free, better options than a 20+ year old compiler. – PaulMcKenzie Dec 04 '19 at 13:51
  • @aldo Make the sizes of the arrays greater. You are entering strings that are greater than the sizes of the arrays. – Vlad from Moscow Dec 04 '19 at 13:52
  • @VladfromMoscow no sir, the caracter of my array is 18(eighteen), and the size of the array i made was 30(thirty), – aldo ls Dec 04 '19 at 13:57
  • @PaulMcKenzie I have downloaded Visual Studio anyway sir, but my leturer wanted me to use Borland, so i have no choice for using any other compiler. Can you please sir, suggest me what should i do to fix this problem? – aldo ls Dec 04 '19 at 13:58
  • 3
    *but my lecturer wanted me to use Borland* -- So your lecturer also suggested to use `gets`, a function that from even 20 years ago, is dangerous to use? – PaulMcKenzie Dec 04 '19 at 14:09
  • the 'gets' is i am coding it sir, i just search on google and found one interesting basic program using **Borland**, so i used it. My lecturer just teaches us with Borland, and the study just happen once til we have to make a program with it – aldo ls Dec 04 '19 at 14:16
  • 1
    *i just search on google* -- Learning C++ via google is not the way to learn a language such as C++. – PaulMcKenzie Dec 04 '19 at 14:21
  • Emm, i am sorry to hear that sir. Maybe i am wrong in a way for learning this language, any suggestion from you for such a beginner like me sir? – aldo ls Dec 04 '19 at 14:23
  • Borland is so ancient it doesn't really fit current reasonable definitions of "C++"; it's more like a relic of when it was on the way to becoming the language it is. You would get more useful help if you can learn actual C++. If a course requires using Borland C++, I can only recommend dropping the class if you still can. – aschepler Dec 05 '19 at 02:22
  • The arrays are too small for the amount you are typing in – M.M Dec 05 '19 at 02:52

2 Answers2

0

All of your input arrays:

  char nama[25];
  char kelas[5];
  char jurusan[30];
  char universitas[30];
  char alamat[30];

are going to be one continuous block of chars. Your input methods will easily overflow from one array to the next causing all sorts of havoc.

Try using std::string instead and input from std::cin:

  std::string nama;
  std::string kelas;
  std::string jurusan;
  std::string universitas;
  std::string alamat;

  std::cout << "Masukkan Nama Anda\t : ";
  std::cin >> nama;
  std::cout << std::endl;
  std::cout <<"Kelas\t\t\t : ";
  std::cin >> kelas;
  std::cout << std::endl;
  std::cout << "Jurusan\t\t\t : ";
  std::cin >> jurusan;
  std::cout <<std::endl;
  std::cout << "Universitas\t\t : ";
  std::cin >> universitas;
  std::cout <<std::endl;
  std::cout << "Alamat\t\t\t : ";
  std::cin >> alamat;
  std::cout << std::endl;
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

After i tried some several suggestion to solve my own problem. Then i wonder, why there is just the variable of Jurusan which contain character that i don't know where it comes from. I tried to see it again, and then i reverse the place of another variable, also i put a comment sign //comment to make it not being seen as a code by machine in front of the Jurusan variable there is no problem anymore. Quite thinking, and then i just tried to count the sum of character in a variable which exactly below the Jurusan variable(Universitas variable), and then i also count the size of that variable(Universitas variable).

After this i just realised if the word "u" which in the variable Jurusan is the rest of the characther which comes from the variable below the Jurusan variable, which variable Universitas.

I am as the user, put the characther greater than the size of Universitas variable. And of course, that is wrong, and a mistake.

I came back to the code, and make the size of the variable Universitas is greater than before, and right now the problem is solved. And you guys can see it in my picture below if that is already succed, you also can compare it with the previous picture that i sent here in the Question section which has problem.

This is the result that i expected, and you guys can compare it with the previoes one which has mistake before

So, thank you guys for you who tried to help me. Cheers!

aldo ls
  • 55
  • 10