I'm trying to solve the 'nilai' was not declared in this scope error which uses array and it happens at line 35, so please fix some thing to make work again and generate maximum number between numbers in an array (and minimum number, but it not included yet). Please be quick ASAP due my homework is due 4 hours left as i created this help forum. The code will be included below. Thank you
#Using array for function to output a variable help
1 messages · Page 1 of 1 (latest)
When your question is answered use !solved or the button below to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
can you share the actual code ? 😅
ah wait, you where already on it
i using geany btw because my univ use it for practice usage
well, if you want access to nilai, you need to pass nilai to the function
like?
@lost scaffold this maybe? https://www.w3schools.com/cpp/cpp_function_array.asp
exactly the same way you pass v to the function
I also highly reccomend not using W3 school or G4G they are both pretty bad for C(++)
How to Learn C++ Programming
We generally recommend a good book to learn the necessary fundamentals:
To actually write and run C++ code, you will need a compiler, editor, and debugger. We strongly recommend to start out using an IDE, which will provide all these tools for you:
<:microsoft:1165512917047853127> Windows
- [Visual Studio](#1165492293810257920 message)
- CLion
<:tux:1165505626894520361> Linux
also is there a specific reason you forward declare max instead of just putting max above main?
i would complain more about global namespace variables than about forward declared func
oh yeah thats another point
what does mean and how to fix it?
your forward declaration says the function will be of the signature int(float, int*)
but your actual function definition has the signature int(float*, int*)
also im not seeing the 'nilai' was not declared in this scope error you talked about in the error list at the bottom of the screenshot
because i fixed it
compile;
@coarse slate
ok
so is the issue solved ?
not yet
what help do you need more ?
;compile
Compiler Output
<source>: In function 'int main()':
<source>:27:21: error: invalid conversion from 'int*' to 'int' [-fpermissive]
27 | maxnum = max(&nilai[100], &v);
| ~~~^~~~~~~~~~~~~~~~~
| |
| int*
<source>:27:21: note: possible fix: dereference with '*'
27 | maxnum = max(&nilai[100], &v);
| ~~~^~~~~~~~~~~~~~~~~
| *
Build failed
imagane | c++ | x86-64 gcc 15.2 | godbolt.org
Compiler Output
<source>: In function 'int main()':
<source>:27:21: error: no matching function for call to 'max(int (*)[jumlah], void*)'
27 | maxnum = max(&nilai, &&v);
| ~~~^~~~~~~~~~~~~
<source>:27:21: note: there are 3 candidates
In file included from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compilers_c++_x86_gcc_15.2.0/include/c++/15.2.0/string:53,
from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compilers_c++_x86_gcc_15.2.0/include/c++/15.2.0/bits/locale_classes.h:42,
from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compilers_c++_x86_gcc_15.2.0/include/c++/15.2.0/bits/ios_base.h:43,
from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compilers_c++_x86_gcc_15.2.0/include/c++/15.2.0/ios:46,
from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compilers_c++_x86_gcc_15.2.0/include/c++/15.2.0/bits/ostream.h:43,
from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compile
imagane | c++ | x86-64 gcc 15.2 | godbolt.org
(this is also part of the reason I don't reccomend doing forward declarations like you do)
;compile
#include <iostream>
using namespace std;
int jumlah, sum, i, v, a, maxnum, minnum;
int max(int*, int);
float rata2;
int main() {
cout << "[Pengelolahan Nilai Mahasiswa]\n\n";
do{
cout << "Masukkan Jumlah Mahasiswa: "; cin >> jumlah;
if(jumlah > 100){
cout << "Jumlah mahasiswa melebihi batas sistem (maksimal 100 orang)\n";
}
}while(jumlah > 100);
string nama[jumlah];
int nilai[jumlah];
for(int i=0; i<jumlah; i++){
cout << "\n[Data Mahasiswa ke-" << i+1 << "]" << endl;
cout << "Masukkan Nama: "; cin >> nama[i];
cout << "Masukkan Nilai Mahasiswa: "; cin >> nilai[i];
cout << endl;
};
for(int i=0; i<jumlah; i++){
sum += nilai[i];
};
rata2 = sum/jumlah;
maxnum = max(nilai, jumlah);
cout << rata2;
cout << maxnum;
}
int max(int nilai[], int v){
int i = 0;
for(i=1; i<jumlah; i++){
if(nilai[i] < v){
v = nilai[i];
}
};
return v;
}
Program Output
[Pengelolahan Nilai Mahasiswa]
Masukkan Jumlah Mahasiswa:
Compiler Output
Program terminated with signal: SIGFPE
j.a.r.v.i.s8285 | 136ms | c++ | x86-64 gcc 15.2 | godbolt.org
what is V supposed to be? an why are you passing it in as a pointer pointer?
this is so hard to debug when i dont know what these jumlah and all words mean, like when i dont know what the problem statement is
these words are comes from indonesian
but it looks like the program is compiling ?
should i tell you my homework?
yes , a problem statement will be very helpful
Buatlah sistem penilaian untuk mahasiswa dimana diawal kita input nama dan nilai mahasiswa, lalu di outputkan semua nilai dan stringnya dan diakhir dikasih statistik penilaian nya contoh:
===== Program Pengolahan Nilai Mahasiswa =====
Masukkan jumlah mahasiswa: 3
--- Data Mahasiswa ke-1 ---
Masukkan Nama: Budi
Masukkan Nilai: 85
--- Data Mahasiswa ke-2 ---
Masukkan Nama: Ani
Masukkan Nilai: 92
--- Data Mahasiswa ke-3 ---
Masukkan Nama: Chandra
Masukkan Nilai: 78
=======================================
--- Hasil Pengolahan Data ---
Daftar Nilai Mahasiswa:
-
Budi : 85
-
Ani : 92
-
Chandra : 78
Statistik Nilai:
Nilai Rata-rata : 85.00
Nilai Tertinggi : 92 (Diraih oleh: Ani)
Nilai Terendah : 78 (Diraih oleh: Chandra)
Here the english translation:
===== Student Score Processing Program =====
Enter the number of students: 3
--- Student Data 1 ---
Enter Name: Budi
Enter Grade: 85
--- Student Data 2 ---
Enter Name: Ani
Enter Grade: 92
--- Student Data 3 ---
Enter Name: Chandra
Enter Score: 78
=======================================
--- Data Processing Results ---
Student Score List:
1. Budi : 85
2. Ani : 92
3. Chandra : 78
Grade Statistics:
Average Grade : 85.00
Highest Grade : 92 (Achieved by: Ani)
Lowest Grade : 78 (Achieved by: Chandra)```
also using function on my programs would get a bonus points for the task
;compile
#include <iostream>
int max(int nilai[], int jumlah){
int ans = 0;
for(int i=0; i<jumlah; i++){
if(nilai[i] > ans){
ans = nilai[i];
}
}
return ans;
}
int main() {
std::cout << "[Pengelolahan Nilai Mahasiswa]\n\n";
int jumlah;
do{
std::cout << "Enter the number of students: ";
std::cin >> jumlah;
if(jumlah > 100){
std::cout << "Jumlah mahasiswa melebihi batas sistem (maksimal 100 orang)\n";
}
}while(jumlah > 100);
std::string nama[100];
int nilai[100];
for(int i=0; i<jumlah; i++){
std::cout << "\n[Data Mahasiswa ke-" << i+1 << "]" << std::endl;
std::cout << "Masukkan Nama: ";
std::cin >> nama[i];
std::cout << "Masukkan Nilai Mahasiswa: ";
std::cin >> nilai[i];
std::cout << std::endl;
}
int sum{}, rata2{}, maxnum{};
for(int i=0; i<jumlah; i++){
sum += nilai[i];
}
rata2 = sum/jumlah;
maxnum = max(nilai, jumlah);
std::cout << rata2;
std::cout << maxnum;
}
Program Output
[Pengelolahan Nilai Mahasiswa]
Enter the number of students:
Compiler Output
Program terminated with signal: SIGFPE
j.a.r.v.i.s8285 | 189ms | c++ | x86-64 gcc 15.2 | godbolt.org
@uncut olive i believe the above program is the debugged version of the code you sent
this?
yes
okei, i'd try this
anyways can you show me which website to compare 2 diff codes?
there are few things you should do and dont do in general when writing code
- Dont use the line
using namespace std;at the top of your code , writestd::coutandstd::cinexplicitly , its not that hard - You cant make an array which is dynamically sized on the stack , instead look into making one on the heap using the
newkeyword. You tried to do something likestd::string names[variable], you cant do that. (or if they have taught you whatstd::vectoris , use that instead) - As you previously saw , having seperate declarations can mess with you , just define your functions at the top
- Revisit what pointers are and how they are pased as arguments and how they are used , your original code looked like you are confused about how to use the properly
;compile
Program Output
[Program Pengelolahan Nilai Mahasiswa]
Masukkan Jumlah Mahasiswa: Jumlah mahasiswa melebihi batas sistem (maksimal 100 orang)
Masukkan Jumlah Mahasiswa: Jumlah mahasiswa melebihi batas sistem (maksimal 100 orang)
Masukkan Jumlah Mahasiswa: Jumlah ma
imagane | 56ms | c++ | x86-64 gcc 15.2 | godbolt.org