Probably a really simple problem.

This is a discussion on Probably a really simple problem. within the C++ Programming forums, part of the General Programming Boards category; Code: if(Arrowkey == ENTERKEY) { bool Check = ItemBuyBox(Itemarray[Arrowkeyplace]); if(Check == false) { BuyBoxClear(); } else if(Check == true) { ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    114

    Probably a really simple problem.

    Code:
      
    if(Arrowkey == ENTERKEY)
    {
           bool Check = ItemBuyBox(Itemarray[Arrowkeyplace]);
            if(Check == false)
            {
                BuyBoxClear();
            }
            else if(Check == true)
            {
                BuyItem(Itemarray[Arrowkeyplace]);
            }
    }
    No matter how i look at this i am unaware of how it always takes the first if regardless of what the function returns to check. It also takes both ifs if the second one isn't else. Im really unsure why this is happening :/
    If i just put TRUE or FALSE it works fine :/

    Code:
            else if(Arrowkey == ENTERKEY)
            {
                switch(Arrowkeyplace)
                {
                    case 0:
                    {
                        return true;
                    }
                    case 1:
                    {
                        return false;
                    }
    
                }
            }
    This is the code that returns the BuyBox function.

    If i get it to return the value to cout it outputs
    184 for both true and false :/
    Last edited by Nathan the noob; 10-16-2010 at 01:02 AM.
    Who needs a signature?

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Show us the code for ItemBuyBox().

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Code:
    bool ItemBuyBox(string Itemname)
    {
        //Variable Setup//
        string Question = "Would you like to purchase";
        string Yes = "Yes";
        string No = "No";
        bool Buying = true;
        int Arrowkeyplace = 0;
        char Arrowkey = '-';
        //Setup Question//
        BuyBoxClear();
        SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 7 );
        ReadString(Question,0,1,LeftSide+45,TopSide+18,2);
        ReadString(Itemname + "?",0,1,LeftSide+45,TopSide+19,2);
        SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 6 );
        ReadString(Yes,0,1,LeftSide+45+Itemname.size()+1+1,TopSide+19,2);
        SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 7 );
        ReadString("/",0,1,LeftSide+45+Itemname.size()+1+1+Yes.size()+1,TopSide+19,2);
        SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
        ReadString(No,0,1,LeftSide+45+Itemname.size()+1+1+Yes.size()+3,TopSide+19,2);
        //The Whileloop that controls the contained menu of yes/no//
        while(Buying)
        {
            while(!kbhit())
            {
                Sleep(1);
            }
            if(kbhit())
            {
                Arrowkey = getch();
                if(Arrowkey == 0)
                {
                    Arrowkey = getch();
                }
            }
            if(Arrowkey == LEFTARROW)
            {
                Arrowkeyplace -= 1;
                if(Arrowkeyplace < 0)
                {
                    Arrowkeyplace = 1;
                }
            }
            else if(Arrowkey == RIGHTARROW)
            {
                Arrowkeyplace += 1;
                if(Arrowkeyplace > 1)
                {
                    Arrowkeyplace = 0;
                }
            }
            else if(Arrowkey == ENTERKEY)
            {
                switch(Arrowkeyplace)
                {
                    case 0:
                    {
                        return(true);
                    }
                    case 1:
                    {
                        return(false);
                    }
    
                }
            }
            if(Arrowkey == LEFTARROW || Arrowkey == RIGHTARROW)
            {
                if(Arrowkeyplace == 0)
                {
                    SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 6 );
                    ReadString(Yes,0,1,LeftSide+45+Itemname.size()+1+1,TopSide+19,2);
                    SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 7 );
                    ReadString("/",0,1,LeftSide+45+Itemname.size()+1+1+Yes.size()+1,TopSide+19,2);
                    SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
                    ReadString(No,0,1,LeftSide+45+Itemname.size()+1+1+Yes.size()+3,TopSide+19,2);
                }
                else if(Arrowkeyplace == 1)
                {
                    SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11 );
                    ReadString(Yes,0,1,LeftSide+45+Itemname.size()+1+1,TopSide+19,2);
                    SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 7 );
                    ReadString("/",0,1,LeftSide+45+Itemname.size()+1+1+Yes.size()+1,TopSide+19,2);
                    SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 6 );
                    ReadString(No,0,1,LeftSide+45+Itemname.size()+1+1+Yes.size()+3,TopSide+19,2);
                }
            }
        }
    }
    And it is surely contained in this and any functions that are user made out side of this are free of this error.
    Who needs a signature?

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Are the following variables global?
    Arrowkeyplace
    Arrowkey

    I would start by changing this...
    Code:
    if(kbhit())
    {
        Arrowkey = getch();
        if(Arrowkey == 0)
        {
            Arrowkey = getch();
        }
    }
    To this...
    Code:
    if(kbhit())
    {
        Arrowkey = getch();
        while (Arrowkey == 0)
        {
            Arrowkey = getch();
        }
    }

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    No those 2 variables are not global i declared them at the beginning of the header.
    The LEFTARROW RIGHTARROW ENTERKEY are global.
    Who needs a signature?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Quote Originally Posted by Nathan the noob
    No those 2 variables are not global i declared them at the beginning of the header.
    That actually makes it sound like they are global.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    No i meant its in the beginning of the header within both functions.
    Last edited by Nathan the noob; 10-16-2010 at 04:41 PM.
    Who needs a signature?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Say what? How about you just show an example of your header file with these functions and these variables?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    The function above shows how i init and use the variables.
    Who needs a signature?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 08:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM

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