Thread: My program causes my compiler to crash

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    14

    Unhappy My program causes my compiler to crash

    I am coding a "guess the number" program and I hope someone can tell me what I have done wrong with my program. When I compile this program I do not get any errors or warning. It prints the intro to the screen correctly but when I put in my first guess it crashes the compiler. I am stuck and hope someone can point out my mistake.

    Sorry for posting the whole program but since I am not sure where the error is happening in it felt I needed to show it all.

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <cstdio> 
    
    #include <cstdlib> 
    
    #include <ctime> 
    
    int randomNumber(); 
    void test(int yourGuess, int rnum); 
    void intro(); 
    
    void main(void) 
    { 
    int i=0; 
    
    int rnum=0; 
    
    int yourGuess = 0; 
    
    int count = 0; 
    
    long ltime = time(NULL); 
    
    unsigned stime = (unsigned)(ltime/2); 
    
    intro(); 
    
    cout << "OK, I am thinking of a number. Try to guess it.\n"; 
    
    cin >> "%d", &yourGuess; 
    
    srand(stime); 
    
    for (i=0;1<1000;i++) 
    { 
    rnum=randomNumber(); 
    test( yourGuess, rnum); 
    } 
    
    return; 
    } 
    
    void intro(void) 
    { 
    cout << "Welcome to the game of Guess It!\n"; 
    cout <<  "I will choose a a number between 1 and 1000.\n"; 
    cout <<  "You will try to guess that number. If you guess wrong.\n"; 
    cout << "I will tell if you are to high or to low.\n"; 
    cout <<  "You have 6 tries to get the number.\n"; 
    } 
    
    int randomNumber() 
    { 
    int number =-99999; 
    
    number=((rand()+101) % 1000); 
    
    return number; 
    } 
    
    void test(int yourGuess, int rnum) 
    { 
    if (yourGuess < rnum) 
    cout << "Too Low.\n"; 
    
    else if (yourGuess > rnum) 
    cout << "Too High.\n"; 
    
    else if (yourGuess > rnum && yourGuess > 1000) 
    cout << "Illegal Guess! Your guess must be between 1 and 1000.\n"; 
    
    else if (yourGuess < rnum && yourGuess < 1) 
    cout << "Illegal Guess! Your guess must be between 1 and 1000.\n"; 
    else 
    cout << "You Guess it!\n"; 
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    cin >> "%d", &yourGuess;

    That is not how you use cin. It doesn't work like scanf.

    Code:
    cin >> yourGuess;

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > It prints the intro to the screen correctly but when I put in my first guess it crashes the compiler.
    Do you really mean crash, or do you just mean output error message and doesn't produce a program for you to run?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    If it can print the intro, it's a seperate program already, and should have no way of it's own to crash your compiler.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Anyway, after compiling it, I see your problem. It doesn't "crash", but enter an infinite loop somewhere. It's running a loop over and over again.

    Note that you only ask for user input once, not six times as you should have.

    And you generate randomNumber() over and over again.

    Worst of all is this typo. Find it:
    Code:
    for(i = 0; 1 < 1000; i++)
    A simple typo can mess up an entire program.


    Besides that, your test() function should test for invalid values first. Otherwise they are never tested; the function only prints "Too Low" or "Too High" no matter what invalid values you enter.

    This does not produce a number between 1 and 100, but a number between 0 and 999:
    Code:
    int randomNumber() 
    {
    int number =-99999;
    number=((rand()+101) % 1000);
    return number; 
    }
    Because the "%" operator will only return a number between 0 and 999.

    Code:
    int randomNumber() 
    { 
    int number =-99999;
    number=rand() % 1000 + 1;
    return number;
    }

    Note that you can initialize variables in loops, unlike in C:
    Code:
    for(int i = 0;)

    Tip: the correct code snippet for main() should resemble

    Code:
    rnum = randomNumber();
    	for(int i = 0; i < 6; i++)
    	{
    		cin >> yourGuess;
    		if(test(yourGuess, rnum))
    		{
    			break;
    			win = 1;
    		}
    	}
    	if(win)
    	{
    		cout << "You Guessed it!\n";
    	}
    	else
    	{
    		cout << "You Lose!\n";
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compiler error in sample program for my class
    By dopedopeson in forum C Programming
    Replies: 3
    Last Post: 05-28-2009, 01:14 PM
  2. Problem using structures (they crash my program)
    By shadow1515 in forum C Programming
    Replies: 7
    Last Post: 06-17-2008, 08:36 AM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. OpenScript2.0 Compiler
    By jverkoey in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 01:52 PM
  5. Diff compiler == diff program????
    By Imperito in forum C++ Programming
    Replies: 21
    Last Post: 04-25-2002, 12:50 PM