-
Fast random quotes
Code:
#include <iostream.h>
#include <time.h>
#include <windows.h>
#include <conio.c>
int rand_mid(int low, int high)
{
srand(time(NULL));
return low + rand()%(high-low+1);
}
int randomnumber = rand_mid(1,10);
char declaration(randomnumber);
char *point =& declaration(randomnumber); // ERROR HERE
char declaration1[20] = "ONE";
char declaration2[20] = "TWO";
char declaration3[20] = "THREE";
char declaration4[20] = "FOUR";
char declaration5[20] = "FIVE";
char declaration6[20] = "SIX";
char declaration7[20] = "SEVEN";
char declaration8[20] = "EIGHT";
char declaration9[20] = "NINE";
char declaration10[20] = "TEN";
int main()
{
cout << point;
Sleep(1000);
return 0;
}
I want this to write randomly "ONE", "TWO", etc., however if gives me an error, where indicated, saying "declaration cannot be used as a function"
Any ideas ?
-
There are many errors in your code. Here's a way to solve the problem:
Code:
char declaration[10][] = {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"};
cout << declaration[ rand()%10 ];
-
This still has errors :
Code:
#include <iostream.h>
#include <time.h>
#include <windows.h>
#include <conio.c>
int rand_mid(int low, int high)
{
srand(time(NULL));
return low + rand()%(high-low+1);
}
char dec[10][] = {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"}; // ERROR
int main()
{
cout << dec[rand_mid(1,10)] << endl << endl; // ERROR 2
system("pause");
return 0;
}
I get the following error :
declaration of 'dec' as multidimensional array
must have bounds for all dimensions except the first
assignment (not initialization) in declaration
Error 2 :
ANSI C++ forbids using pointer to a function in arithmetic
What's wrong now ? Thanks.
-
>>char dec[10][]
C++ requires that you supply the size of all dimensions except the first. But here it's better to just use an array of char *'s so that you don't have to worry about the second dimension.
>>srand(time(NULL));
Don't call srand() in the same function as rand(), if you do it'll reseed the generator every time you call the function and your random numbers won't be random at all.
>>dec[rand_mid(1,10)]
This is an off by one error, the array only has indices from 0-9.
>>ANSI C++ forbids using pointer to a function in arithmetic
dec is a modifier for cout in C++, this name clash is causing your error, you should change the name of the array.
On top of all this you still neglected to include stdlib.h, which is required for rand() and srand(). :-) Try this, it should work better.
Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int rand_mid(int low, int high)
{
return low + rand()%(high-low+1);
}
char *decl[] = {
"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"
};
int main()
{
srand(time(NULL));
cout << decl[rand_mid(0,9)] << endl << endl;
system("pause");
}
-
You don't need the rand_mid function, do you? Just use this:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
char *decl[] = {
"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","N INE","TEN"
};
cout << decl[rand()%10] << "\n\n";
system("pause");
}
-