This program is essentially supposed to count the results of random throws of a dice 100 times and count the occurrence of each face then display all of them as a histogram of asterisks. It would seem the functions could be working but i'm unable to verify because after I make my choice, nothing displays.
#include <iostream>
#include <iomanip>
#include <string>
#include <random>
#include <sstream>
using namespace std;
enum class Side {
ONE, TWO, THREE, FOUR, FIVE, SIX
};
struct Bar {
int value;
Side label;
};
//roll dice function
void rollDice( Bar h[], int n = 100);
void rollDice( Bar h[], int){
default_random_engine en;
uniform_int_distribution<> dist{1,6};
int results[] ={0,0};
for( int n; n<=100; n++){
cout << dist(en);
results[dist(en)]++;
h[n].value = results[n];
if(h[n].value == 1){
h[n].label = Side::ONE;
}
else if(h[n].value == 2){
h[n].label = Side::TWO;
}
else if(h[n].value == 3){
h[n].label = Side::THREE;
}
else if(h[n].value == 4){
h[n].label = Side::FOUR;
}
else if(h[n].value == 5){
h[n].label = Side::FIVE;
}
else {
h[n].label = Side::SIX;
}
}
};
string getHistogram(Bar h[], char c = '*');
string getHistogram(Bar h[], char c ){
stringstream ast;
for( int n; n<=100; n++){
switch (h[n].label)
{
case Side::ONE:
return to_string(c);
break;
case Side::TWO:
return to_string(c);
break;
case Side::THREE:
return to_string(c);
break;
case Side::FOUR:
return to_string(c);
break;
case Side::FIVE:
return to_string(c);
break;
case Side::SIX:
return to_string(c);
break;
default:
break;
}
}
ast << "One: " << c << endl;
ast << "Two: " << c << endl;
ast << "Three: " << c << endl;
ast << "Four: " << c << endl;
ast << "Five: " << c << endl;
ast << "Six: " << c << endl;
string output = ast.str();
cout<< output;
return output;
}
int main (){
Bar histogram[] = {
{0,Side::ONE},{0,Side::TWO}, {0,Side::THREE},
{0,Side::FOUR},{0,Side::FIVE}, {0,Side::SIX}
};
char choice;
do {
cout << "DICE ROLLING SIMULATION" << endl
<<"===============================" << endl
<< "r. Roll Dice" << endl
<< "h. Display histogram" << endl
<< "q. Quit program\n" << endl
<< "\nEnter your choice:" << endl;
// Reading a single character using the scanner
cin >> choice;
switch(choice) {
case 'r': case 'R':
rollDice(histogram, 100);
break;
case 'h': case 'H':
cout<< getHistogram(histogram, '*');
break;
case 'q': case 'Q':
cout << "Good bye\n" << endl;
break;
default:
cout << "Invalid choice\n" << endl;
}
} while(choice != 'q');
}
So, this code contains many different mistakes. Let's go in order.
In structBar label may be const
struct Bar {
int value;
const Side label;
};
You don't need forward declaration for function signature
In rollDice you have many unnecessary local variables and I would use mt19937 random generator. So rollDice will look like this
void rollDice(Bar h[], int count = 100) {
default_random_engine en;
uniform_int_distribution<> dist{1,6};
for( int n = 0; n <= count; n++) {
cout << dist(en) << endl;
const auto index = dist(en) - 1;
++h[index].value;
}
};
In getHistogram you have return to_string(c); in switch therefore you don't see histogram. You can delete swith because you can match index and Side. And why cycle up to 100?
string getHistogram(Bar h[], char c = '*') {
stringstream ast;
for( int n = 0; n <= 5; n++) {
const string strValue = string(h[n].value, c);
switch (h[n].label)
{
case Side::ONE: ast << "One: "; break;
case Side::TWO: ast << "Two: "; break;
case Side::THREE: ast << "Three: "; break;
case Side::FOUR: ast << "Four: "; break;
case Side::FIVE: ast << "Five: "; break;
case Side::SIX: ast << "Six: "; break;
}
ast << strValue << '(' << h[n].value << ')' << endl;
}
return ast.str();
}
In main function you have to clear histogram before next rollDice
case 'r': case 'R':
for (Bar& b : histogram) {
b.value = 0;
}
rollDice(histogram, 100);
break;
Full version
#include <iostream>
#include <iomanip>
#include <string>
#include <random>
#include <sstream>
using namespace std;
enum class Side {
ONE, TWO, THREE, FOUR, FIVE, SIX
};
struct Bar {
int value;
Side label;
};
void rollDice(Bar h[], int count = 100) {
default_random_engine en;
uniform_int_distribution<> dist{1,6};
for( int n = 0; n <= count; n++) {
cout << dist(en) << endl;
const auto index = dist(en) - 1;
++h[index].value;
}
};
string getHistogram(Bar h[], char c = '*') {
stringstream ast;
for( int n = 0; n <= 5; n++) {
const string strValue = string(h[n].value, c);
switch (h[n].label)
{
case Side::ONE: ast << "One: "; break;
case Side::TWO: ast << "Two: "; break;
case Side::THREE: ast << "Three: "; break;
case Side::FOUR: ast << "Four: "; break;
case Side::FIVE: ast << "Five: "; break;
case Side::SIX: ast << "Six: "; break;
}
ast << strValue << '(' << h[n].value << ')' << endl;
}
return ast.str();
}
int main (){
Bar histogram[] = {
{0, Side::ONE},
{0, Side::TWO},
{0, Side::THREE},
{0, Side::FOUR},
{0, Side::FIVE},
{0, Side::SIX}
};
char choice;
do {
cout << "DICE ROLLING SIMULATION" << endl
<<"===============================" << endl
<< "r. Roll Dice" << endl
<< "h. Display histogram" << endl
<< "q. Quit program\n" << endl
<< "\nEnter your choice:" << endl;
// Reading a single character using the scanner
cin >> choice;
switch(choice) {
case 'r': case 'R':
for (Bar& b : histogram) {
b.value = 0;
}
rollDice(histogram, 100);
break;
case 'h': case 'H':
cout << getHistogram(histogram, '*') << endl;
break;
case 'q': case 'Q':
cout << "Good bye\n" << endl;
break;
default:
cout << "Invalid choice\n" << endl;
}
} while(choice != 'q');
}
Related
This code is working fine, however this whole time I've tried avoiding using the goto statements that you will see in the switch (dice_total) statement.
Without the goto statements, the program will not loop back to the beginning of while (again=='y' || again=='Y'), and instead it keeps looping itself when it reaches the do-while loop.
However, I believe that it is also important to say, that if dice_total is = to the point_total the first time around then the program will function properly, and loop back to the beginning. For example, when the program starts, the first round will generate the point_total, which we will say its 10. Which is a value that will allow the program to continue to the next round, and if the dice_total also gets the same number, 10, the program will say you win, and the loop will work properly. However, if the program reaches the do while loop, and generates a number that isn't 10, but generates a 10 after a few loops, then the program will not loop to the beginning. So what I want to ask, what is wrong with my switch(dice_total) statement, and how can I fix it, to give the program the same effect without using the goto statements?
#include "stdafx.h"
#include <iostream>
#include <random>
#include <string>
using namespace std;
int main()
{
//Declared Variables***********************************
char again = 'y';
int point1;
int point2;
int point_total;
int round_1=1;
int dice1;
int dice2;
int dice_total;
//*****************************************************
//RANDOM SEED******************************************
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int>dist(1, 6);
//*****************************************************
start://TEMPORARY
while (again == 'y'||again=='Y')
{
int round_1 = 1;
system("CLS");
cout << "WELCOME TO THE CRAPS GAME" << endl;
cout << "THROWING ROUND:" << round_1 << " DICES.............." << endl;
point1 = dist(mt);
point2 = dist(mt);
point_total = point1 + point2;
cout << "ROUND: " << round_1 << " First dice is: " << point1 << " and second dice is: " << point2 <<" and the total is:"<<point_total<< endl;
switch (point_total)
{
case 7:
case 11:
cout << "YOU WON CONGRATS PRESS Y TO PLAY AGAIN!!" << endl;
cin >> again;
break;
case 2:
case 3:
case 12:
cout << "YOU LOST, PRESS Y TO TRY AGAIN" << endl;
cin >> again;
break;
default:
do
{
++round_1;
cout << "ROUND " << round_1 << endl;
dice1 = dist(mt);
dice2 = dist(mt);
dice_total = dice1 + dice2;
cout << "THROWING ROUND: " << round_1 << " DICES.............." << endl;
cout << "ROUND 1 DICE TOTAL IS: " << point_total << endl;
cout << "ROUND: " << round_1 << " First dice is: " << dice1 << " and second dice is: " << dice2 << " and the total is:" << dice_total << endl;
switch (dice_total)
{
case 11:
cout << "YOU WON CONGRATS PRESS Y TO PLAY AGAIN!!" << endl;
cin >> again;
goto start;
case 2:
case 3:
case 7:
case 12:
cout << "YOU LOST, PRESS Y TO TRY AGAIN" << endl;
cin >> again;
goto start;
default:
if (dice_total == point_total)
{
cout << "YOU WON CONGRATS PRESS Y TO PLAY AGAIN!!<<endl;
cin >> again;
break;
}//if
else
{
cout << "Going to next round" << endl;
}
}//dice_total
}//do
while (dice_total != point_total);
break;
}//switch point
}//again while
}//main
The problem you're facing is usual when you have too many nested loops in the same function, and is an indicator that you need to refactor parts of your code to be in their own functions.
If you do this, then you have more possibilities to control the flow of your code: in each function you have break and return, and as you can return a custom value, you can use it to determine in the surrounding function if you need to break or return again.
Besides, this gives you the opportunity to put self-explanatory names to your functions, which makes your code clearer for people that look at it for the first time (as it's written, it's so dense that I can't understand it unless I stare at it for some minutes).
An example of what I mean in code:
Before
int main() {
start:
while (a) {
b1();
switch(c) {
case 1:
do {
d();
if (cond) goto start;
} while(e);
break;
}
b2();
}
}
After
int main() {
while (a) {
if (!doStuff1())
break;
}
...
}
bool doStuff1() {
b1();
while (a) {
bool res = doStuff2();
if (res) return true;
}
b2();
...
}
bool doStuff2() {
switch(c) {
case 1:
if (doStuff3()) return true;
}
return false;
}
bool doStuff3() {
do {
d();
if (cond) return true;
} while (e);
return false;
}
How about this design?
bool stop=false;
while(!stop && (again == 'y'||again=='Y'))
{
while(again == 'y'||again=='Y')
{
// ...
break; /* breaks inner while*/
// ...
stop=true;
break; /* breaks inner while, and prevents running outer loop*/
}
}
Novice C++ user trying to practice program building. The point of this program is just simple name storage with vectors.
My previous program https://pastebin.com/MG1hHzgK works perfectly fine for just adding first names.
This upgraded version is supposed to have an input of First Last names then it is converted into Last, First name before being added to the list.
My problem is that after I input names, they arent added to the list. The differences between my previous program and current one are all in the function addNames and to me it looks correct when its obviously not.
Any hints or help is greatly appreciated.
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Prototypes
string addNames(vector <string>& nameList);
string removeName(vector <string>& nameList);
int findName (vector <string>& nameList);
void showList(vector <string>& nameList);
void commandList(vector <string>& nameList);
void inputCall(vector <string>& nameList);
void sortList(vector <string>& nameList);
int main()
{
vector <string> nameList;
commandList(nameList);
}
void commandList(vector <string>& nameList)
{
cout << "\nPress any key to continue..." << endl;
getch();
system("cls");
cout << "Enter a Command " << endl;
cout << "<A> - Add names to the list" << endl;
cout << "<R> - Remove a name from the list" << endl;
cout << "<F> - Search for a name on the list" << endl;
cout << "<L> - Show current state of the list" << endl;
cout << "<S> - Sort the list" << endl;
cout << "<Q> - Ends the program" << endl;
inputCall(nameList);
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
string addNames(vector <string>& nameList)
{
string input;
int pos = input.find(' ');
nameList.clear();
for (;true;)
{
cout << endl;
cout << "Enter a Name or 'Stop' to end name entry: " << endl;
getline(cin, input);
if (input == "Stop" || input == "stop"){
commandList(nameList);
} else if(pos != -1) {
string first = input.substr(0, pos);
string last = input.substr(pos + 1);
input = last + "," + first;
nameList.push_back(input);
commandList(nameList);
}
}
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
string removeName(vector <string>& nameList)
{
string x;
cout << endl;
cout << "Enter the name to remove: " << endl;
cin >> x;
for (int i=0; i < nameList.size(); ++i) {
if (nameList[i]== x) nameList[i]="";
}
commandList(nameList);
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
int findName (vector <string>& nameList)
{
string target;
int i, x=0;
int p=0;
cout << endl;
cout << "Enter a name to search for: " << endl;
cin >> target;
if (target == "Quit" || target == "quit") {exit(0);
}
for (int i=0; i < nameList.size(); i++)
{
if (nameList[i] == target)
{
cout << endl;
cout << "The entered name is listed as #" << p+1 << '.' << endl;
commandList(nameList);
return p;
}
if (nameList[i] == "") {
p--;
}
p++;
}
cout << endl;
cout << "Name not found!" << endl;
commandList(nameList);
return -1;
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
void showList(vector <string>& nameList)
{
cout << endl;
cout << "The current state of the list is: " <<endl;
for (int i=0; i<nameList.size(); i++)
if(nameList[i] !="")
cout << nameList[i] << endl;
commandList(nameList);
}
void sortList(vector <string>& nameList)
{
string temp;
for (int i=0; i < nameList.size()-1; i++)
{
for (int j=0; j < (nameList.size()-i-1); j++)
{
if (nameList[j] > nameList[j+1])
{
temp = nameList[j];
nameList[j] = nameList[j+1];
nameList[j+1] = temp;
}
}
}
cout << endl;
cout << "The list has been sorted alphabetically." << endl;
commandList(nameList);
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
void inputCall(vector <string>& nameList) // Function to complement the menu for switch casing
{
bool running = true;
char input;
do {
input = getch();
switch(input)
{
case 'a': addNames(nameList);break;
case 'A': addNames(nameList);break;
case 's': sortList(nameList);break;
case 'S': sortList(nameList);break;
case 'l': showList(nameList);break;
case 'L': showList(nameList);break;
case 'f': findName(nameList);break;
case 'F': findName(nameList);break;
case 'r': removeName(nameList);break;
case 'R': removeName(nameList);break;
case 'q': exit(0);break;
case 'Q': exit(0);break;
default : cout << "Unknown Command: Enter a command from the menu." << endl; continue;
}
} while (running);
}
if you insert
pos = input.find(' '); in else { } just above ( if(pos != -1) )
Your code will work
I am new to c++ and and am working on a program that has is a simple dvd rental program. I am having issues with case 3 & 4 specifically. Maybe I am misunderstanding the purpose behind sizeof. What I am trying to have it do is tell if the char array is empty and if it is allow the user to check it out by putting their name in and if it is not available give them a response saying that it is not available. case 4 should do the opposite and allow them to check it in. Any suggestions would be greatly appreciated.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <limits>
using namespace std;
const int arrSize = 5;
struct dvdStruct //distance struct
{
int id;
char title[51] = { 0 };
char rating[5] = { 0 };
double price;
char borrower[51] = { 0 };
} dvd;
dvdStruct dvds[arrSize] = {};
int userSelection; //intput variable for main menu selection
int borrowId (0);
int borrowIdReturn(0);
//void initalize();
int main() {
int size(0);
dvds[0].id = 1;
dvds[1].id = 2;
dvds[2].id = 3;
dvds[3].id = 4;
dvds[4].id = 5;
strcpy(dvds[0].title, "Fast 1");
strcpy(dvds[1].title, "Fast 2");
strcpy(dvds[2].title, "Fast 3");
strcpy(dvds[3].title, "Fast 4");
strcpy(dvds[4].title, "Fast 5");
strcpy(dvds[0].rating, "PG - 13");
strcpy(dvds[1].rating, "PG - 13");
strcpy(dvds[2].rating, "PG - 13");
strcpy(dvds[3].rating, "PG - 13");
strcpy(dvds[4].rating, "PG - 13");
dvds[0].price = '19.1';
dvds[1].price = '19.2';
dvds[2].price = '19.3';
dvds[3].price = '19.4';
dvds[4].price = '19.5';
strcpy(dvds[0].borrower, "");
cout << strlen(dvds[0].borrower) << endl;
strcpy(dvds[1].borrower, "\0");
strcpy(dvds[2].borrower, "\0");
strcpy(dvds[3].borrower, "\0");
strcpy(dvds[4].borrower, "\0");
do {
cout << "1.Display All DVD’s" << endl << "2.Display DVD Detail" << endl << "3.Check Out a DVD" << endl << "4.Check In a DVD" << endl << "5.Exit" << endl;
cin >> userSelection; //Input from the user.
switch (userSelection)
{
case 1:
for (int i = 0; i < arrSize; i++)
{
std::cout << dvds[i].title << "' " << dvds[i].rating << " " << dvds[i].borrower << endl;
}
system("pause");
system("CLS");
break;
case 2:
int dvdNum;
cout << "Enter a DVD number:";
cin >> dvdNum;
std::cout << dvds[dvdNum - 1].title << "' " << dvds[dvdNum - 1].rating << endl;
system("pause");
system("CLS");
break;
case 3:
cout << "Enter and id:";
cin >> borrowId;
if (strlen(dvds[borrowId-1].borrower) == 0)
{
cout << "Enter your name: ";
cin >> dvds[borrowId-1].borrower;
}
else
{
cout << "This dvd is not available" << endl;
}
system("pause");
system("CLS");
break;
case 4:
cout << "Enter and id:";
cin >> borrowIdReturn;
if (strlen(dvds[borrowIdReturn - 1].borrower) == 0)
{
cout << "This dvd is available" << endl;
}
else
{
cout << "Your DVD has been returned " << endl;
strcpy(dvds[borrowIdReturn - 1].borrower, "\0");
}
system("pause");
system("CLS");
break;
case 5:
return 0;
break;
}
} while (userSelection == 1 || userSelection == 2 || userSelection == 3 || userSelection == 4);
}
sizeof() gives you the size of an object. The size of the object is always the same, no matter what's in the object. In fact, sizeof() is calculated at compile time, and its value could not be affected, in any way, by whatever happens at runtime.
C++ code should use std::string, instead of char arrays, in most cases. std::string's empty() method indicates whether the string is empty.
If you still insist on working with C-style char arrays, and C-style '\0' terminated strings, use the C strlen() function to check if the character array contains nothing but a leading '\0', indicating an empty string.
I'm trying to get the user to choose a team (names are contained in 2D array), type the desired name/team, then use a for loop to compare the user's typed word with the words of a 2D array, and use that loop to access each string in the array:
#include <string>
#include <iostream>
#include <string.h>
int main(int argc, char* argv[]){
char cPlaychar[10][15] = { "BlackFurs", "EppiGods", "FairyDusters",
"Dwarvin", "Bloods", "Cryptics", "ArcAngels",
"DarkVillians", "Heroiteks", "Mass", };
char cKeyInput[15];
int results[10];
std::cout << "Please select a character
by typing the name and pressing enter" << std::endl;
std::cin >> cKeyInput;
for(int Array = 0; Array < 10; Array++){
switch (Array){
case '0':
results[0] = strcmp(cPlaychar[Array], cKeyInput);
if (results[0] = 0){
std::cout << "you have picked the first char";
}
break;
case '1': results[1] = strcmp(cPlaychar[Array], cKeyInput);
if (results[1] = 0){
std::cout << "you have picked the secound char";
}
break;
case '2':results[2] = strcmp(cPlaychar[Array], cKeyInput);
if (results[2] = 0){
std::cout << "you have picked the third char";
}
break;
case '3':results[3] = strcmp(cPlaychar[Array], cKeyInput);
if (results[3] = 0){
std::cout << "you have picked the fourth char";
}
break;
case '4':results[4] = strcmp(cPlaychar[Array], cKeyInput);
if (results[4] = 0){
std::cout << "you have picked the fith char";
}
break;
case '5':results[5] = strcmp(cPlaychar[Array], cKeyInput);
if (results[5] = 0){
std::cout << "you have picked the sixth char";
}
break;
case '6':results[6] = strcmp(cPlaychar[Array], cKeyInput);
if (results[6] = 0){
std::cout << "you have picked the seventh char";
}
break;
case '7':results[7] = strcmp(cPlaychar[Array], cKeyInput);
if (results[7] = 0){
std::cout << "you have picked the eighth char";
}
break;
case '8':results[8] = strcmp(cPlaychar[Array], cKeyInput);
if (results[8] = 0){
std::cout << "you have picked the ninth char";
}
break;
case '9':results[9] = strcmp(cPlaychar[Array], cKeyInput);
if (results[9] = 0){
std::cout << "you have picked the tenth char";
}
break;
} // end of switch
} // end of for
system("pause");
return 0;
}
first you can put
using namespace std ;
in the beginning and remove all (std::)
second in if (results[ ] = 0) this must be if (results[ ] == 0)
third i don't know why you use this method , you can do it easily like this
string names[] = {"a","b"} ;
string num[] = { "first", "second" } ;
string input;
cin >> input;
for (int i = 0; i < 2; ++i)
{
if (input == names[i])
cout << "you choose" << num[i] << endl;
}
but if you want 2d array you can do this
char names[2][2] = { "a", "b" };
char num[2][7] = { "first", "second" };
char input[2];
cin >> input ;
for (int i = 0; i < 2; ++i)
{
if (!strcmp(input,names[i]))
cout << "you choose " << num[i] << endl;
}
Trying to make the string "Abbot" the [1] slot of my basePlayer array. Basically they select the player in the switch and then the player string will be stored in array.
line 118
Help would be SUPER appreciated, program will run if you copy, paste, compile, and run.
Thank you for taking a look!!
#include <iostream>
#include <string>
#include <windows.h>
#include <cmath>
#include <cstring>
#include <cstdlib>
using namespace std;
int main(){
Beep(251.63,100);
Beep(329.63,100);
Beep(392,100);
Beep(251.63,100);
Beep(329.63,100);
Beep(392,100);
Beep(251.63,100);
Beep(329.63,100);
Beep(392,100);
string playerList[6] = {"Abbot", "Seer", "Hellion", "Vagabond", "Knave","##QUIT##"};
string cityList[4] = {"city1","city2","city3","city4"};
string spiritList[4] = {"spirit1","spirit2","spirit3","spirit4",};
string yesNo[2] = {"yes","no"};
string name;
string player;
string city;
string spirit;
string basePlayer[4] = {name, player, city, spirit};
int pointer = 0;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
cout << "What is your name?" << endl;
cin >> name;
cout << "your name is " << name << "?" << endl;
basePlayer[0] = name;
while(true){
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
cout << basePlayer[0]<<" please choose your Adventurer:\n*tab* for description\n\n";
for (int row = 0; row < 6; ++row){
if(row == pointer){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
cout << playerList[row] << endl;}
else{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
cout << playerList[row] << endl;}}
while(true){
if (GetAsyncKeyState(VK_UP) != 0){
Beep(800,50);
pointer -= 1;
if (pointer == -1){
pointer = 0;}
break;}
else if (GetAsyncKeyState(VK_DOWN) != 0){
Beep(800,50);
pointer += 1;
if (pointer == 6){
pointer = 0;}
break;}
else if (GetAsyncKeyState(VK_TAB) != 0){
Beep(1200,50);
Beep(1000,50);
switch(pointer){
case 0:{
system("cls");
cout << "AbbotFacts.\n\n*RightArrow* to become the Abbot\n*ArrowDown* to return to TitleScreen";
Sleep(1000);
}
break;
case 1:{
system("cls");
cout << "SeerFacts.\n\n*RightArrow* to become the Sear\n*ArrowDown* to return to TitleScreen";
Sleep(1000);
}
break;
case 2:{
system("cls");
cout << "HellionFacts.\n\n*RightArrow* to become the Hellion\n*ArrowDown* to return to TitleScreen";
Sleep(1000);
}
break;
case 3:{
system("cls");
cout << "VagabondFacts.\n\n*RightArrow* to become the Vagabond\n*ArrowDown* to return to TitleScreen";
Sleep(1000);
}
break;
case 4:{
system("cls");
cout << "KnaveFacts.\n\n*RightArrow* to become the Knave\n*ArrowDown* to return to TitleScreen";
Sleep(1000);
}
break;
case 5:{return 0;}
break;
break;}}
else if (GetAsyncKeyState(VK_RIGHT) != 0){
Beep(1000,50);
Beep(1200,50);
switch(pointer){
case 0:{
system("cls");
player = "Abbot";
cout << basePlayer[0] << ", You have chosen the "<< basePlayer[1] << endl;
Sleep(1000);
}
break;
case 1:{
system("cls");
cout << "You have chosen the Seer"<< endl;
player="Seer";
cout << basePlayer[1] << endl;
Sleep(1000);
}
break;
case 2:{
system("cls");
cout << "You have chosen the Hellion"<< endl;
player="Hellion";
cout << basePlayer[1] << endl;
Sleep(1000);
}
break;
case 3:{
system("cls");
cout << "You have chosen the Vagabond" << endl;
player="Vagabond";
cout << basePlayer[1] << endl;
Sleep(1000);
}
break;
case 4:{
system("cls");
cout << "You have chosen the Knave"<< endl;
player="Knave";
cout << basePlayer[1] << endl;
Sleep(1000);
}
break;
case 5:{return 0;
break;}
break;}}
}
Sleep(150);
}
}
What you have to do is assign player to basePlayer[1] after setting the player value.
switch(pointer){
case 0:{
player = "Abbot";
basePlayer[1] = player;
cout << "You have chosen the "<< basePlayer[1] << endl;
I advise you have a read up on how character arrays work in C++, and memory allocation.
If you want a simpler solution, you might like the std string library, which handles most of the memory and copying side for you.
I seriously think you might want to try something simpler first, or at least look in to an oop design, which might be simpler in this case.
It would be much, much better to store this data in a structure rather than an array. This would allow you to group other player data that may not be represented as a string.
struct PlayerData
{
std::string m_Name;
std::string m_Player;
std::string m_City;
std::string m_Spirit;
int m_MaxHealth;
};
...
PlayerData basePlayer;
basePlayer.m_Name = playerList[playerSelection];
Better yet why not have a class store this data, allowing you to control access to it and also perform operations with the data.
class Player
{
public:
Player( const std::string& name, const std::string& player, const std::string& city, const std::string& spirit );
virtual ~Player();
std::string GetName() const;
void ReduceHealth( int damage );
private:
std::string m_Name;
std::string m_Player;
std::string m_City;
std::string m_Spirit;
int m_MaxHealth;
};
You may want to read up more on C++ itself and it's features before attempting a to write a whole game.