Reputation: 1
I know it's been asked,
but I still can't find a way out
I have to create data which will be sorted by student ID number
I hope you can help me out
#include <iostream>
using namespace std;
int main () {
int NIM[10];
string nama[10], alamat[10];
char golongan_darah[10];
int i, j;
for (i=0; i<10; i++) {
cout << "Masukkan data siswa ke-" << i+1 << endl;
cout << "NIM : "; cin >> NIM[i];
cout << "Nama : "; cin >> nama[i];
cout << "Alamat : "; cin >> alamat[i];
cout << "Golongan Darah ('A'/'B'/'AB'/'O') : "; cin >> golongan_darah[i];
}
int pivot;
for (i=0; i<10; i++) {
for (j=i+1; j<10; j++){
if (NIM[i] > NIM[j]) {
pivot= NIM[i];
NIM[i]=NIM[j];
NIM[j]=pivot;
pivot= nama[i];
nama[i]=nama[j];
nama[j]=pivot;
pivot= alamat[i];
alamat[i]=alamat[j];
alamat[j]=pivot;
pivot= golongan_darah[i];
golongan_darah[i]=golongan_darah[j];
golongan_darah[j]=pivot;
}
}
}
cout << " NIM | Nama | Alamat | Golongan Darah" << endl;
for (i=0; i<10; i++) {
cout << NIM [i] << nama[i] << alamat[i] << golongan_darah[i] << endl;
}
}
it gets error on pivot= nama[i];
and pivot= alamat[i];
thanks in advance
Upvotes: 0
Views: 2936
Reputation: 10096
You need to use a temporary variable to switch the order in the array. You've already done this with the pivot
for the integers, however, this does not work with strings.
int pivot;
std::string temp;
for (i=0; i<10; i++) {
for (j=i+1; j<10; j++){
if (NIM[i] > NIM[j]) {
pivot= NIM[i]; // NIM stores integers, so this is fine
NIM[i]=NIM[j];
NIM[j]=pivot;
temp= nama[i]; // nama stores strings, so use the new string temp variable
nama[i]=nama[j];
nama[j]=temp;
temp= alamat[i]; // alamat stores strings, so use the new string temp variable
alamat[i]=alamat[j];
alamat[j]=temp;
pivot= golongan_darah[i]; // golongan_darah is stores chars, these can be stored in int
golongan_darah[i]=golongan_darah[j];
golongan_darah[j]=pivot;
}
}
}
Alternatively, you can use std::swap
for (i=0; i<10; i++) {
for (j=i+1; j<10; j++){
if (NIM[i] > NIM[j]) {
std::swap( NIM[i], NIM[j] );
std::swap( nama[i], nama[j] );
std::swap( alamat[i], alamat[j] );
std::swap( golongan_darah[i], golongan_darah[j] );
}
}
}
Upvotes: 1
Reputation:
#include <iostream>
using namespace std;
int main () {
int NIM[10];
string nama[10], alamat[10];
char golongan_darah[10];
int i, j;
for (i=0; i<10; i++) {
cout << "Masukkan data siswa ke-" << i+1 << endl;
cout << "NIM : "; cin >> NIM[i];
cout << "Nama : "; cin >> nama[i];
cout << "Alamat : "; cin >> alamat[i];
cout << "Golongan Darah ('A'/'B'/'AB'/'O') : "; cin >> golongan_darah[i];
}
int pivot;
for (i=0; i<10; i++) {
for (j=i+1; j<10; j++){
if (NIM[i] > NIM[j]) {
pivot= NIM[i];
NIM[i]=NIM[j];
NIM[j]=pivot;
pivot= stoi(nama[i]); //as pivot is int type nama is changed using stoi() function.
nama[i]=nama[j];
nama[j]=pivot;
pivot= stoi(alamat[i]); //as pivot is int type nama is changed using stoi() function.
alamat[i]=alamat[j];
alamat[j]=pivot;
pivot= golongan_darah[i];
golongan_darah[i]=golongan_darah[j];
golongan_darah[j]=pivot;
}
}
}
cout << " NIM | Nama | Alamat | Golongan Darah" << endl;
for (i=0; i<10; i++) {
cout << NIM [i] << nama[i] << alamat[i] << golongan_darah[i] << endl;
}
}
Upvotes: 0
Reputation: 11
You're trying to assign a string value to an integer, you can fix this by using the stoi()
function. This will take a string and convert it into an integer datatype.
pivot= stoi(nama[i]);
pivot= stoi(alamat[i]);
Here is some more information: http://www.cplusplus.com/reference/string/stoi/
Upvotes: 1