This is my very first time posting of many, and I am in desperate need of help. I am attempting to put together a program to run Craps for school, and I am running into the error listed in the title. I have looked around here and messaged my professor, done everything he has asked, and yet still there is no fixing it. Rather than pester him again, could someone please help me figure out what to do next?
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int win1=7;
const int win2 =11;
const int crap1=2;
const int crap2=3;
const int crap3=12;
//prototypes for 5 functions are below, you need to code the bodies for these functions down below the main routine
int rollTheDice();
bool flag = false ;
bool winner( int );
bool craps( int );
void rollMore( int, double );
double getWager();
int diceRoll,dice1,dice2,roll,point;
double wager;
int main()
{
srand(34); //the final run will have 34, seed of 6 rolls a 7 -automatic winner, seed of 36 rolls 2-loser
cout << fixed << setprecision(2); //set output format
wager = getWager(); // call get wager and get back the amount the player wagered
cout<< "The amount wagered is "<<wager<<endl<<endl; //i added this cout to verify what wager is returned from getWager()
diceRoll = rollTheDice();
if( winner(diceRoll) ==true )
{
cout << "\nCongratulations! You have won " << wager << " $ \n";
}
else if( craps(diceRoll)==true) //call function craps and test if true is returned
{
cout << "\nSorry! You have lost " << wager << " $ \n";//print loser message and amount loss
}
else
{
rollMore( diceRoll, wager );
}
return 0;
//Get the user's wager amount with a cin and verify it before returning the value to main
double getWager()
{
cout << "Please enter your initial bet, in whole dollar amounts. The starting bet is 5.00$. \n" ;
cin >> wager ;
while (wager < 5)
{
cout << "I am sorry, the number you have entered is an insufficient bet. Please Try Again \n" ;
cin >> wager;
}
return wager;
}
int rollTheDice() //method is called and returns the roll of two dice, value between2-12
{ //you need two dice variables and generate a random number between 1-6 for each dice
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
diceRoll = dice1 + dice2;
roll=diceRoll;
cout << "You have rolled a " << diceRoll <<" \n";
return roll;
}
bool winner( int roll ) //returns true if roll is 7 or 11,else return false
{
if (roll==win1 or roll==win2)
{
return true;
}
else return false;
}
bool craps( int roll ) //returns true if player craps out which is a roll is 2,3,12,
if (roll==crap1 or roll==crap2 or roll==crap3)
return true ;
else return false ;
}
void rollMore( int point, int diceRoll, double wager, bool flag )
{
point=diceRoll;
cout << "You did not win or lose! We are now rolling to match the point by rolling " << point << "\n";
while (flag=false)
{
diceRoll = rollTheDice();
if (diceRoll==7)
cout << "\nSorry! You have lost " << wager << " $ \n";
flag == true ;
if (diceRoll=point)
cout << "\nCongratulations! You have won " << wager << " $ \n";
flag==true;
}
}
Related
Hello I am making this question because on the for loop "for (grade = 0; grade != -1; numbOfTests++)" or in the getGrade function there seems to be an error if you enter the integer "-1" in the cin input first. More so it gives the "timeout: the monitored command dumped core" error. What is causing this? I looked up on google but the errors dont seem to be the same as mine. Id appreciate the help, my code is below.
#include <iostream>
const int EXTRA_CREDIT = 3;
void displayIntro();
int getGrade();
int finalAverage(int, int);
int main()
{
int grade, sum, numbOfTests, average;
displayIntro();
numbOfTests = -1;
sum = 0;
for (grade = 0; grade != -1; numbOfTests++)
{
grade = getGrade();
sum = sum + grade;
}
sum++;
average = finalAverage(sum, numbOfTests);
std::cout << "Exam average, including extra credit, "
<< "is: " << average << std::endl;
return 0;
}
void displayIntro()
{
std::cout << "This program will calculate the average(%) of "
<< "exam grades." << std::endl;
std::cout << "It will also add extra credit points to the exam "
<< "average given the course difficulty." << std::endl;
std::cout << "Enter all of the grades for one student. Type (-1)"
<< " when finished with that student." << std::endl;
std::cout << "If you have additional students, you will be prompted"
<< " to repeat the program at the end." << std::endl;
}
int getGrade()
{
int userGrade = 0;
std::cout << "Enter an exam grade (type -1 to quit):" << std::endl;
std::cin >> userGrade;
return userGrade;
}
int finalAverage(int runningSum, int counter)
{
int final;
final = (runningSum / counter) + EXTRA_CREDIT;
return final;
}
When -1 is the first input, numbOfTests++ in the for loop is executed once.
This causes numbOfTests to be zero and the finalAverage function will perform division by zero according to that. This may lead to runtime error.
My program will repeat output: "You are currently on the 2 floor out of 5
The sum of the codes is: 7 and the product of the codes is: 12
Try again before he catches onto you!"
Based on how many wrong characters are added how can I fix this? I have inserted the cin.clear and cin.ignore but it will repeat the part above.
i.e. if I type wasds it will repeat 5x. Any other notes are also appreciated.
#include <iostream>
#include <ctime>
using namespace std;
int PlayerLevel = 0;
int MaxLevel = 5;
bool GamePlay ()
{
srand(time(NULL));
int PlayerGuessA, PlayerGuessB, PlayerGuessC;
int CodeA = rand() % PlayerLevel + PlayerLevel;
int CodeB = rand() % PlayerLevel + PlayerLevel;
int CodeC = rand() % PlayerLevel + PlayerLevel;
int SumofCodes = CodeA + CodeB + CodeC;
int ProductofCodes = CodeA * CodeB * CodeC;
cout << "You are currently on the " << PlayerLevel << " floor out of 5" << endl;
cout << "The sum of the codes is: " << SumofCodes << " and the product of the codes is: " << ProductofCodes << endl;
cin >> PlayerGuessA >> PlayerGuessB >> PlayerGuessC;
int PlayerProduct = PlayerGuessA * PlayerGuessB * PlayerGuessC;
int PlayerSum = PlayerGuessA + PlayerGuessB + PlayerGuessC;
if (PlayerProduct == ProductofCodes && SumofCodes == PlayerSum) {
cout << "Great Job you got this!!!\n" << endl;
++PlayerLevel;
return true;
}
else
{
cout << "Try again before he catches onto you!\n" << endl;
return false;
}
}
int GameStart()
{
string Introduction = "Welcome to your worst nightmare. You are trapped in a murderer's house. You are on the 5th floor and need to get to the first floor to escape.\n";
string Instructions = "He has each door locked behind a security system that requires a 3 number code to disarm it.\nEnter the codes and move foward. Each level will the code will be harder to figure out.\n";
string PlayerStart;
cout << Introduction << endl;
cout << Instructions << endl;
cout << "Would you like to escape? Yes or No" << endl;
cin >> PlayerStart;
if (!(PlayerStart != "Yes" && PlayerStart != "yes")) {
++PlayerLevel;
}
return 0;
}
int main ()
{
if (PlayerLevel == 0) {
GameStart();
}
while (PlayerLevel <= MaxLevel)
{
bool bLevelComplete = GamePlay();
cin.clear ();
cin.ignore();
}
cout << "You Made it out! Now run before he finds out!" << endl;
return 0;
}
When the type of the input doesn't match the type of the variable that it is being extracted to, cin sets the fail bit. Once this happens, all subsequent reads fail until the stream is reset. The offending characters are still left in the buffer, so that needs to be cleared out as well.
Your usage of cin.clear() and cin.ignore() meant that the fail bit was getting reset, but only one offending character was being removed (cin.ignore() ignores one character by default). This is why you saw the output repeating x times for x erroneous characters.
You could do something like this:
while (PlayerLevel <= MaxLevel)
{
bool bLevelComplete = GamePlay();
if (cin.fail())
{
//Input extraction failed, need to reset stream and clear buffer until newline
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
}
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int numofEmployees();
int daysMissed(int);
int AverageMissed(int, int);
int main()
{
cout << "Welcome to employee absentee calculator!" << endl;
int numEmployees = numofEmployees();
int Missed = daysMissed(numEmployees);
double misAverage = AverageMissed(numEmployees, Missed);
cout << "There are " << numEmployees << " in the company. They have missed " << Missed << " days total. On average, they have missed " << misAverage << " days." << endl;
return 0;
}
int numofEmployees() {
cout << "How many employees are in your company? ";
int employees;
cin >> employees;
while (employees < 1) {
cout << "Employee count must 1 or greater!" << endl;
}
return employees;
}
int daysMissed(int numEmployees) {
int Absence, totAbsence = 0;
for (int i = numEmployees; i < numEmployees; i++) {
cout << "How many days has each employee missed this passed year? ";
cin >> Absence;
totAbsence += Absence;
}
while (Absence < 0) {
cout << "Values entered must be positive numbers!" << endl;
cin >> Absence;
}
return totAbsence;
}
int AverageMissed(int numEmployees, int Missed){
double Average;
Average = double(numEmployees) / double(Missed);
return Average;
}
This code is being used to calculate the average number of employee absences by way of using three functions. The second function is not working correctly as it is not being called properly by the main. This is for a school assignment.
The problem is daysMissed - if numEmployees is <= 0, then Absense will be uninitialized. But, you say, "I check that in numofEmployees" - the problem is that the compiler doesn't do that sort of whole-program analysis before issuing these warnings.
There is another problem: daysMissed is wrong (twice). If there are two employees, and I enter -2 and 1, there will be no error for the negative number. If on the other hand, if I enter 1 and -2, you never correct totAbsence. You would be much better off writing a little function which reads a number >= some limit in a loop, and keeps prompting until given the correct value. Something like:
int read(const char* prompt, const char* err_prompt, int limit) {
cout << prompt << endl;
for(;;) {
int result;
cin >> result;
if (result >= limit) {
return result;
}
cout << err_prompt << endl;
}
}
Then daysMissed becomes much pleasanter to write - and you can use the same function to read the number of employees (which will go into an infinite loop at the moment)
You should also validate a division by zero plus change the return type.
double AverageMissed(int numEmployees, int Missed){
if (Missed > 0) return double(numEmployees) / Missed;
return 0;
}
by the way, there is no need to cast both operands in the division (/). Casting one of them will be enough to return a double type.
Can you help me guys? I'm a total beginner. My code worked fine then KEEP LOOPING FOREVER and never goes back to or cmd would crash with "Process terminated with status -1073741676". It should loop once then CIN >> again. It happens when I enter 10 digit numbers in my CIN >>.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class noteAssign { //This class return "A" if the random number generated is between 1 and 10
public:
int x;
int noteOut(int x){
if(x>1 && x<10){
cout << "ITS A" << endl;
return x;
}else{
cout << "IT'S NOT A" << endl;
return x;
}
}
}gonote;
int main()
{
cout << "Match the note's Hertz!" << endl;
cout << "Your answer may range from 1 to 20" << endl;
cout << "Type 0 to quit" << endl;
int noteIn; //No real purpose YET
do {
srand(time(0)); //ADDING MULTIPLE RAMDOMIZER FOR WIDER RANDOM RANGE
int rand1 = 1+(rand()%20); //randomizer 1
int rand2 = 1*(rand()%20); //randomizer 2
int hzout = (rand1 * rand2 + rand1 / rand2)%20; //rand 3
noteAssign gonote;
cout << gonote.noteOut(hzout) << endl; //calls the function and gives the parameter
cin >> noteIn; //No real purpose YET
} while(noteIn != 0); //program quits when you enter "0"
};
new to the forums here and beginning to learn C++. This site has already helped me so much with syntax and other things. What I'm trying to do with my code is have the number print to screen, have the time delay, then print the next number. Currently the time delay works, but it prints all 13 numbers generated. Any ideas of what I'm doing wrong?
Here's my code:
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <windows.h>
using namespace std;
int main( )
{
// Function prototypes
int random ( int minValue, int maxValue);
// Constant declarations
const int maxValue = 9;
const int minValue = 0;
// Local variable declarations
int seed;
int numberOfPeople;
int peopleCount = 0;
int numberCount;
int number;
// Initialize the random number generator
cout << "Welcome to the Lottery!" << endl;
cout << "Enter your lucky number to start: " << endl;
cin >> seed;
srand (seed);
// Generate and display numbers
cout << "Enter the number of people participating in the lottery:" << endl;
cin >> numberOfPeople;
cout << "Your lucky lottery numbers for the day are:" << endl;
cout.setf (ios::left, ios::adjustfield);
cout << setw(8) << "Pick 3" << setw(10) << "Pick 4" <<
setw(15) << "Pick 6" << endl;
while (peopleCount < numberOfPeople) {
numberCount = 0;
while (numberCount < 13){
number = random (minValue, maxValue);
Sleep (500); // pauses for half a second
cout << number << " ";
if (numberCount == 2){
cout << " "; }
else if (numberCount == 6){
cout << " "; }
else if (numberCount == 12){
cout << endl; } //end if, else if
numberCount++;
} //end nested while
peopleCount++;
} // end while
return 0;
} // end main()
/**
* Produces a pseudo-random number
* #param minValue minimum value that can be generated
* #param maxValue maximum value that can be generated
*
* #return psuedo-random number in the specified range
*/
int random ( int minValue, // min possible number to be generated
int maxValue) // max possible number to be generated
{
return ( (rand() % maxValue) + minValue);
} // end random()
cout is generally buffered, and the newline causes the buffer to be flushed to the screen. As you display the numbers on the same line, this could explain that everything is displayed at onece despite the delay that you've build in.
Use cout.flush(); to force output to be done without buffering delay. You could as well use the manipulator form: cout << number << " " << flush;