Personal C++ Project: Expected Primary Expression

Hi!
I’m working on a project in C++ that generates random DnD characters. This is my code so far:

#include <iostream>
#include <cstdlib>

std::string find_name() {
    int name_number = std::rand() % 6;
    std::string name;

    switch (name_number) {
    case 0:
        name = "Robert";
    case 1:
        name = "Peter";
    case 2:
        name = "Charles";
    case 3:
        name = "Susan";
    case 4:
        name = "Daniela";
    case 5:
        name = "Katrina";
    }

    return name;
  }

  std::string find_species() {
    int species_number = std::rand() % 6;
    std::string species;

    switch (species_number) {
    case 0:
        species = "Human";
    case 1:
        species = "Dwarf";
    case 2:
        species = "Elf";
    case 3:
        species = "Faerie";
    case 4:
        species = "Satyr";
    case 5:
        species = "Nymph";
    }

    return species;
  }

  std::string find_age() {
    int age;
    std::string string_age = "Immortal";
    std::string age_phrase = "Age: ";

    if (find_species() == "Human") {
      age = std::rand() % 18 + 79;
      age_phrase += age;
    } else if (find_species() == "Dwarf") {
      age = std::rand() % 24 + 240;
      age_phrase += age;
    } else if (find_species() == "Elf") {
      age = std::rand() % 100 + 750;
      age_phrase += age;
    } else if (find_species() == "Satyr") {
      age = std::rand() % 50 + 500;
      age_phrase += age;
    } else {
      age_phrase += string_age;
    }

    return age_phrase;
  }

  std::string find_class() {
    int class_number = std::rand() % 12;
    std::string class = " ";

    switch (class_number) {
    case 0:
        class = "Barbarian";
    case 1:
        class = "Bard";
    case 2:
        class = "Cleric";
    case 3:
        class = "Druid";
    case 4:
        class = "Fighter";
    case 5:
        class = "Monk";
    case 6:
        class = "Paladin";
    case 7:
        class = "Ranger";
    case 8:
        class = "Rogue";
    case 9:
        class = "Sorcerer";
    case 10:
        class = "Warlock";
    case 11:
        class = "Wizard";
    }

    return class;
  }

int main() {
  std::cout << "NEW CHARACTER\n";
  std::cout << "Name: " + find_name() + "\n";
  std::cout << "Species: " + find_species() + "\n";
  std::cout << find_age() + "\n";
  std::cout << "Class: " + find_class() + "\n";
}

When I try to compile and run it, find_class gives me a whole slew of errors, because it’s angry that, on each line that I try to redefine the class variable, I haven’t put a primary-expression in front of the word class. I thought it was wanting me to create the variable anew in each case of the switch statement, however, when I did that (and deleted the original definition) it got mad for a different reason that I can’t remember. Anyways, what’s the best fix for a primary-expression error?

By the way, it also calls the primary-expression error for the original variable declaration.

I believe the word class is a reserved word in C++ (for classes).

So you need to give your string variable a different name. This should fix that error.

1 Like

That made the code work a little better! Thank you :slight_smile:

A new issue has arisen! I believe that I declared the random numbers incorrectly (or something), because the returned character is always the same:

NEW CHARACTER
Name: Katrina
Species: Nymph
Age: Immortal
Class: Wizard

Code is exactly the same, except class has been changed to char_class.

cout each time you generate a random number to confirm…
You have to change your seed to get a different random set of numbers.

I added the code to change the seeds, and checked that it was creating random numbers (it was!). I ended up rewriting the code so the switch statements were if/else statements, and now it works 100%!