What's wrong with this coding?
Guys, do you know what's wrong with my coding??? it can't execute --" please help me find and fix the error
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<string.h>
using namespace std;
//Struct declaration
struct TItem {
int nim;
char name[100];
char jrs[100];
};
TItem item[1000]; //Array of struct
int item_count = 0;
//Function declaration
int search(int _nim);
void add(int _nim,char _name[],char _jrs[]);
void del(int _nim);
void menu_tambah();
void menu_cari();
void menu_lihat();
int main()
{
int choice = 0;
do
{
cout<<"=== ITEM MANAGEMENT ==="<<endl;
cout<<"1. tambah mahasiswa" <<endl;
cout<<"2. cari mahasiswa"<<endl;
cout<<"3. lihat data mahasiswa"<<endl;
cout<<"4. Exit"<<endl;
do
{
cout<<"Input ?";
cin>>choice;
}
while ((choice < 1) || (choice > 4));
cout<<endl;
switch (choice)
{
case 1:menu_tambah(); break;
case 2:menu_cari();break;
case 3:menu_lihat(); break;
case 4: break;
}
cout<<endl;
}
while(choice !=4);
return 0;
}
int search(int _nim)
{
for(int i=0;i<item_count;i++)
{
if(item[i].nim== _nim) return(i);
}
return(-1);
}
void tambah(int _nim, char _name[],char _jrs[])
{
item[item_count].nim= _nim;
strcpy(item[item_count].name, _name);
item[item_count].jrs= _jrs;
item_count = item_count +1;
}
void cari(int _nim)
{
int nimx = search(_nim);
for( int i=nimx+1; i<item_count;i++)
{
item[i-1] = item[i];
}
item_count = item_count -1;
}
void menu_tambah()
{
int _nim;
char _name[], _jrs[];
cout<<"tambah data mahasiswa"<<endl;
cout<<"nim = "<<endl;
cin>> _nim;
getchar();
cout<<"nama mahasiswa = ";
cin.getline( _name, sizeof(_name));
cout<<"jurusan = ";
cin>>_jrs;
add(_nim,_name,_jrs);
cout<<"Item added."<<endl;
}
void menu_cari()
{
int _nim;
cout<<"mencari data"<<endl;
cout<<"nim = ";
cin>> _nim;
int nimx = search(_nim);
if(nimx>-1)
{
char answer;
cout<<"Item found.";
}
else
{
cout<<"Item not found"<<endl;
}
}
void menu_lihat()
{
cout<<"list data mahasiswa"<<endl;
cout<<setiosflags(ios::left);
cout<<setw(5) <<"NIM"
<<setw(30) <<"NAME"
<<setw(10)<<"JURUSAN"
<<endl;
for(int i=0;i<item_count;i++)
{
TItem P = item[i];
cout<<setw(5)<<P.nim
<<setw(30)<<P.name
<<setw(10)<<P.jrs<<endl;
}
}
|
Line 71: You can't assign char pointers to char arrays using operator=. If _jrs is a null terminated string you can use std::strcpy.
Line 88: You have to specify the size of the arrays when you define them.
so what must i do with line 71? change to strcpy? or use another code?
Topic archived. No new replies allowed.