The variabel in my program is not giving the value user already gave

102 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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!

6
On

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;