Thread: unknown error, urgent help needed

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    84

    unknown error, urgent help needed

    i have the following piece of code, which asks the player what they want to do. depending on their answer it is supposed to take the to the releven funtion. however after entering your choice nothing happens and i get a windows error, can someone please help?

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    84
    oops, my bad thats the important thing i forgot.

    cout<<"Would you like to gamble or hold? (g for gamble, h for hold)\n";
    cin>>gambleyn;
    if (stricmp(gambleyn,"g"))
    {
    cout<<"Gamble selected\n";
    gamble();
    }
    else if (stricmp(gambleyn, "h"))
    {
    cout<<"You have selected hold\n";
    getholreq();
    }

    just realised the code compiles with no errors or warnings though. but the progarm just holds after you enter your choice, then throws in the windows error. dosen't even get to the functions.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    84
    just updated the prog, was using the old one. when comparing the user input so it will go to the function i get this error -

    C:\Documents and Settings\computing\Desktop\fruit machine\game.cpp(70) : error C2446: '==' : no conversion from 'char *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\Documents and Settings\computing\Desktop\fruit machine\game.cpp(70) : error C2040: '==' : 'int' differs in levels of indirection from 'char [2]'
    C:\Documents and Settings\computing\Desktop\fruit machine\game.cpp(75) : error C2446: '==' : no conversion from 'char *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\Documents and Settings\computing\Desktop\fruit machine\game.cpp(75) : error C2040: '==' : 'int' differs in levels of indirection from 'char [2]'
    Error executing cl.exe.

    on this bit of code -

    if (gambleyn == "g")
    {
    cout<<"Gamble selected\n"; //sends program to corresponding function, depending on user responce to previous question
    this ->gamble();
    }
    else if (gambleyn == "h")
    {
    cout<<"You have selected hold\n";
    this ->getholreq();

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    84
    also why is this sectio of code not working? it is supposed to end the game if the credit level reaches 0.

    cout<<"Would you like to gamble or hold? (g for gamble, h for hold)\n"; //askes player if they want to hold or gamble
    cin>>gambleyn;
    if (gambleyn == "g")
    {
    cout<<"Gamble selected\n"; //sends program to corresponding function, depending on user responce to previous question
    this ->gamble();
    }
    else if (gambleyn == "h")
    {
    cout<<"You have selected hold\n";
    this ->getholreq();
    }

    checkforwin(); //uses checkforwin function to see if winning line was generated

    if (credit = 0)
    {
    cout<<"You have run out of credits\n"; //ends players game if they run out of credit
    cout<<"Thank you for playing\n";
    }
    }

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    "g" is a string 'g' is a character. You cannot compare strings with if (x == "g") you need to use a string comparator like strcmp(). You can compare a value to a character if ( x == 'g').

    Without seeing how you are declaring these values it is impossible to say more.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    84
    sorted it now.

    but i have a problem with the gameble feature, it runs the loop without any user input. this is the code, please help.

    game::gamble()
    {
    char guess;

    cout<<"Winning a gamble wins 5 credits\n";
    gambleval = (rand () % 10) + 1; //generates random number for gamble function
    cout<<" "<<gambleval<<endl;

    cout<<"Higher or lower? correct guess wins.(H for high, l for low)\n";
    cin>>guess;

    if (guess == 'h')
    {
    cout<<"You have guessed higher\n"; //tells player what they guessed
    }
    else
    {
    cout<<"You have guessed lower\n";
    }

    gambleval2 = (rand () % 10) + 1; //generates second random number for gamble function

    if (gambleval2 >= gambleval && (guess == 'h'))
    {
    {
    cout<<"You guessed correct\n"; //if the second number is greater than the first, then it works this out and
    credadd =5; //tells the user they guessed correctly or not
    }
    }
    else
    {
    cout<<"You guessed wrong\n"; //tells the user they guessed wrong in the circumstances
    }

    if (gambleval >= gambleval2 && (guess == 'l'))
    {
    {
    cout<<"You guessed right\n"; //if the second number was smaller than the first then this works it out and
    credadd = 5; //tells the user they guessed correctly
    }
    }
    else
    {
    cout<<"You guessed wrong\n"; //tells the user they guessed wrong under the circumstances
    }
    addcredit(); //calls the update credit function to update the amount of credits in case the player wins
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. How to make unknown size arrays?
    By KneeLess in forum C Programming
    Replies: 6
    Last Post: 09-08-2003, 12:08 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM