[help] struct with switch-menu-default

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
#include <iostream>
#include <stdio>

struct mahasiswa
{
 char nama[40];
 char nim[12];
 int nilai;
};

int main()
{
char pilih;
cout<<"*********menu*********\n";
cout<<" 1. Masukkan Identitas\n";
cout<<" 2. Display Identitas\n";
cout<<" 3. Keluar\n";
cout<<"**********************\n\n";

do
{
cout<<"Masukkan pilihan: ";
cin>>pilih;
switch(pilih)
{
case '1':
{
 mahasiswa id;
 cout<<"Nama Mahasiswa  : ";
 gets(id.nama);
 cout<<"NIM             : ";
 gets(id.nim);
 cout<<"Nilai           : ";
 gets(id.nilai);
}
break;
case '2':
{
 mahasiswa id;
 cout<<"Nama Mahasiswa  : "<<id.nama<<endl;
 cout<<"NIM             : "<<id.nim<<endl;
 cout<<"Nilai           : "<<id.nilai<<endl;
}
break;
case '3':
break;
default:
cout<<"no. "<<pilih
    <<" tidak terdapat dalam pilihan\n\n";
};
}while (pilih!='3');
return 0;
}




anyone can fix it? i'm using borland c++
thanks before :D
closed account (iw0XoG1T)
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
#include <iostream>
#include <cstdio>

struct mahasiswa
{
 char nama[40];
 char nim[12];
 int nilai;
};

int main()
{
char pilih;
std::cout<<"*********menu*********\n";
std::cout<<" 1. Masukkan Identitas\n";
std::cout<<" 2. Display Identitas\n";
std::cout<<" 3. Keluar\n";
std::cout<<"**********************\n\n";

do
{
std::cout<<"Masukkan pilihan: ";
std::cin>>pilih;
switch(pilih)
{
case '1':
{
 mahasiswa id;
 std::cout<<"Nama Mahasiswa  : ";
 std::gets(id.nama);
 std::cout<<"NIM             : ";
 std::gets(id.nim);
 std::cout<<"Nilai           : ";
 std::cin >> id.nilai;
}
break;
case '2':
{
 mahasiswa id;
 std::cout<<"Nama Mahasiswa  : "<<id.nama<<std::endl;
 std::cout<<"NIM             : "<<id.nim<<std::endl;
 std::cout<<"Nilai           : "<<id.nilai<<std::endl;
}
break;
case '3':
break;
default:
std::cout<<"no. "<<pilih
    <<" tidak terdapat dalam pilihan\n\n";
};
}while (pilih!='3');
return 0;
}
still not work...
get much more mistakes T_T
Referring to your OP:

Line 2: Should be <cstdio>

Line 3: Missing using namespace std;

Line 34: You can't use gets() into an int.
Use: cin >> id.nilai;

With these 3 changes the code compiles cleanly using VS2010.
Also, chwsks's code also compiles cleanly for me.

Please post the exact text of the errors you're getting along with the exact code you're trying to compile.



borland not supported with <cstdio> and using namespace std;
it's work when "int nilai" i changed into "char nilai"
1
2
3
4
5
6
struct mahasiswa
{
 char nama[40];
 char nim[12];
 int nilai;
};


but, when i'm trying using "cin" to replace "gets()" it have an error on (41,8): Illegal structure operation...
thanks for help :D
Maybe you could benefit with using an `up to date' compiler.
At least one that conforms with the 98 standard, by instance gcc.
Topic archived. No new replies allowed.