Im getting an error at line 67 "cout << party_name[x] << "\n";" expression must be a pointer to a complete object type. Any help would be great as I am quite new to C++ and am not sure what exactly is wrong with the code. I have googled a fair bit but nothing has really made sense as to why I am not able to get the string array passed back correctly.
I am a student with the assignment to create a text based game and for part of the criteria we need to randomise the amount of party members that are with you and give the ability to assign them names. I have managed to get the amount of party members done and the variable is being passed back to the game section perfectly fine where I am struggling is passing back the string array party_names into the game function.
I should stress these all come from the main which goes straight into the game function the formatting is just for the programs sake.
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
usingnamespace std;
void game();
int party_rnd(int &party_members)
{
char roll_party;
cout << "Please [R]oll the dice to decide how many will dare enter with you.\n";
cin >> roll_party;
if (roll_party == 'R') {
srand(time(NULL));
party_members = rand() % 6 + 1;
cout << "You have been joined by " << party_members << " of your friends!\n\n";
}
else {
cout << "\nYou can't not roll!\n";
system("Pause");
system("cls");
game();
}
return 0;
}
string party_name(int &party_members, string (&party_names))
{
string* party_name = new string[party_members];
for (int i = 0; i < party_members; i++) {
cout << "Please enter a name for your party member: ";
cin >> party_name[i];
cout << endl;
}
cout << "Your party consists of: ";
cout << endl;
for (int x = 0; x < party_members; x++) {
cout << party_name[x] << "\n";
}
return *party_name;
}
void game()
{
system("cls");
int party_members = 0;
string* party_names = new string[party_members];
cout << "Welcome to Professor Scott and the Temple of Doom!\n";
cout << "Before you can enter you must roll a dice to see how many of your friends will follow you!\n\n";
party_rnd(party_members);
party_name(party_members, party_names[party_members]);
for (int y = 0; y < party_members; y++) {
cout << party_name[y] << "\n";
}
return;
}
Thank you for the reply, I have changed that line to: int party_members; instead of having a = value. However the error is there still even with this change.
int party_members = 0;
party_rnd(party_members); // *** <---
string* party_names = new string[party_members];
cout << "Welcome to Professor Scott and the Temple of Doom!\n";
cout << "Before you can enter you must roll a dice to see how many of your friends will follow you!\n\n";
Maybe this is what you meant so you get a random number of players. In other words reposition the arrowed line.
I did the changes you suggested and was still getting the error on line 67 which is why I didn't see it as fixed. This is the current code I have for void game() where I am still getting that error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void game()
{
system("cls");
int party_members;
party_rnd(party_members);
string* party_names = new string[party_members];
cout << "Welcome to Professor Scott and the Temple of Doom!\n";
cout << "Before you can enter you must roll a dice to see how many of your friends will follow you!\n\n";
party_name(party_members, party_names[party_members]);
for (int y = 0; y < party_members; y++) {
cout << party_name[y] << "\n";
}
return;
}
The main() I didn't include in the quote because I didn't want to paste a huge wall of code but have put all the code I have below. I altered the code to put the party_members value along with each name as it gets printed out.
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
usingnamespace std;
void game();
int party_rnd(int &party_members)
{
char roll_party;
cout << "Welcome to Professor Scott and the Temple of Doom!\n";
cout << "Before you can enter you must roll a dice to see how many of your friends will follow you!\n\n";
cout << "Please [R]oll the dice to decide how many will dare enter with you.\n";
cin >> roll_party;
if (roll_party == 'R') {
srand(time(NULL));
party_members = rand() % 6 + 1;
cout << "You have been joined by " << party_members << " of your friends!\n\n";
}
else {
cout << "\nYou can't not roll!\n";
system("Pause");
system("cls");
game();
}
return 0;
}
string party_name(int &party_members, string (&party_names))
{
string* party_name = new string[party_members];
for (int i = 0; i < party_members; i++) {
cout << "Please enter a name for your party member: ";
cin >> party_name[i];
cout << endl;
}
cout << "Your party consists of: ";
cout << endl;
for (int x = 0; x < party_members; x++) {
cout << party_name[x] << " " << party_members << "\n";
}
return party_name[party_members];
}
void game()
{
system("cls");
int party_members;
party_rnd(party_members);
string* party_names = new string[party_members];
party_name(party_members, party_names[party_members]);
for (int y = 0; y < party_members; y++) {
cout << party_name[y] << "\n";
}
return;
}
int main()
{
char menu_choice;
cout << "Main Menu:\n";
cout << "[S]tart Game\n";
cout << "[E]xit Game\n\n";
cout << "Please choose an option: ";
cin >> menu_choice;
if (menu_choice == 'S') {
cout << "\nWelcome to The Temple of Doom!";
game();
}
elseif (menu_choice == 'E') {
cout << "\nMaybe you'll enter next time.";
} else {
cout << "\nPlease enter a valid option\n";
system("Pause");
system("cls");
main();
}
return 0;
}
Well that fixed the error. Can't believe I missed the 's' off. I changed that it built without any errors however when run after entering the last name it crashes.
Also, because you are using new without a corresponding delete[] there might be a problem when you run the program without exiting - just a guess on my part.
So if that is the case what would be the the solution? I am assuming that delete would delete the array and if so I would need to do that at the very end of the program which is still a ways off.
Main Menu:
[S]tart Game
[E]xit Game
Please choose an option: S
Welcome to The Temple of Doom!Welcome to Professor Scott and the Temple of Doom!
Before you can enter you must roll a dice to see how many of your friends will follow you!
Please [R]oll the dice to decide how many will dare enter with you.
R
You have been joined by 2 of your friends!
Please enter a name for your party member: Mark
Please enter a name for your party member: Mary
Your party consists of:
Mark 2
Mary 2
Program ended with exit code: 0