Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <iostream>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <random>
#include <ctime>

using namespace std;

int iseed = (int)time(0);
srand(iseed);
int d6 = (rand() %6) + 1;
string name;
string race;
int str = d6+d6+6;
int dex = d6+d6+6;
int con = d6+d6+6;
int inte = d6+d6+6;
int wis = d6+d6+6;
int cha = d6+d6+6;
int hp = ((con - 10) / 2)  + d6;
bool simpleWeaponProficiency = false;
bool martialWeaponProficiency = false;
bool exoticWeaponProficiency = false;
bool while_1 = true;

void characterCreation() {
    while (while_1){
        while_1 = false;
        cout << "Your name?: ";
        cin >> name;
        cout << name << ", your sure?" << "\n";
        string nameChoice;
        cin >> nameChoice;
        if (nameChoice == "yes") {
            break;
        } else {
            while_1 = true;
        }
    }
    cout << str << "\n";
    cout << "Alright, " << name << ". where do you hail from?" << "\n";
    cout << "The Human Cities? The Elven Forests perhaps? or the Mighty Dwarven Kingdoms?" << "\n";
    string origin;
    cin >> origin;
    if (origin == "the human cities" or "human cities" or "human") {
        race = "human";
        str + 1;
        dex + 1;
        con + 1;
        inte + 1;
        wis + 1;
        cha + 1;
        cout << race << "?" << "\n";
    }
    cout <<  "Strength:" << str << "\n";
    
}

int main() {
    cout << "Welcome to my adventure game!" << "\n" << "Please type everything in lowercase." << "\n";
    cout << "-----------------------------" << "\n";
    characterCreation();
}


There is the code. Everytime I run it it produces:

C++
main.cpp:11:6: error: expected constructor, destructor, or type conversion before ‘(’ token
   11 | srand(iseed);
      |      ^
main.cpp: In function ‘void characterCreation()’:
main.cpp:49:13: warning: statement has no effect [-Wunused-value]
   49 |         str + 1;
      |         ~~~~^~~
main.cpp:50:13: warning: statement has no effect [-Wunused-value]
   50 |         dex + 1;
      |         ~~~~^~~
main.cpp:51:13: warning: statement has no effect [-Wunused-value]
   51 |         con + 1;
      |         ~~~~^~~
main.cpp:52:14: warning: statement has no effect [-Wunused-value]
   52 |         inte + 1;
      |         ~~~~~^~~
main.cpp:53:13: warning: statement has no effect [-Wunused-value]
   53 |         wis + 1;
      |         ~~~~^~~
main.cpp:54:13: warning: statement has no effect [-Wunused-value]
   54 |         cha + 1;
      |         ~~~~^~~
make: *** [<builtin>: main.o] Error 1


I don't know why there is these statements aren't affecting anything nor do I know how to fix the error at the top. Thanks to anyone who wants to help.

What I have tried:

Scowering google and other forums. Plus some documentation.
Posted
Updated 4-Jan-23 0:49am

1 solution

Because it isn't contained in any method: all executable code (other than initialization of variables with compile-time constant values) must be inside a function body.

The "has no affect" errors mean exactly what they say: the statement has no effect (because the result isn't stored or otherwise used anywhere).

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900