I am new in C++
This is my code below:
the result of my function lancerDe is an array TabDes. I want the data to return to the main of my program. is it possible
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int lancerDe()
{
int TabDes[10];
srand ( time(NULL) );
for (int i=0;i<10;i++)
{
TabDes[i]= rand() % 6 + 1;
cout << TabDes[i] << endl;
}
}
int main()
{
int Table[10];
char reponse;
reponse = 'o' ;
int counter = 0;
while (reponse == 'o')
{
if (counter == 0)
{
cout <<"Voulez-vous lancer un de (o/n) ? ";
cin >> reponse ;
counter++;
}
else
{
cout <<"Voulez-vous rejouer (o/n) ? ";
cin >> reponse ;
}
cout <<"Les faces obtenues a chaque lancer de de sont :" << endl;
lancerDe();
Yes, but you would have to allocate the array using new and use delete on the pointer at the end of main. Also, make lancerDe() return an int* instead of an int (it's an array not a single integer).
int* makeArray(unsignedint size) {
if(size == 0) throw std::exception("I don't feel like handling this");
returnnewint[size];
}
int main() {
int* array = makeArray(5);
for(unsignedint i = 0; i < 5; ++i) array[i] = 2*i;
for(unsignedint i = 0; i < 5; ++i) std::cout<<array[i]<<std::endl;
delete array; //remember to delete it, since you are the only one with a pointer to it
return 0;
}
Alright, I don't know much about C++...I'm still learning the language so idk what the HECK a throw or catch or exception is, but I do know that when you're returning arrays, you CAN'T. You can return a pointer to an array.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<iostream>
#include<cmath>
int * that_array_function(void);
int main(void)
{
...
int * arr = that_array_function();
return 0;
}
int * that_array_function(void);
...
int b[x] //where x is the size
return b //or return * b or something...