C++ maze count passing time [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've created functioning maze game in c++ in console.
I would like to add function to count time needed to pass the maze by player.
Total time could be displayed after pass the maze.
I would really appreciate any help or ideas.
main game-loop looks something like that:
do {
show(); // function do display maze , 2d array
cout << "Your position: " << x << " " << y << endl;
cout << "Coins gained: " << coins << endl;
cout << "blahblahblah" : "<<endl;
m = getche();
cout << endl;
move(m); // function to recognize which way player want to go, including checking for not going through the wall
cout << endl;
system("CLS");
} while (x != 12 || y != 18 || coins < 10); //for pass the maze player have to move on these position and gain x coins
system("CLS");
cout << "You Won!" << endl;
cout << "Click enter to move on. \n";

#include <time.h>
#include <iostream>
int main () {
int start, end, total;
start = time(NULL);
//place loop here
//game ends, calc time
end = time(NULL);
total = end - start;
std::cout << "You completed the maze in " << total << " seconds.";
return 0;
}
Essentially, what this does is start counting time at a certain point, then stop counting at a later point (in seconds), though cin and getch(), or anything else that pauses the program in order to get an input may cause the timer to stop. In certain libraries, it will use system time. In other libraries, it will use running time. Be careful of this, and if it does use running time, be sure to use an input method such as
int main(){
time_t myTime,myTimeEnd;
time(&myTimeEnd);
myTime = myTimeEnd;
//code
time(&myTime);
int total = myTimeEnd - myTime;
std::cout<< "Time taken is " << total << " seconds.";
return 0;
}
The second method gets and holds the time for you, which is saved by passing your time variable by reference to the time function, which 'gets' time.

Related

Why the output of my code has 4745728 as the output? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm making a simple program to count user balance whenever there's a positive integer input between 1-5 from user. I only make the code for the first option, but something weird is happening from the output, here's my code :
#include <iostream>
using namespace std;
int balance = 100000;
int iNet = 59900;
int internet(int iNet, int code, int remain){
if(code == 1){
remain = balance - iNet;
cout << "Price = " << iNet << endl;
cout << "Remaining credit = " << remain <<endl;
}
}
int main(){
int code, remain;
cin >> code;
cout << internet(iNet, code, remain);
return 0;
}
But when i run the program, it shows like this :
user input
1
program output
Price = 59900
Remaining credit = 40100
4745728
I have no idea where's the 4745728 coming from.
If you don't want to return anything from a function, make the return type void:
void internet(int iNet, int code, int remain){
// ^
if(code == 1){
remain = balance - iNet;
cout << "Price = " << iNet << endl;
cout << "Remaining credit = " << remain <<endl;
}
}
Then compiler will not allow you to print a rubbish value anymore and you will have to fix your main:
int main(){
int code, remain;
cin >> code;
internet(iNet, code, remain);
//^ don't print anything, just call the function
return 0;
}

Conditional statement outputting the wrong command [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 3 years ago.
Improve this question
Beginner here so I'm sorry if I made nooby mistakes
I assign di to be the array myworld[] depending the the user input it'll assign the di into the appropriate array position, but for some reason the if statement keep outputting "make" instead of "change" when my input is 'c'
I tried to remove else if and put if for all of them, or got rid of else if and just use else.
#include <iostream>
using namespace std;
int main() {
char di;
char myword[] = {'d','m','s' ,'c'};
do {
cout << "Make a selection:" << endl;
cout << "d - insert 1$ bill" << endl;
cout << "m - view menu" << endl;
cout << "s - select an item" << endl;
cout << "c - get change" << endl;
cin >> di;
if (di == 'd')
di = myword[0];
else if (di == 'c')
di = myword[3];
}while (!myword);
if (myword[0])
cout << "make";
else if (myword[3])
cout << "change";
return 0;
}
Probably you forgot to make a comparison inside if statement. For now you are just saying if('d'!= 0) which is always true. Perhaps you tried to make if(di == myword[0]). The same applies for the else if statement.

Cannot exit while loop with boolean condition c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi this is my first post. I apologize if I'm not following certain rules or conventions. If that is the case please let me know.
I have a game which runs in a while loop until the score limit is reached by either player, at which point the other player has one last (iteration) chance to beat the first players score. However after the score limit is reached, the loop continues to run and the winner is never checked.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
int roll();
int playTurn(int);
int main(){
const int LIMIT = 5;
int whoseTurn = 1;
int pnts1 = 0;
int pnts2 = 0;
bool suddenDeath = false; //True when score limit is reached
while(!suddenDeath){
if(pnts1 >= LIMIT || pnts2 >= LIMIT){ //Limit was reached by previous player.
suddenDeath == true; //Next player has 1 turn to win
}
if(whoseTurn == 1){
pnts1 += playTurn(whoseTurn); //Play turn and tally points
whoseTurn = 2; //Swith player for next iteration
}
else if(whoseTurn == 2){
pnts2 += playTurn(whoseTurn);
whoseTurn = 1;
}
cout << "-------------------------------------" << endl //Display score
<< "Player 1 has " << pnts1 << " points" << endl
<< "Player 2 has " << pnts2 << " points" << endl
<< "-------------------------------------" << endl << endl;
};
if(pnts1 > pnts2)
cout << "Congratulations Player 1! You won with a score of: " << pnts1 << " - " << pnts2;
else if(pnts2 > pnts1)
cout << "Congratulations Player 2! You won with a score of: " << pnts2 << " - " << pnts1;
else if(pnts1 == pnts2)
cout << "A tie! What are the chances?";
return 0;
}
suddenDeath == true;
// ^^
is an expression meaning "compare those two values", which is then thrown away. The C statement 42; is equally valid, and equally useless (a).
You want to assign the value, so you'd use:
suddenDeath = true;
// ^
It's actually the other end of the much more common if (a = 0) problem where people assign rather than compare.
(a) If you're wondering why anyone in their right mind would allow this into a language, it actually allows for some powerful constructs with minimal code.
And, you've seen it before most likely. The statement i++; is such a beast. It's an expression giving i (which you throw away here) with the side effect that i is incremented afterwards.
suddenDeath = true;
Use a single = for assignment. == is used for condition check.

How can I keep track of the coin number available coins in a vending machine? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a school project in c++ for a simple vending machine that accepts one-dollar-bills and give change in quarters, dimes, nickels. All the items cost less than 1 dollar .
But I need to add an extra ability to keep track of the available change in the machine, and I dont how to do it.
And this is the piece of code I wrote:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//set the variables
int Qav=5 ;
int Dav=5 ;
int Nav=5 ;
int purchaseAmount ;
cout << "Simple Vending Program for Adam Ashouri (Regular Version)" <<endl;
cout <<endl;
cout <<endl;
cout << "Enter a purchase amount [5 - 100] -->";
cin >> purchaseAmount;
int chaneBack = 100 - purchaseAmount ;
changecalculator ();
}
void changecalculator ()
{
int purchaseAmount ;
int QBack ,DBack ,NBack ;
int chaneBack = 100 - purchaseAmount ;
if (purchaseAmount %5 == 0)
{
cout << "You entered a purchase amount of " << purchaseAmount << " cents." <<endl;
cout <<endl;
QBack = chaneBack / 25 ;
DBack = (chaneBack % 25) / 10;
NBack = (chaneBack %25 %10) / 5;
cout <<"Your change of " <<chaneBack <<" cents is given as " <<QBack <<" Q, " <<DBack <<" D,and " <<NBack <<" N." <<endl;
int coinsNum = QBack + DBack + NBack;
cout << "The value of your " <<coinsNum <<" coins adds up to " <<chaneBack <<" cents." <<endl;
cout << "Thank you for using my program.";
}
else
{
cout << "Unable to process an invalid purchase amout of " <<purchaseAmount <<" cents." <<endl;
cout << "Thank you for using my program." <<endl;
}
}
First of all, your current program doesn't work, because you don't transfer purchaseAmount inside function changecalculator(). You need to do it this way:
1) Change signature of the function to this: void changecalculator(int iPurchaseAmount)
2) Send purchaseAmount inside the function (i.e. call it this way: changecalculator(purchaseAmount);)
3) Use value iPurchaseAmount inside the body of your function and remove line int purchaseAmount; from it.
And about your main question (saving of number of available coins):
Ok, you've added Qav, Dav and Nav (available coins) in the body of your program. It is right. Now you have to transfer these values inside your changecalculator() function. Try to use this signature of your function: void changecalculator(int iPurchaseAmount, int& ioQav, int& ioDav, int& ioNav) (these values will be available inside body of the function). And call this function this way: changecalculator(purchaseAmount, Qav, Dav, Nav);.
After it you need only to analyze these values and change it inside the function. Let's add changeing of the values (it is necessary in case of many calls of this function). Just add following lines after calculation of QBack, DBack and NBack:
ioQav -= QBack;
ioDav -= DBack;
ioNav -= NBack;
And the most complex part of this task is analysis of values ioQav, ioDav, ioNav before calculation of QBack, DBack and NBack. Let's do it for quarters:
QBack = std::min(chaneBack / 25, ioQav);
It is calculation of the smallest value of two: available number of quarters and optimal number of quarters. So, this line calculates exactly what do you need. After it you can calculate number of dimes:
chaneBack -= QBack * 25; // subtract quarters
DBack = std::min(chaneBack / 10, ioDav);
If you understand this logic, you can complete it for NBack.

Pre or Post test loop? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to figure out whether a pre or post test loop would be the best method so that the user can continue to input values that will be searched for, until a sentinel value is entered to end the program. Also, what would my parameters look like for the loop? Here is my code, I need to include the loop. Also, I understand that a post test loop is executed at least once. Thanks in advance!
#include<iostream>
using namespace std;
int searchList( int[], int, int); // function prototype
const int SIZE = 8;
int main()
{
int nums[SIZE]={3, 6, -19, 5, 5, 0, -2, 99};
int found;
int num;
// The loop would be here
cout << "Enter a number to search for:" << endl;
cin >> num;
found = searchList(nums, SIZE, num);
if (found == -1)
cout << "The number " << num
<< " was not found in the list" << endl;
else
cout << "The number " << num <<" is in the " << found + 1
<< " position of the list" << endl;
return 0;
}
int searchList( int List[], int numElems, int value)
{
for (int count = 0;count <= numElems; count++)
{
if (List[count] == value)
// each array entry is checked to see if it contains
// the desired value.
return count;
// if the desired value is found, the array subscript
// count is returned to indicate the location in the array
}
return -1; // if the value is not found, -1 is returned
}
Your question is more of a use case dependent.
Post Case: When you need the loop to run AT LEAST once ( 1 or more times)
Pre Case: The loop can run 0 or more times.
I must say, I'm not entirely sure what you want to know. I would honestly recommend a good book on C++. Post test loops are not super popular in C++ (they are of the form "do.. while" where "while" loops / pre test loops are much more common). A little more information is available here: "Play It Again Sam"
EDIT: you need to get data from the user, test it, and then do stuff based on it. Your best bet is something along the lines of
static const int SENTINEL = ??;
int num;
cout << "please input a number" << endl;
cin >> num;
while( num != SENTINEL ) {
// DO STUFF HERE
// Now get the next number
cout << "please input a number" << endl;
cin >> num;
}