-3

so I wanna make a mini-quiz in c++ and it doesn't work, here is a code that i've tried

static const char alphanum[] =

"which one of this cities located in france"

cout << "1. paris   2. singapore   3. dubai    4. thailand";

"which one of this countries located in asia"

cout << "1. thailand   2. germany   3. spain    4. italy";

int stringLength = sizeof(alphanum) - 1;

char genRandom()  // Random string generator function.
{

    return alphanum[rand() % stringLength];

}

int main()

{

    srand(time(0));

    for(int z=0; z < 1; z++)

    {

        cout << genRandom();

    }

example:

which one of this countries located in asia

  1. thailand 2. germany 3. spain 4. italy
Lala5th
  • 1,137
  • 7
  • 18
  • 3
    For `c++` you should use `std::string` instead of `const char*`. An other thing to note is to have a `C` like string you should use `const char* array[]`, because `const char array[]` only declares an array of characters not an array of strings. You don't need the `cout << "..."` in the array declaration either. There are other syntax errors, though. You should see look up how to declare arrays. – Lala5th Aug 07 '21 at 16:53

1 Answers1

0

This is the working code similar to your original code, but using std::string instead of char*. Your code has many problems, therefore I will not explain them, I just recommend you to read a decent book about C++:

#include <string>
#include <iostream>
using namespace std;

static const string alphanum[] ={
"which one of this cities located in france \n1. paris   2. singapore   3. dubai    4. thailand",
"which one of this countries located in asia\n1. thailand   2. germany   3. spain    4. italy"
};

const int stringLength = sizeof(alphanum)/sizeof( *alphanum);

const string& genRandom()  // Random string generator function.
{
    return alphanum[rand() % stringLength];
}

int main()
{
    srand(time(0));

    for(int z=0; z < 1; z++)
    {
        cout << genRandom();
    }
}
Laci
  • 2,738
  • 1
  • 13
  • 22