I got this problem when I tried to code for competitive programming courses, but I got this problem about void:
Code 1: On the first code, it could be executed properly. the void didn't need to be initialized and everything works really well.
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#define ll long long
#define ar array
using namespace std;
int n; int b;int a[100000];
void solve(){
cin >> n >> b;
for(int i =0;i <n;++i){
cin >> a[i];
}
sort(a,a+n);
int ans=0;
for(int i=0;i < n;++i){
if(b>=a[i]){
b-=a[i];
++ans;
}
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t;int e=1;
cin >> t;
while(t--){
cout << "Case #" << e <<":";
solve();
}
}
Output:
Success
but on another code, the void need to be initialized ,and when I call it in main,it isn't declared on the scope.is there something wrong with the code?or i need to redownload the gcc? code 2:
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#define ll long long
#define ar array
int a; int b;int c[10000]
void solution(){
cin >> a >> b;
string f;
cin >> f;
while(a--){
string l;int x; int y; int z;
cin >> l >> x >> y >> z;
cout << l << x << y << z endl;
}
}
using namespace std;
int main()
{
int t;
cin >> t;
while(t--){
solution();
}
}
Output:
seleksi_olimpiade.cpp:7:1: error: expected initializer before 'void'
7 | void solution(){
| ^~~~
seleksi_olimpiade.cpp: In function 'int main()':
seleksi_olimpiade.cpp:23:5: error: 'solution' was not declared in this scope
23 | solution();
| ^~~~~~~~