C++: array index is not increasing - c++

I want to make a simple game of 3 players, each player moves in a block depending of the random function from 1 to 6 blocks each time, when first player has been moved the second player start and then then the third player. To do that I increase the index of an array rach time a player finish its move.
My problem is that the indexer seems no to been increased, and it stacks in the player 1 even if I increase it. I have exactly the same code in C# and it works well!
Here is the code in C++.
int main ()
{
string namesofplayers[] = {"one","two","three"};
int movementofplayers[] = {0,0,0}; // start position of players is
int gamesize = 32; //32 blocks-steps of game
int random;
int y = 0;
a:
y++;
if (y >= 3)
{
y = 0;
}
cout << "it's" << namesofplayers[y] << "turn to play";
int R = (rand() % 6 + 1);
cout << "player " << namesofplayers[y] << " moves to block" << R << endl;
movementofplayers[y] += random;
cout << movementofplayers[y];
if (movementofplayers[y] < gamesize)
{
goto a;
}
else
{
cout << "Player " << namesofplayers[y] << " wins the game" << endl;
}
}

On the off chance of doing your work, I took the liberty to write up an alternative implementation which fixes some of the problems your former code had and also produces more readable output. I also threw out the one-liners because they drive me crazy, but that's personal preference. Also, I tend to explicitly qualify symbols from the standard library using the appropriate scope.
Get rid of goto. You can browse SO and the web for multiple reasons why not to use an explicit jump like that. Just use a loop
Fix the missing initial seed for the pseudo-random number generator. If you set a varying seed, i.e. by invoking it with some variable value (e.g. time(nullptr) ), you'll always get the same succession of "random" values - with each program invocation.
Fix the use of the variable random. You tried to add some garbage-initialized value random to movementofplayers[y]. Interestingly, g++-4.7 seems to ensure that the variable is set to 1 before being used in the arithmetic op. However, the correct variable you need is R.
Return a well defined value from main().
I hope the code still does what you intended it to do:
#include <string>
#include <iostream>
int main ()
{
srand(time(NULL));
std::string namesofplayers[] = {"one","two","three"};
int movementofplayers[] = {0,0,0}; // start position of players is
int gamesize = 32; //32 blocks-steps of game
int y = 1;
while(movementofplayers[y] < gamesize)
{
if (y >= 3)
{
y = 0;
}
std::cout << "it's " << namesofplayers[y] << " turn to play" << std::endl;
int R = (rand() % 6 + 1);
std::cout << "player " << namesofplayers[y] << " moves to block " << R << std::endl;
movementofplayers[y] += R;
std::cout << "movements of player " << namesofplayers[y] <<": " << movementofplayers[y] << std::endl;
y++;
}
std::cout << "Player " << namesofplayers[y] << " wins the game" << std::endl;
return 0;
}

Here is how I would do it.
Added seeding the random number generator so you don't get the same game every time.
Added a constant for number of players to get rid of the magic number and also make it easier to expand the number of players if desired.
Got rid of the goto. Although it is possible to use goto in a reasonable way it is prone to accidental misuse, makes the code harder to follow, and makes people angry. :)
I tweaked the output and names a bit just to make it a little easier for me to test. In doing so I corrected an issue where it said the player moved to block R which was their roll for that turn, not their actual position in the game.
#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
std::srand(static_cast<unsigned int>(std::time(0)));
const int gamesize = 32;
const int num_players = 3;
const std::string namesofplayers[num_players] = {"1", "2", "3"};
int movementofplayers[num_players] = {0, 0, 0};
int current_player = 0;
for(;;) //Loop forever, the game logic will exit the loop when a winner is found
{
const int roll = rand() % 6 + 1;
movementofplayers[current_player] += roll;
std::cout << "Player " << namesofplayers[current_player] << " rolls a " << roll << " and moves to block " << movementofplayers[current_player] << std::endl;
//Check if they won and if so, end the game
if(movementofplayers[current_player] >= gamesize)
{
std::cout << "Player " << namesofplayers[current_player] << " wins the game!" << std::endl;
break;
}
current_player = (current_player + 1) % num_players;
}
return 0;
}

Related

Why is my function continuing even after I give it wrong input?

I made up a game called password hacker in C++, purpose is to guess the password through given hints, when I input the correct password, it works correct, and moves to the new level as well.
But it does the same even when I input wrong password as well.
#include <iostream>
void Intro(int Level) {
std::cout << "SUP, this PC is locked\n\n" << "well, sure why not give it a try.... it will all be over soon when you type the password incorrectly.\n" << "type your best code here to break security of server number " << Level;
}
bool PlayGame(int Diff) {
Intro(Diff);
int CodeA = 0;
int CodeB = 1;
int CodeC = 2;
int CodeProduct = CodeA * CodeB * CodeC;
int CodeSum = CodeA + CodeB + CodeC;
std::cout << std::endl;
//Instructions
std::cout << "+ 3 number password" << "\n+The numbers adds up to " << CodeSum << "\n+The numbers multiply up to " << CodeProduct << std::endl;
int PlayerGuessA;
int PlayerGuessB;
int PlayerGuessC;
std::cin >> PlayerGuessA >> PlayerGuessB >> PlayerGuessC;
int PlayerSum = PlayerGuessA + PlayerGuessB + PlayerGuessC;
int PlayerProduct = PlayerGuessA * PlayerGuessB * PlayerGuessC;
std::cout << "You entered:\n" << PlayerGuessA << " " << PlayerGuessB << " " << PlayerGuessC;
std::cout << "\n \n Your numbers multiply up to: " << PlayerProduct;
std::cout << "\n Your numbers add up to: " << PlayerSum;
if (PlayerSum != CodeSum && PlayerProduct != CodeProduct) {
std::cout << std::endl << "Like i said earlier, PATHETIC" << std::endl;
return false;
} else {
std::cout << std::endl << "Well, No shit Sherlock " << std::endl;
return true;
}
}
int main() {
int Lev = 1;
while (true) {
bool bLevelComplete = PlayGame(Lev);
std::cin.clear(); //clears any errors
std::cin.ignore(); //discards buffer
++Lev;
}
return 0;
}
Seems to me like you need to put your level up code within your win condition statement, otherwise you're telling the game to keep going regardless of the outcome.
So make Lev global, take the ++Lev out of the main function and put it in the else statement of PlayGame.
OR
Have an if statement wrapped around the ++Lev that takes the return value of PlayGame as it condition. So,
if(bLevelComplete){
++Lev;
}

Variable not changing from user input in C++

I'm trying to create multiple calculators in the C++ console for Geometry Theorems and other formulas in Algebra, and for some weird reason on the start of the program, when selecting an option the variable scene does not want to change(shown before the array of calculators[], and instead of going to the Pythagorean Theorem(scene 1), the console says, "Press any key to continue. . ." and closes.
I've tried both the switch() andif() statements to navigate scene management, but what am I doing incorrectly? (I'm still a C++ learner by the way, but I have other programming language experience).
Thanks for the help in advance.
#include "stdafx.h"
#include <iostream>
#include <cmath>
int scene(0);
char calculators[3][25] =
{
"",
"Pythagorean Theorem",
"Homer's Formula"
};
void selection()
{
std::cout << "Enter a number to select a calculator." << std::endl; // Opening
for (int i = 1; i <= 2; i += 1) {
std::cout << "Option " << i << ": " << calculators[i] << std::endl;
}
}
void pTheorem()
{
int a;
int b;
std::cout << "Enter side a: ";
std::cin >> a;
std::cout << "Enter side b: ";
std::cin >> b;
std::cout << "Side length of c is " << sqrt(pow(a, 2) + pow(b, 2)) << std::endl;
}
int main()
{
switch(scene)
{
case 0:
selection();
std::cin >> scene;
std::cout << "You've selected the " << calculators[scene] << " Calculator" << std::endl;
break;
case 1:
pTheorem();
break;
}
return 0;
}
Your main problem is that scene has been declared and initialized 0 at the beginning(globally) itself. This will give you always the same switch case = 0. Changing scene inside the switch cases will not work. Instead, you need to input the scene before the switch.
int main()
{
selection();
int scene = 0;
std::cin >> scene;
switch(scene)
{
......
}
}
Secondly, use std::string instead of char array and use std::vector<>/std::array to store them. For example:
std::array<std::string,2> calculators =
{
"Pythagorean Theorem",
"Homer's Formula"
};
and for loop can be:
for (int i = 0; i < 2; ++i)
std::cout << "Option " << i+1 << ": " << calculators[i] << std::endl;

Trouble finding differential between two items in C++

Let me preface this by saying I'm still extremely new to C++ and want to keep things as simple as possible. I'm also pretty terrible at math.
Mostly, I'm looking to see if anyone can help my code so it will always give the correct result. I've mostly got it to do what I want, except in one scenario.
My code is trying to find out how many packages of hotdog weiners and how many packages of hotdog buns someone has purchased. Then it tells the user how many hotdogs they can make from that as well as how much leftover weiners or buns they would have. Assuming a package of weiners contains 12 and a package of buns contains 8, this is what I have come up with so far:
#include <iostream>
#include <cmath>
using namespace std;
void hotdog(int a, int b){ //a = weiner packages, b = bun packages
int weiners = 12 * a;
int buns = 8 * b;
int total = (weiners + buns) - (weiners - buns);
int leftOverWeiners = total % weiners;
int leftOverBuns = total % buns;
int totalHotDogs = total / 2;
cout << "You can make " << totalHotDogs << " hotdogs!" << endl;
if (leftOverWeiners > 0){
cout << "You have " << leftOverWeiners << " weiners left over though." << endl;
}else if (leftOverBuns > 0){
cout << "You have " << leftOverBuns << " buns left over though." << endl;
}
}
int main(){
int a;
int b;
cout << "Let's see how many hotdogs you can make!" << endl;
cout << "How many weiner packages did you purchase?: ";
cin >> a;
cout << "How many bun packages did you purchase?: ";
cin >> b;
hotdog(a, b);
return 0;
}
With this, I can always get the correct answer if the ratio of buns to weiners is the same or if there are more weiners than buns.
Because of the way I've set up total and/or leftOverBuns (lines 9, 11), I will never get the correct answer to how many left over buns there will be. I know there must be a simpler way to do this if not a way to modify my current code but I am stumped.
I know I left virtually zero notation, so if you would like some please let me know!
You're making it too complicated. Try this:
if(weiners > buns)
{
cout << "You can make " << buns << " hotdogs!" << endl;
cout << "with " << weiners-buns << " weiners left over" << endl;
return;
}
cout << "You can make " << weiners << " hotdogs!" << endl;
if(buns > weiners)
{
cout << "with " << buns-weiners << " buns left over" << endl;
}
The smaller of {buns, weiners} is the number of hot dogs, and the if-then blocks determine whether the function will report leftover buns or weiners.
#include <iostream>
void hotdog( int weinerspackages, int bunspackages ){
const int weinersPerPackage = 12;
const int bunsPerPackage = 8;
const int totalweiners = weinerspackages * weinersPerPackage;
const int totalbuns = bunspackages * bunsPerPackage;
int leftoverweiners = 0;
int leftoverbuns = 0;
int amountOfHotdogs = 0;
if( totalweiners > totalbuns ){
leftoverweiners = totalweiners - totalbuns;
amountOfHotdogs = totalbuns;
leftoverbuns = 0;
}
else if( totalbuns > totalweiners ){
leftoverbuns = totalbuns - totalweiners;
amountOfHotdogs = totalweiners;
leftoverweiners = 0;
}
else{
amountOfHotdogs = totalweiners;
leftoverweiners = 0;
leftoverbuns = 0;
}
std::cout << "You can make: " << amountOfHotdogs << " Hotdogs" << std::endl;
std::cout << "Leftover Weiners: " << leftoverweiners << " || Leftover Buns: " << leftoverbuns << std::endl;
}
int main(){
int PackagesW = 8;
int PackagesB = 12;
hotdog( PackagesW, PackagesB );
system("pause");
return 0;
}
Note: It is possible to do this with less variables, I declared this amount of variables to make it easier to understand what the numbers represent.
Assuming that it only takes one of each to make a hotdog, you can find which of the ingredients you have the least, and the amount of hotdogs you can make will be limited by the amount of that ingredient, that is why amountOfHotdogs takes the value of the lesser one. If both are equal in amount, then amountOfHotdogs can take the amount of either.
Only the ingredient with the larger amount will have leftovers, therefore leftoverweiners = totalweiners - totalbuns; when totalweiners > totalbuns and vice-versa.

How can I run multiple functions after an if statement

How can I have a the return and cout both take place as a result of being true or false?
gameState computerMove(gameState state) {
if (sizeOfPile(state,1) > 0)
return removeCoinsFromPile(state,1,1);
cout << "I take 1 coin from Pile 1." << endl;
else
return removeCoinsFromPile(state,1,2);
cout << "I take 1 coin from Pile 2" << endl;
C++ uses the concept of a "compound statement" - a statement made up of multiple sub-statements - denoted by brackets, { and }, just as you used for your function.
gameState computerMove(gameState state) {
}
You can use a compound statement pretty much anywhere you can use a regular statement:
int sum = 0;
for (int i = 0; i < 100; ++i)
sum += i;
or
int sum = 0;
for (int i = 0; i < 100; ++i) {
sum += i;
}
These do the same thing, but if we need to add more instructions inside the for, we add them between the '{'s.
One important thing about this: a compound statement is considered to have "scope"
int i = 1;
{
int i = 2;
std::cout << "inside, i = " << i << '\n';
}
std::cout << "outside, i = " << i << '\n';
Programmers sometimes use this to maintain lifetime of advanced objects. The following code opens a file and releases it as soon as we're done with it - because that's what happens when the 'istream' object goes out of scope.
std::string instructions;
{
std::istream file("instructions.txt");
file >> instructions;
} // <-- 'file' goes away, which closes the file.
However: the return keyword causes a function to end and return a value. So you're going to have to do that after your cout.
if (sizeOfPile(state,1) > 0) {
cout << "I take 1 coin from Pile 1." << endl;
return removeCoinsFromPile(state,1,1);
}
Your function ends after the return statement, so you should prepare what to return in a variable, print it, and then do the return, like this:
int res;
if (sizeOfPile(state,1) > 0) {
res = removeCoinsFromPile(state,1,1);
cout << "I take 1 coin from Pile 1." << endl;
} else {
res = removeCoinsFromPile(state,1,2);
cout << "I take 1 coin from Pile 2" << endl;
}
return res;
However, in your specific case you can rewrite this without an if by using a conditional operator:
int pile = sizeOfPile(state,1) > 0 ? 1 : 2;
res = removeCoinsFromPile(state, 1, pile);
cout << "I take 1 coin from Pile " << pile << endl;
return res;
You could move the return statement to after the cout statement.
gameState computerMove(gameState state) {
int res;
if (sizeOfPile(state,1) > 0) {
cout << "I take 1 coin from Pile 1." << endl;
res = removeCoinsFromPile(state,1,1);
} else {
cout << "I take 1 coin from Pile 2" << endl;
res = removeCoinsFromPile(state,1,2);
}
return res;
}
This website might be helpful when learning about functions in c++.

Project Euler Number Nine with C++

I wrote this, but when running it, the console just sits at "Running..." and won't really do anything, at least that I can see. I am kind of at a loss here as I can't think of anything else to do.
#include <iostream>
#include <cmath>
#include <cstdlib>
int main(void) {
int count = 0;
do {
int a = 1;
int b = 2;
int c = 3;
int total;
for (a=1;a<b;a++) {
for (b=2;b<c;b++) {
for (c=3;c<=1000;c++) {
total = a+b+c;
if (total == 1000 && a*a + b*b == c*c) {
std::cout << a << ", " << b << ", " << c;
}
}
}
}
count++;
} while(count < 1000);
return 0;
std::cin.get();
}
You might not see any output because your terminal is line-buffered and you never write a line break or flush the stream. To fix this you could add std::endl to your output line:
std::cout << a << ", " << b << ", " << c << std::endl;
This way you should see all triples as soon as they are found, but the program will still take a long time to complete. It might even take a long time till any results are found. You could speed the program up by avoiding some of the nested loops.