Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ code not working

Name: Anonymous 2006-06-28 19:11

What is wrong with this code? I think it has to do with the char variable but it just messes up whenever it askes for a y or n input.

/
#include <stdafx.h>
#include <iostream>
#include <iomanip>
#include <cstdlib> //for rand() function
#include <ctime> // for srand() function
using namespace std;

int main()
{
    int frequencyHeads = 0;
    int frequencyTails = 0;
    int loopCount = 0; //count the repetition of coin toss
    int coinToss; // 1 for head, 2 for tail
    char yesNo;

    srand( time( NULL )); //seed value for the random number generator

    //format floating-point numbers
    cout << fixed << showpoint << setprecision(2);

    cout << "Do you want to continue the game? "
        << " (Y for yes and N for no.)\n";
    cin >> yesNo;

    //Table heading
    cout << "\n\nFrequency of heads  "
        << "Frequency of tails  "
        << "Ratio of heads / tails\n";
    cout << "------------------  ------------------  "
        << "----------------------\n";


    //Add your code here whatever you see fits.
   
    while(yesNo == 'y') {
        cout << "\n\n" << frequencyHeads
             << " " << frequencyTails
        << " " << coinToss;

        while( loopCount < 100)//do not modify this loop body
        {
            //generate a random number, 1 or 2
            coinToss = rand() % 2 + 1;

            if( coinToss == 1 )
                frequencyHeads++;
            if( coinToss == 2 )
                frequencyTails++;

            loopCount++;
        }//No modification-Restriction ends here.

        //Add your code again from this point on
        //to complete the program
       
        cout << "Do you want to continue the game? "
        << " (Y for yes and N for no.)\n";
        cin >> yesNo;
    }
   
    cout << "GameOver: Thank you!";
    cout << endl << endl;

    return 0;
}

Name: Anonymous 2006-06-28 20:18

LINE 1: INVALID INPUT: /
SEGMENTATION FAULT

Name: Anonymous 2006-06-28 20:21

on a quick glance, i note many things wrong with this code.  if you would care to elaborate on the particular problem you're having, you may get a fitting response.

Name: Anonymous 2006-06-29 13:44 (sage)

>>3
GTFO NOOB

Fix'd!

Name: Anonymous 2006-07-06 14:32

You have some random '/' in the begining of the code.

Fixed.

Name: Anonymous 2006-07-06 15:09

well the first error is that you never initialized the coinToss variable, and you try to output it on line 37 before you put any value into it.  that will cause the program to abort in debug mode.

Name: Anonymous 2006-07-06 23:22

>>6
int coinToss; // 1 for head, 2 for tail

Yeah, he did.

Wouldn't it be easier to initialize coinToss as a bool instead of an int?

Name: Anonymous 2006-07-06 23:33

>>13
He declared the variable coinToss, but he never initialized it.
i.e. int coinToss = 0;

Name: mnu 2006-07-06 23:48

>>14

It doesn't really matter for a program like this... when you initialize int a, it's the equivilent to saying int a = 0.

But that's not the issuer that I've found... for some reason the while loop embedded in the other while loop isn't run whenever 'y' is pressed. Hmm...

Name: mnu 2006-07-06 23:54

AHAH!! VICTORY!


#include <iostream>
#include <iomanip>
#include <cstdlib> //for rand() function
#include <ctime> // for srand() function
using namespace std;

int main()
{
    static int frequencyHeads = 0;
    static int frequencyTails = 0;
    static int loopCount = 0; //count the repetition of coin toss
    static int coinToss; // 1 for head, 2 for tail
    static char yesNo;

    srand( time( NULL )); //seed value for the random number generator

    //format floating-point numbers
    cout << fixed << showpoint << setprecision(2);

    cout << "Do you want to continue the game? "
        << " (Y for yes and N for no.)\n";
    cin >> yesNo;

    //Table heading
    cout << "\n\nFrequency of heads  "
        << "Frequency of tails  "
        << "Ratio of heads / tails\n";
    cout << "------------------  ------------------  "
        << "----------------------\n";


    //Add your code here whatever you see fits.
   
    while(yesNo == 'y') {
     

       
        while( loopCount < 100)//do not modify this loop body
        {
            //generate a random number, 1 or 2
            coinToss = rand() % 2 + 1;

            if( coinToss == 1 )
                frequencyHeads++;
            if( coinToss == 2 )
                frequencyTails++;

            loopCount++;

           
        }//No modification-Restriction ends here.
        loopCount = 0;

        cout << "\n\n"                      << frequencyHeads
             << "                      "    << frequencyTails
             << "                       "   << coinToss << endl << endl;
        frequencyHeads = 0;
        frequencyTails = 0;       

       
        //Add your code again from this point on
        //to complete the program
       
       
      
        cout << "Do you want to continue the game? "
             << " (Y for yes and N for no.)\n";
        cin >> yesNo;       

    }
   
    cout << "GameOver: Thank you!";
    cout << endl << endl;
    system("PAUSE");

    return 0;
}

That should work. The first time you run through the embeded while loop, it sets loopCount to 100, so the next time you run through the loop you get something to the effect of while( 100 <  100). Also, the frequencyHeads and frequencyTails aren't set back to zero, so it causes a related problem. Anything else?

Name: Anonymous 2006-07-07 1:00 (sage)

>>15
Only when it's a global, moran.

Name: Anonymous 2006-07-07 2:23 (sage)

>>15
It DOES matter that the variable has not been initialized when you are trying to output it before it has been assigned a value.  And stop saying "when you initialize int a", its called DECLARING the variable.  INITIALIZING is setting the initial value.

Name: Anonymous 2006-07-07 9:53 (sage)

when you initialize int a, it's the equivilent to saying int a = 0.

Hahaha, oh wow.

Enjoy your buggy non-portable program!

Name: mnu 2006-07-08 15:16 (sage)

LOL.. I got e-owned. First time in a while.

I learned something!! whee.

Name: Anonymous 2009-01-14 14:02

Satori

Name: Anonymous 2009-08-17 0:38

Lain.

Name: Anonymous 2010-06-07 6:51

Hi, I can spam /prog/ too, you faggot.

Also, smoke weed everyday.

Name: Anonymous 2010-12-08 19:32

Name: Anonymous 2011-01-31 20:11

<-- check em dubz

Name: Anonymous 2011-02-03 5:48

Name: Anonymous 2011-02-04 15:27

<

Name: Sgt.Kabu๥㍐kiman⵲龷 2012-05-28 19:28

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List