How do I use rand() with an enum? - c++

Hello I'm beginning to learn c++ , I don't understand how enum works that well and I need help knowing how can I make the rand() working with enum
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
cout << "First part : Create an item. " << endl;
int choice;
int Fire = 25;
int Water = 23;
int Wind = 24;
int Earth = 20;
int WeaponNature = 0;
enum NatureWeapons { Fire, Water, Wind, Earth}; // enum here if its wrong pls let me know ):
cout << "Enter the nature of weapon you want : " << endl;
cout << " 1 - Fire " << endl;
cout << " 2 - Water " << endl;
cout << " 3 - Wind " << endl;
cout << " 4 - Earth" << endl;
cin >> choice;
switch(choice)
{
case 1:
cout << "You picked fire."
cout << " Power : " << Fire << endl;
WeaponNature = Fire;
break;
case 2:
cout << "You picked water." << endl;
cout << " Power : " << Water << endl;
WeaponNature = Water;
break;
case 3:
cout << "You picked wind nature." << endl;
cout << " Power : " << Wind << endl;
WeaponNature = Wind;
break;
case 4:
cout << "You picked earth nature." << endl;
cout << " Power : " << Earth << endl;
WeaponNature = Earth;
break;
default:
cout << "Incorrect input. Your weapon will be : " << rand() // this is where i need help
}
}
When the default: runs in the switch() i wanted it to choose a random nature with rand(), please any help ): ?

As take from http://www.cprogramming.com/tutorial/enum.html:
Printing Enums
You might wonder what happens when you print out an enum: by default, you'll get the integer value of the enum. If you want to do something fancier than that, you'll have to handle it specially.
You would have to create another switch block converting the random integer into the proper value from the enum. Also, initialise a seed for your random number generator via srand( time( NULL ) )
Assign the rand value to WeaponNature as well.

Related

Replace if else with Switch statement

I am working on the code below and trying to use switch statement instead of if/else.
The problem is that I cannot figure out how to get the switch to work.
A few things a tried:
I know that every time an expression is equal to the case constant, the code then will be executed. Example:
switch (expression)
{
case 1:
// code to be executed if
// expression is equal to 1;
break;
}
My code below has a similar concept, but I cannot get it display the calculation. There are no errors, but it does not display the total price.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
const int CHEESE_PIZZA = 11;
const int SPINACH_PIZZA = 13;
const int CHICKEN_PIZZA = 14;
cout << " *********** MENU ***********" << endl;
cout << setw (9) << "ITEM" << setw (20) << "PRICE" << endl;
cout << " (1) Cheese Pizza" << setw (8) << "$"
<< CHEESE_PIZZA << endl;
cout << " (2) Spinach Pizza" << setw (7) << "$"
<< SPINACH_PIZZA << endl;
cout << " (3) Chicken Pizza" << setw (7) << "$"
<< CHICKEN_PIZZA << endl;
cout << endl;
cout << "What would you like? ";
int option;
cin >> option;
cout << "You picked pizza option " << option << endl;
cout << "How many orders? ";
int quantity;
cin >> quantity;
cout << "You choose quantity " << quantity << endl;
int price;
switch (option)
{
case 1:
price = CHEESE_PIZZA;
break;
case 2:
price = SPINACH_PIZZA;
break;
case 3:
price = CHICKEN_PIZZA;
break;
default:
cout << "Please select valid item from menu. " << endl;
}
return 1;
int amount = price * quantity;
cout << "Your Bill: $ " << amount << endl;
cout << endl;
return 0;
}
I am confused about the output for any input other than 1, 2, and 3 in case 4.
The if/else statement works:
int price;
if (option == 1) price = CHEESE_PIZZA;
else if (option == 2) price = SPINACH_PIZZA;
else if (option == 3) price = CHICKEN_PIZZA;
else {
cout << "Please select valid item from menu. " << endl;
return 1;
}
The problem, from eyeballing, appears to be because you return 1 outside the switch statement, which is why the code after it never gets run.
You should replace your case 4 with a default: label and move the return 1; into that case as well as add a break statement under case 3.

c++ cast int to double not working

int main(){
srand(time(0));
int numOfTimes;
int randNum;
int oneRoll = 0, twoRoll = 0, threeRoll = 0, fourRoll = 0, fiveRoll = 0, sixRoll = 0;
int onePercent, twoPercent, threePercent, fourPercent, fivePercent, sixPercent;
int count = 0;
cout << "How many times would you like to roll the dice?\n";
cin >> numOfTimes;
while (numOfTimes <= 0){
cout << "Invalid entry enter a number greater than 0\n";
cout << "How many times would you like to roll the dice?\n";
cin >> numOfTimes;
}
while (count < numOfTimes)
{
randNum = rand() % 6 + 1;
switch (randNum)
{
case 1:
oneRoll++;
break;
case 2:
twoRoll++;
break;
case 3:
threeRoll++;
break;
case 4:
fourRoll++;
break;
case 5:
fiveRoll++;
break;
case 6:
sixRoll++;
break;
default:
cout << "\n";
}
count++;
}
onePercent = (int)((oneRoll*100.0) /numOfTimes);
twoPercent = (int)((twoRoll*100.0) / numOfTimes);
cout << " # Rolled # Times % Times" << endl;
cout << "--------- -------- --------" << endl;
cout << "1 " << oneRoll << " " <<double (onePercent) << endl;
cout << "2 " << twoRoll << " " << "" << endl;
cout << "3 " << threeRoll << " " << ""<< endl;
cout << "4 " << fourRoll << " " <<"" << endl;
cout << "5 " << fiveRoll << " " <<"" << endl;
cout << "6 " << sixRoll << " " <<"" << endl;
I need it to print out the the one percent as a double. So I converted it as an int then to a double so it only prints two zeros like this (14.00) but its not converting at all its only printing 14
The main problem, just as Barmar mentioned in his comment, is that although you want the value to be printed to 2 decimal points, you round off the number in onePercent when you do:
onePercent = (int)((oneRoll*100.0) /numOfTimes); // Casting to "int" rounds off the number
Also, the declared data type for onePercent is int from the start:
int onePercent, twoPercent, threePercent, fourPercent, fivePercent, sixPercent; // onePercent is an "int" here
So you don't need a typecast of int, because you're casting an int to an int.
Therefore, even if you print onePercentwith 2 decimal-point precision, you will always get .00 as a result.
I would recommend taking off the (int) cast from that expression itself, and changing the initial data type of onePercent to type double. If you do not want to change the data types for the other variables declared alongside onePercent, then declare onePercent as a double on another line. That way, the precision of the value after the calculation will be maintained, and you will be able to output it to 2 decimal places.
As an aside, to specify the number of decimal places to output, the setprecision() function can be used:
cout << setprecision(2) << ... << endl; // The value passed to "setprecision" is up to you.

C++ logic error

I got 158, 1000, and 140 for the money outputs while I played.
The original amount I put in was 100.
The total amount of money and the amount of money entered by the user aren't showing up correctly when ran.
Sometimes, the total and the amount entered displays correctly, it should be noted.
But not always, and that's a problem.
There is some logic error(s) I can't figure out. Help?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <stdlib.h>
using namespace std;
void switchStatementsCalculations (int &slot1, int &slot2, int &slot3, string cherries, string
oranges, string plums, string bells, string melons, string bars);
void calculateAmountEarnedByPlaying (double &money, int slot1, int slot2, int slot3,
double &total);
int main()
{
int slot1;
int slot2;
int slot3;
double money=0;
double total=0;
double amountOfMoneyEnterd=0;
int count;
string cherries = "cherries";
string oranges = "oranges";
string plums = "plums";
string bells = "bells";
string melons = "melons";
string bars = "bars";
string doAgain;
cout << "We are going to be playing a slot machine game today." << endl;
srand(time(0));
do
{
cout << "Please enter the amount of money you'd like to insert into the slot machine. We will pull the lever for you." << endl;
cin >> money;
cout << "You put in $" << money << endl;
slot1=rand()%6+1;
slot2=rand()%6+1;
slot3=rand()%6+1;
switchStatementsCalculations(slot1, slot2, slot3, cherries, oranges, plums, bells, melons, bars);
calculateAmountEarnedByPlaying(money, slot1, slot2, slot3, total);
amountOfMoneyEnterd=(amountOfMoneyEnterd+money);
cout << "Would you like to play again? Please type yes if so." << endl;
cin >> doAgain;
if(doAgain!= "yes")
{
cout << "The total amount of money you put in the slot machine is " << amountOfMoneyEnterd << endl;
cout << "The total amount of money you won is $" << total << endl;
}
}
while(doAgain=="yes");
system("Pause");
return 0;
}
void switchStatementsCalculations(int &slot1, int &slot2, int &slot3, string cherries, string
oranges, string plums, string bells, string melons, string bars)
{
switch (slot1)
{
case 1:
cout << "You got " << cherries << endl;
case 2:
cout << "You got " << oranges << endl;
break;
case 3:
cout << "You got " << plums << endl;
break;
case 4:
cout << "You got " << bells << endl;
break;
case 5:
cout << "You got " << melons << endl;
break;
case 6:
cout << "You got " << bars << endl;
}
switch (slot2)
{
case 1:
cout << "You got " << cherries << endl;
break;
case 2:
cout << "You got " << oranges << endl;
break;
case 3:
cout << "You got " << plums << endl;
break;
case 4:
cout << "You got " << bells << endl;
break;
case 5:
cout << "You got " << melons << endl;
break;
case 6:
cout << "You got " << bars << endl;
}
switch (slot3)
{
case 1:
cout << "You got " << cherries << endl;
break;
case 2:
cout << "You got " << oranges << endl;
break;
case 3:
cout << "You got " << plums << endl;
break;
case 4:
cout << "You got " << bells << endl;
break;
case 5:
cout << "You got " << melons << endl;
break;
case 6:
cout << "You got " << bars << endl;
}
}
void calculateAmountEarnedByPlaying(double &money, int slot1, int slot2, int slot3, double &total)
{
double won=0;
if(slot1==slot2 || slot1==slot3 || slot2==slot3)
{
cout << "Congratulations! You won." << endl;
won=(money * 2);
cout << "You won " << won << endl;
}
else if ((slot1==slot2 && slot1==slot3) || (slot2==slot1 && slot2==slot3) || (slot3==slot1 && slot3==slot2))
{
cout << "Congratulations! You won." << endl;
won=(money*3);
cout << "You won " << won << endl;
}
else
{
cout << "You didn't earn any money." << endl;
}
total=(total+won);
}
One mistake is this in your calculateAmountEarnedByPlaying function:
double won;
You did not initialize this variable, thus it contains an indeterminate value.
It is only set if you've won something, but not set if you didn't win. You then do this at the end of your function:
total = (total + won);
If won is not initialized, then total will also be set to an indeterminate value (or undefined behavior occurs).
The won variable should be initialized to 0:
double won = 0;
Most compilers give a warning if you access an uninitialized variable like this. Please check that you have your warnings on to detect these issues.
The other issue is that you forgot a break in your switch statements:
switch (slot1)
{
case 1:
cout << "You got " << cherries << endl; // no break statement
case 2:
cout << "You got " << oranges << endl;
break;
If slot1 is 1, it will print both sets of output for case 1 and case 2.
None of this would be necessary if you used arrays instead of 6 separate variables and 6 separate lines of output:
std::string slot_items[] = {"cherries", "oranges", "plums", "bells", "melons", "bars"};
//...
int slots[3];
//...
for (int i = 0; i < 3; ++i)
slots[i] = rand()%6+1;
//...
// inside the function...
//...
for (int i = 0; i < 3; ++i)
cout << "You got " << slot_items[slots[i]] << endl;
A two line for loop replaces 60 lines of switch and case statements.

(C++) Goto statement not working. Beginner [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm making a USD to MXN converter and I want to have it work both ways. The if statement works (tryed cout << "test"; and it worked) but it wont work when I replace it with the goto statement.
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int user;
int u, m;
cout << "US/MXN Converter" << endl;
cout << "1 US = 12.99 MXN (6/12/2014)" << endl;
cout << endl;
cout << "What Way to convert" << endl;
cout << "[1] US to MXN" << endl;
cout << "[2] MXN to US" << endl;
cout << "Selection: ";
cin >> user;
if (user == 1)
{
goto USTMXN;
}
else
{
goto MXNTUS;
}
USTMXN:
cout << "Enter the amount of US Dollars to Convert" << endl;
cout << "Amount: ";
cin >> u;
m = u * 12.99;
cout << endl;
cout << "MXN Pesos: " << m << endl;
goto END;
MXNTUS:
int mm, uu;
cout << "Enter the amount of Pesos to Convert" << endl;
cout << "Amount: ";
cin >> mm;
uu = mm / 12.99;
cout << endl;
cout << "US Dollars: " << m << endl;
goto END;
END:
system("PAUSE");
return EXIT_SUCCESS;
}
One of the most fundamental things we have to do as programmers is to learn to break problems into smaller problems. You are actually running into a whole series of problems.
I'm going to show you how to solve your problem. You may want to book mark this answer, because I'm pre-empting some problems you're going to run into a few steps down the line and preparing you - if you pay attention - to solve them on your own ;)
Let's start by stripping down your code.
Live demo here: http://ideone.com/aUCtmM
#include <iostream>
int main()
{
std::cout << "Enter a number: ";
int i;
std::cin >> i;
std::cout << "Enter a second number: ";
int j;
std::cin >> j;
std::cout << "i = '" << i << "', j = '" << j << "'\n";
}
What are we checking here? We're checking that we can ask the user two questions. That works fine.
Next is your use of goto, which I strongly recommend you do not use. It would be better to use a function. I'll demonstrate with your goto case here first:
#include <iostream>
int main()
{
int choice;
std::cout << "Enter choice 1 or 2: ";
std::cin >> choice;
if ( choice == 1 )
goto CHOSE1;
else if ( choice == 2 )
goto CHOSE2;
else {
std::cout << "It was a simple enough question!\n";
goto END;
}
CHOSE1:
std::cout << "Chose 1\n";
goto END;
CHOSE2:
std::cout << "Chose 2\n";
goto END;
END:
std::cout << "Here we are at end\n";
}
live demo: http://ideone.com/1ElcV8
So goto isn't the problem.
That leaves your use of variables. You've really mixed things up nastily by having a second set of variables (mm, uu). Not only do you not need to have these, you're doing something very naughty in that these variables only exist inside one scope and not the other. You can "get away" with this but it will come back to haunt you later on.
The difference in your two main streams of code is the variable names. The second conversion case looks like this:
MXNTUS:
int mm, uu;
cout << "Enter the amount of Pesos to Convert" << endl;
cout << "Amount: ";
cin >> mm;
uu = mm / 12.99;
cout << endl;
cout << "US Dollars: " << m << endl;
goto END;
The problem here is that you have - accidentally - used the variable "m" in your output. It's what we call uninitialized.
cout << "US Dollars: " << m << endl;
That m in the middle should be mm.
Your compiler should actually be warning you about this. If it's not, and you're just setting out learning, you should figure out how to increase the compiler warning level.
It would be better to make a function to do the conversions; you could make one function for each direction, but I've made a function that handles both cases:
#include <iostream>
static const double US_TO_MXN = 12.99;
static const char DATA_DATE[] = "6/12/2014";
void convert(const char* from, const char* to, double exchange)
{
std::cout << "Enter the number of " << from << " to convert to " << to << ".\n"
"Amount: ";
int original;
std::cin >> original;
std::cout << to << ": " << (original * exchange) << '\n';
}
int main() // this is valid since C++2003
{
std::cout << "US/MXN Converter\n"
"1 US = " << US_TO_MXN << " MXN (" << DATA_DATE << ")\n"
"\n";
int choice = 0;
// Here's a better demonstration of goto
GET_CHOICE:
std::cout << "Which conversion do you want to perform?\n"
"[1] US to MXN\n"
"[2] MXN to US\n"
"Selection: ";
std::cin >> choice;
if (choice == 1)
convert("US Dollars", "Pesos", US_TO_MXN);
else if (choice == 2)
convert("Pesos", "US Dollars", 1 / US_TO_MXN);
else {
std::cerr << "Invalid choice. Please try again.\n";
goto GET_CHOICE;
}
// this also serves to demonstrate that goto is bad because
// it's not obvious from the above that you have a loop.
}
ideone live demo: http://ideone.com/qwpRtQ
With this, we could go on to clean things up a whole bunch and extend it:
#include <iostream>
using std::cin;
using std::cout;
static const double USD_TO_MXN = 12.99;
static const double GBP_TO_MXN = 22.03;
static const char DATA_DATE[] = "6/12/2014";
void convert(const char* from, const char* to, double exchange)
{
cout << "Enter the number of " << from << " to convert to " << to << ".\n"
"Amount: ";
int original;
cin >> original;
cout << '\n' << original << ' ' << from << " gives " << int(original * exchange) << ' ' << to << ".\n";
}
int main() // this is valid since C++2003
{
cout << "Foreign Currency Converter\n"
"1 USD = " << USD_TO_MXN << " MXN (" << DATA_DATE << ")\n"
"1 GBP = " << GBP_TO_MXN << " MXN (" << DATA_DATE << ")\n"
"\n";
for ( ; ; ) { // continuous loop
cout << "Which conversion do you want to perform?\n"
"[1] USD to MXN\n"
"[2] MXN to USD\n"
"[3] GBP to MXN\n"
"[4] MXN to GBP\n"
"[0] Quit\n"
"Selection: ";
int choice = -1;
cin >> choice;
cout << '\n';
switch (choice) {
case 0:
return 0; // return from main
case 1:
convert("US Dollars", "Pesos", USD_TO_MXN);
break;
case 2:
convert("Pesos", "US Dollars", 1 / USD_TO_MXN);
break;
case 3:
convert("British Pounds", "Pesos", GBP_TO_MXN);
break;
case 4:
convert("Pesos", "British Pounds", 1 / GBP_TO_MXN);
break;
default:
cout << "Invalid selection. Try again.\n";
}
}
}
http://ideone.com/iCXrpU
There is a lot more room for improvement with this, but I hope it helps you on your way.
---- EDIT ----
A late tip: It appears you're using visual studio, based on the system("PAUSE"). Instead of having to add to your code, just use Debug -> Start Without Debugging or press Ctrl-F5. It'll do the pause for you automatically :)
---- EDIT 2 ----
Some "how did you do that" points.
cout << '\n' << original << ' ' << from << " gives " << int(original * exchange) << ' ' << to << ".\n";
I very carefully didn't do the using namespace std;, when you start using more C++ that directive will become the bane of your existence. It's best not to get used to it, and only let yourself start using it later on when you're a lot more comfortable with C++ programming and more importantly debugging odd compile errors.
But by adding using std::cout and using std::cin I saved myself a lot of typing without creating a minefield of function/variable names that I have to avoid.,
What does the line do then:
cout << '\n' << original << ' ' << from << " gives " << int(original * exchange) << ' ' << to << ".\n";
The '\n' is a single character, a carriage return. It's more efficient to do this than std::endl because std::endl has to go poke the output system and force a write; it's not just the end-of-line character, it actually terminates the line, if you will.
int(original * exchange)
This is a C++ feature that confuses C programmers. I'm actually creating a "temporary" integer with the result of original * exchange as parameters.
int i = 0;
int i(0);
both are equivalent, and some programmers suggest it is better to get into the habit of using the second mechanism so that you understand what happens when you later run into something called the "most vexing parse" :)
convert("Pesos", "British Pounds", 1 / GBP_TO_MXN)
The 1 / x "invert"s the value.
cout << "Foreign Currency Converter\n"
"1 USD = " << USD_TO_MXN << " MXN (" << DATA_DATE << ")\n"
"1 GBP = " << GBP_TO_MXN << " MXN (" << DATA_DATE << ")\n"
"\n";
This is likely to be confusing. I'm mixing metaphors with this and I'm a little ashamed of it, but it reads nicely. Again, employ the concept of breaking problems up into smaller problems.
cout << "Hello " "world" << '\n';
(note: "\n" and '\n' are different: "\n" is actually a string whereas '\n' is literally just the carriage return character)
This would print
Hello world
When C++ sees two string literals separated by whitespace (or comments) like this, it concatenates them, so it actually passes "Hello world" to cout.
So you could rewrite this chunk of code as
cout << "Foreign Currency Converter\n1 USD = ";
cout << USD_TO_MXN;
cout << " MXN (";
cout << DATA_DATE;
cout << ")\n1 GBP = ";
cout << GBP_TO_MXN;
cout << " MXN (";
cout << DATA_DATE;
cout << ")\n\n";
The << is what we call "semantic sugar". When you write
cout << i;
the compiler is translating this into
cout.operator<<(i);
This odd-looking function call returns cout. So when you write
cout << i << j;
it's actually translating it to
(cout.operator<<(i)).operator<<(j);
the expression in parenthesis (cout.operator<<(i)) returns cout, so it becomes
cout.operator<<(i); // get cout back to use on next line
cout.operator<<(j);
Main's fingerprint
int main()
int main(int argc, const char* argv[])
Both are legal. The first is perfectly acceptable C or C++. The second is only useful when you plan to capture "command line arguments".
Lastly, in main
return 0;
Remember that main is specified as returning int. The C and C++ standards make a special case for main that say its the only function where it's not an error not to return anything, in which case the program's "exit code" could be anything.
Usually its best to return something. In C and C++ "0" is considered "false" while anything else (anything that is not-zero) is "true". So C and C++ programs have a convention of returning an error code of 0 (false, no error) to indicate the program was successful or exited without problems, or anything else to indicate (e.g. 1, 2 ... 255) as an error.
Using a "return" from main will end the program.
Try to change youre code for sth like this. Using goto label is not recommended.
Main idea of switch statement :
int option;
cin >> option
switch(option)
{
case 1: // executed if option == 1
{
... code to be executed ...
break;
}
case 99: //executed id option == 99
{
... code to be executed
break;
}
default: // if non of above value was passed to option
{
// ...code...
break;
}
}
Its only example.
int main(int argc, char *argv[])
{
int user;
int u, m;
cout << "US/MXN Converter" << endl;
cout << "1 US = 12.99 MXN (6/12/2014)" << endl;
cout << endl;
cout << "What Way to convert" << endl;
cout << "[1] US to MXN" << endl;
cout << "[2] MXN to US" << endl;
cout << "Selection: ";
cin >> user;
switch(user )
{
case 1 :
{
//USTMXN:
cout << "Enter the amount of US Dollars to Convert" << endl;
cout << "Amount: ";
cin >> u;
m = u * 12.99;
cout << endl;
cout << "MXN Pesos: " << m << endl;
break;
}
}
default :
{
//MXNTUS:
int mm, uu;
cout << "Enter the amount of Pesos to Convert" << endl;
cout << "Amount: ";
cin >> mm;
uu = mm / 12.99;
cout << endl;
cout << "US Dollars: " << m << endl;
break;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}

program crashes at CIN input | C++

so i made a DOS program however my game always crashes on my second time running to the cin function.
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
//call functions
int create_enemyHP (int a);
int create_enemyAtk (int a);
int find_Enemy(int a);
int create_enemyDef (int a);
// user information
int userHP = 100;
int userAtk = 10;
int userDef = 5;
string userName;
//enemy Information
int enemyHP;
int enemyAtk;
int enemyDef;
string enemies[] = {"Raider", "Bandit", "Mugger"};
int sizeOfEnemies = sizeof(enemies) / sizeof(int);
string currentEnemy;
int chooseEnemy;
// ACTIONS
int journey;
int test;
int main()
{
// main menu
cout << "welcome brave knight, what is your name? " ;
cin >> userName;
cout << "welcome " << userName << " to Darland" << endl;
//TRAVELING
MENU:
cout << "where would you like to travel? " << endl;
cout << endl << " 1.> Theives Pass " << endl;
cout << " 2.> Humble Town " << endl;
cout << " 3.> Mission HQ " << endl;
cin >> journey;
if (journey == 1)
{
// action variable;
string c_action;
cout << "beware your journey grows dangerous " << endl;
//begins battle
// Creating the enemy, HP ATK DEF AND TYPE. ;
srand(time(0));
enemyHP = create_enemyHP(userHP);
enemyAtk = create_enemyAtk(userAtk);
enemyDef = create_enemyDef(userDef);
chooseEnemy = find_Enemy(sizeOfEnemies);
currentEnemy = enemies[chooseEnemy];
cout << " Here comes a " << currentEnemy << endl;
cout << "stats: " << endl;
cout << "HP :" << enemyHP << endl;
cout << "Attack : " << enemyAtk << endl;
cout << "Defense : " << enemyDef << endl;
ACTIONS:
cout << "Attack <A> | Defend <D> | Items <I>";
cin >> c_action;
//if ATTACK/DEFEND/ITEMS choice
if (c_action == "A" || c_action == "a"){
enemyHP = enemyHP - userAtk;
cout << " you attack the enemy reducing his health to " << enemyHP << endl;
userHP = userHP - enemyAtk;
cout << "however he lashes back causing you to have " << userHP << "health left " << endl;
//end of ATTACK ACTION
}
the last line "cin >> c_action crashes. i use two other pages. they just create the functions. is it a complier issue. also why does my complier always shutdown after it runs he app. is there a way to stop it?
A few hints:
I never use forward declarations of functions ( such as "int create_enemyHP (int a);" ) if I can avoid them. If you do this then there are two places in your code that must be correct for your program to work. It makes life easier if there is always a "single source of truth"
Have you run this code through the debugger? It will help you find problems much more quickly.
If your c_action variable is only intended to be a char, I'd suggest to use a char variable, rather than a string.
You might want to try this way, and if you're still faced with an error, you might give
scanf("%c", &c_action); //assuming you used a char.
I didn't understand if the program crashes before you type the "action" or after. Because if it crashes before, then I think your problems are caused by white spaces characters in the input buffer.