You have a number of issues.
- Your code as presented seems to have unbalanced braces { }
- Arrays start from 0; to get the input and average correct your loops need to be
for (i = 0; i < n; i ++)
(twice). Note the start at i=0.
- You are possibly a victim of the fact that
cin >>
doesn't extract a newline from the stream, so that any following getline will take the rest of the line up to '\n' (which is usually blank). There are a number of ways around this, including using cin.ignore for (up to) a large number of characters, delimited by '\n'. Alternatively, you can use getline for all input, and stringstream the resulting strings (or use stoi(), stod() etc) to extract numerical values from them
- It's a personal view, but I think you are overdoing the typedefs.
Sorry I can't provide a better revision to your code, but once it leaves English or French behind then I'm stuck.
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
|
#include <iostream>
// #include <stdio.h> // (Remove; unnecessary)
#include <string>
using namespace std;
typedef struct
{
string nama;
float nilai;
}nilai;
typedef nilai siswa[100];
siswa tabel;
int main()
{
int i, n;
cout << "Enter n: "; // (Include a prompt)
cin >> n; cin.ignore( 1000, '\n' ); // **** Clear the rest of the line (including newline)
for (i = 0; i < n; i ++){ // **** Count from 0
cout << "Masukan Nama Siswa : ";
getline(cin, tabel[i].nama);
cout << "Masukan Nilai Siswa : ";
cin >> tabel[i].nilai; cin.ignore( 1000, '\n' ); // **** Clear the rest of the line (including newline)
// } // **** This line produces too many } braces
}
float total = 0;
for (i = 0; i < n; i++){ // **** Count from 0
total += tabel[i].nilai;
}
float rata_rata;
rata_rata = total/n;
cout << "rata_tata = " << rata_rata;
return 0;
}
|