Ik wou een script maken dat adhv de lengte afmetingen de oppervlakte van een figuur kan berekenen en het volume als de figuur 3D is. Ik heb daarvoor deze code gemaakt:
De errors zijn:
Ik denk dat alle errors er zijn omdat die arr niet goed wordt teruggestuurd/aangemaakt. Ik gebruikt geen pointers, wat zaagt ie dan over die int*, ik heb er nergens 1 staan? Wat doe ik fout?
Code:
#include <iostream.h>
int get_maten(int demensie)
{
if(demensie == 2)
{
int lengte;
int breedte;
cout << "Geef de lengte: ";
cin >> lengte;
cout << "Geef de breedte";
cin >> breedte;
int arr[2] = { lengte, breedte };
return arr;
}
else
{
int lengte;
int breedte;
int hoogte;
cout << "Geef de lengte: ";
cin >> lengte;
cout << "Geef de breedte";
cin >> breedte;
cout << "Geef de hoogte";
cin >> hoogte;
int arr[3] = { lengte, breedte, hoogte };
return arr;
}
}
bool check_maten(int demensie, int arr)
{
if(arr[0] < 1)
return FALSE;
if(arr[1] < 1)
return FALSE;
if(demensie == 3 && arr[2] < 1)
return FALSE;
return TRUE;
}
int get_opp_2d(int arr)
{
return arr[0] * arr[1];
}
int get_opps_3d(int arr)
{
int opp1 = arr[0] * arr[1];
int opp2 = arr[1] * arr[2];
int opp3 = arr[0] * arr[2];
int arr2 = { opp1, opp2, opp3 };
return arr2;
}
int get_volume_3d(int arr)
{
return arr[0] * arr[1] * arr[2];
}
int main()
{
int demensie;
cout << "Is uw figuur 2D of 3D? [2/3]";
cin >> demensie;
if(demensie < 2 || demensie > 3)
cout << "Fout, de figuur moet ofwel 2D ofwel 3D zijn.";
else
{
int arr;
bool check:
arr = get_maten(demensie);
check = check_maten(demensie,arr);
if(check === FALSE)
cout << "1 van de maten is kleiner dan 1 en is bijgevolg niet geldig/";
else
{
int opp;
if(demensie == 2)
{
opp = get_opp_2d(arr);
cout << "De oppervlakte van deze figuur is " << opp << endl;
}
else
{
int volume;
opp = get_opps_3d(arr);
volume = get_volume_3d(arr);
cout << "De oppervlakten zijn" << endl << opp[0] << endl << opp[1] << endl opp[3] << endl << endl << "Het volume is " << volume << endl;
}
}
}
return 0;
}
De errors zijn:
oppervlak_inhoud-6.cpp: In function ‘int get_maten(int)’:
oppervlak_inhoud-6.cpp:18: error: invalid conversion from ‘int*’ to ‘int’
oppervlak_inhoud-6.cpp:16: warning: address of local variable ‘arr’ returned
oppervlak_inhoud-6.cpp:37: error: invalid conversion from ‘int*’ to ‘int’
oppervlak_inhoud-6.cpp:35: warning: address of local variable ‘arr’ returned
oppervlak_inhoud-6.cpp: In function ‘bool check_maten(int, int)’:
oppervlak_inhoud-6.cpp:43: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:44: error: ‘FALSE’ was not declared in this scope
oppervlak_inhoud-6.cpp:46: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:47: error: ‘FALSE’ was not declared in this scope
oppervlak_inhoud-6.cpp:49: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:50: error: ‘FALSE’ was not declared in this scope
oppervlak_inhoud-6.cpp:52: error: ‘TRUE’ was not declared in this scope
oppervlak_inhoud-6.cpp: In function ‘int get_opp_2d(int)’:
oppervlak_inhoud-6.cpp:57: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:57: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp: In function ‘int get_opps_3d(int)’:
oppervlak_inhoud-6.cpp:62: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:62: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:63: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:63: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:64: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:64: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:66: error: scalar object ‘arr2’ requires one element in initializer
oppervlak_inhoud-6.cpp: In function ‘int get_volume_3d(int)’:
oppervlak_inhoud-6.cpp:73: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:73: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:73: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp: In function ‘int main()’:
oppervlak_inhoud-6.cpp:88: error: a function-definition is not allowed here before ‘:’ token
oppervlak_inhoud-6.cpp:91: error: ‘check’ was not declared in this scope
oppervlak_inhoud-6.cpp:93: error: expected primary-expression before ‘=’ token
oppervlak_inhoud-6.cpp:93: error: ‘FALSE’ was not declared in this scope
oppervlak_inhoud-6.cpp:111: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:111: error: invalid types ‘int[int]’ for array subscript
oppervlak_inhoud-6.cpp:111: error: expected `;' before ‘opp’
Ik denk dat alle errors er zijn omdat die arr niet goed wordt teruggestuurd/aangemaakt. Ik gebruikt geen pointers, wat zaagt ie dan over die int*, ik heb er nergens 1 staan? Wat doe ik fout?