any one have any suggestions why my code won't work?

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int intro(int &num_games);
int random();

int main(void)
{
    int seed(0), num_games(0);
    int point, point2(1);
    int a(0), b(0), c(0), d(0);

    cout<<"Lexi Barlow and Alex Puffer    craps.cpp"<<endl;

    intro(num_games);

    cout<<"\nEnter a random seed number: "<<endl;
    cin>>seed;
    srand(seed);

    while(num_games>0)
    {
        cout<<"\nYou have "<<num_games<<" games left."<<endl;
        num_games--;
        cout<<"Roll the die"<<endl;
        system("pause");
        a=random();
        b=random();
        point=a+b;
        cout<<"\nYou rolled "<<a<<"+"<<b<<"="<<point<<"."<<endl;
        
        if(point==7)
        {cout<<"You win!";}
        else if(point==11)
        {cout<<"You win!";}
        else if(point==2)
        {cout<<"You lose.";}
        else if(point==3)
        {cout<<"You lose.";}
        else if(point==12)
        {cout<<"You lose.";}
        else
        {
            while((point2!=7)||(point2!=point))
                {
                cout<<"You roll again\n";
                c=random();
                d=random();
                point2=c+d;
                cout<<"\nYou rolled "<<c<<"+"<<d<<"="<<point2<<"."<<endl;
                if (point2==7)
                {cout<<"You lost";}
                else if (point2==point)
                {cout<<"You won";}
                }
        }
        }
       
    
system("pause");
return(0);
}

int intro(int &num_games)
{
    cout<<"\nOur program simulates a game of craps. "<<endl;
    cout<<"Random numbers will be generated to simulate rolling dice. "<<endl;
    cout<<"You will need to input a random seed number and the number "<<endl;
    cout<<"of games you would like to play. Enjoy!"<<endl;

    cout<<"How many games would you like to play? "<<endl;
    cin>>num_games;
    return(num_games);
}

int random()
{ 
    return 1+ rand()%6;
}

Dani AI

Generated

The most likely cause of the behavior described by is a logic error in the loop that handles "point" re-rolls: the condition uses OR so it will almost never become false and the re-roll loop never exits. That explains why the program appears to hang even though the dice code and printing work (as observed). and were right to ask for specifics — the symptom is an apparent infinite loop on the point phase.

Fix the loop condition so it stops when the roll equals the point (win) or equals 7 (loss). For example:

while (point2 != 7 && point2 != point) {
    // roll again, update point2
}

Other practical improvements and gotchas:

  • Include <cstdlib> (or use std::rand/std::srand) so rand/srand are declared; consider seeding once with a sensible seed. For modern code prefer the C++11 <random> facilities (std::mt19937 + std::uniform_int_distribution) for better quality and clarity (see cppreference).
  • Avoid naming a function random() (it can clash with library functions); use a clear name like roll_die.
  • intro currently takes num_games by reference and also returns int — consider changing it to void intro(int &) or make it return the value rather than rely on side effects.
  • system("pause") is Windows-only; replace with a portable "press Enter to continue" using std::getline or cin.get for portability.
  • Clean up unused headers and add newlines after messages so output is readable (as suggested about spacing).

Checklist to apply: change the loop boolean to &&, add <cstdlib> or switch to <random>, rename the die function, and remove system-dependent calls. These changes resolve the infinite-loop symptom and make the code more robust and portable.

Recommended Answers

All 3 Replies

Care to supply some details? Or are we supposed to guess?

No idea why it doesn't work. What does it do that it is not supposed to do? Or what doesn't it do that it should?

I ran it and it worked just fine. Just a tip though, try spacing out some of your code. It's sorta hard to read.

like change this:

cout<<"CRAPS IS A FUNNY NAME"<<endl;

to this:

cout << "CRAPS IS A FUNNY GAME" << endl;

I'm just saying.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.