runtime error

This is a discussion on runtime error within the C++ Programming forums, part of the General Programming Boards category; howdy, this code is a random letter game, it selects a letter at random then gives the user 10 chances ...

  1. #1
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    runtime error

    howdy,
    this code is a random letter game, it selects a letter at random then gives the user 10 chances to guess. i have not implemented th do/while loop to allow for all 10 guesses.
    my question is:
    this code compiles with no errors and executes all of the way through about half the time. the rest of the time it executes all of the way through and issues a segmentation fault error.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    void guess (char* letter);
    
    using namespace std;
    int main ()
    {
      system("clear");
      char  alphabet[][26] = {
       {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'},
      {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}
    };
    
    srand(time(0));
    {
        int u_l = (rand()%2);
        int alph_answer = (rand()%26);
        char alpha_rand = alphabet[u_l][alph_answer];
      cout<<"Random letter: "<<alpha_rand<<endl;
      
      guess(&alpha_rand);
    	
      } 
       return 0;
      }
    
      void guess (char* letter)
      {
      char* alpha_guess;
      int guess_num = 1;
      unsigned int result = 0;
      cout<<"I am thinking of a letter\n";
      cout<<"it could be upper or lower case\n";
      cout<<"I'll give you 10 trys to guess what it is\n";
      cout<<"Take a try.\n";
      int g;
      int i;
     
     cin>>alpha_guess;
      i = *letter;
      g = *alpha_guess;
     cout<<"Guess: "<<g<<endl;
     cout<<"Letter: "<<i<<endl;
     // result = (strcmp (alpha_guess, letter));
      cout<<"Result: "<<result<<endl;
      if(i != g)
      {
        cout<<"Wrong! Try again.\n";
        cout<<"you've guessed: "<<guess_num<<" times"<<endl;
        cout<<"you have: "<<(10 - guess_num)<<" trys remaining"<<endl;
        guess_num++;}
    else
      cout<<"Correct! you got it in: "<<guess_num<<" guesses"<<endl;
    }

    what could the problem be?

    thanks
    M.R.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    should be:

    Code:
    guess (char letter);
    
    char alpha_guess;
     cin>>alpha_guess;
      i = letter;
      g = (int)alpha_guess;
    dont stuff around with pointers unless you really need to!

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21