Tic Tac Toe loop

This is a discussion on Tic Tac Toe loop within the C++ Programming forums, part of the General Programming Boards category; Hi, I have to write tic tac toe for a class and I have never even written a loop. Obviously ...

  1. #1
    Your Father
    Join Date
    Sep 2005
    Posts
    34

    Tic Tac Toe loop

    Hi, I have to write tic tac toe for a class and I have never even written a loop. Obviously I haven't put my study time in, but that's besides the point. Can someone please explain to me why this keeps repeating
    "Please make a selection:" even after I enter 1 or 2? It's probably obvious.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    void printMenu();
    float makeSel();
    int main()
    {
     do{
      printMenu();
      makeSel();
      //game goes here
      cout<<"game"<<endl;
     
     }while (makeSel()!=2);
     
     
      
    
     return 0;
     }
    void printMenu()
    {
     cout<<"Welcome to Tic-Tac-Toe!"<<endl<<"Three in a row wins!"<<endl;
     cout<<"        1).Play a game."<<endl<<"        2).Quit"<<endl;
    }
    float makeSel()
    {
     float sel;
     do{
      cout<<"Please Make a Selection: ";
      cin>>sel;
       } while(sel!=1 ||sel!=2);
    
     
     
     return sel;
    }

  2. #2
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    int main()
    {float x;
     do{
      printMenu();
      x=makeSel();
      //game goes here
      cout<<"game"<<endl;
     
     }while (x!=2);
    catch the value of function and then compare it.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  3. #3
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Why are you storing the user input into a float?
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    while(sel!=1 || sel!=2), say sel is 2, so then it becomes while(1 || 0), which becomes while(1). You're saying that if sel is not equal to 1, or 2, then it continues, but you have it in two different statements so its basicly expecting sel to be two numbers. What you want is sel != 1 && sel != 2, that way its only going to return true if both are true, and hence sel isn't equal to 1 or 2.

    I'd also lose the float, it isn't necessary.. just pointless.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Your Father
    Join Date
    Sep 2005
    Posts
    34
    what would happen if some clever idiot decided to put 3.3 as an answer?


    thank you for the help

  6. #6
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Or how about a letter or string!!
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  7. #7
    Your Father
    Join Date
    Sep 2005
    Posts
    34
    ugh im glad im getting out of this major

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Code:
    #include <climits>
    
    inline void ClearStream(std::istream& in)
    {
      in.clear ();
      in.ignore (std::numeric_limits <std::streamsize>::max (), '\n');
    }
    
    inline bool InputCheck()
    //returns true (1) if cin did not fail
    {
      if (std::cin.fail()) {
        std::cerr << "Error: invalid input entry.\n";
        ClearStream (std::cin);
      } else {
        return 1;
      }
    
      return 0;
    }
    
    int makeSel()
    {
      int sel;
      do{
        cout<<"Please Make a Selection: ";
        cin>>sel;
      } while(InputCheck() != 1);
    
      return sel;
    }
    Don't conform to idiots - prevent their cleverness.

    The reason for ClearStream() is because if the wrong datatype is put into the stream, cin will keep using that as its input and keep looping through that (the solution to that clever person).
    Last edited by Dae; 10-29-2005 at 09:57 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Your Father
    Join Date
    Sep 2005
    Posts
    34
    dae, thanks for the help, but if i used that in my program my teacher would probably smack me and ask me how i got it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making Tic Tac Toe Smarter
    By abh!shek in forum C Programming
    Replies: 15
    Last Post: 06-05-2008, 11:43 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 07:25 PM
  3. Tic Tac Toe... so close...
    By SlayerBlade in forum C Programming
    Replies: 14
    Last Post: 10-10-2005, 09:58 PM
  4. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM
  5. Tic Tac Toe -- Can you guys rate this please?
    By Estauns in forum Game Programming
    Replies: 2
    Last Post: 09-15-2001, 11:22 AM

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