Help with loop

This is a discussion on Help with loop within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm making this game, you have to select 0-7 for a row and 0-7 for a column, Now this ...

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

    Help with loop

    Hey, I'm making this game, you have to select 0-7 for a row and 0-7 for a column, Now this is what I have:

    Code:
     char *column2;
       char *row2;
    
       do
       {
    
       do
       {
       	cout << " \nEnter column: C- ";
          cin >> column2;
          cin.ignore(80,'\n');
          column=atoi(column2);
    
       } while(column > '7');
    
       do
       {
    		cout << "\nEnter Row R- ";
          cin>> row2;
          cin.ignore(80,'\n');
    
          row=atoi(row2);
    
       } while(row > '7');
    
       if(board[row][column] == 'X')
       {
       	piecevalid++;
       }else{
       	cout << " \nPlease select a valid piece\n";
       }
    
       } while(piecevalid != 1);
    Now If i just did INT, then if someone typed C it would go in a infinate loop, now This prevents the infinite loop but ppl can enter 9 and E and it will have no affect, meaning that it will pass through the loop.

    IF I enetered F and F it would just continue on and say I selected 0,0.

    Any suggestions?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Use std::getline(cin, string)

    Kuphryn

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    194

    Re: Help with loop

    Code:
    char column2;
    char row2;
    
    
    do
    {
    
       do
       {
          cout << " \nEnter column: C- ";
          cin >> column2;
          cin.ignore(80,'\n');
          column=atoi(column2);
    
       } while(column > 7);  // column should be an int, not a char, so dont compare to a char
    
       do
       {
    		cout << "\nEnter Row R- ";
          cin>> row2;
          cin.ignore(80,'\n');
    
          row=atoi(row2);
    
       } while(row > 7);  //same with row. compare it to an int, not a char
    
       if(board[row][column] == 'X')
       {
       	piecevalid++;
       }else{
       	cout << " \nPlease select a valid piece\n";
       }
    
       } while(piecevalid != 1);
    your code was not behaving correctly, because you were comparing an int, to the ascii value of the char '7' instead of comparing to 7

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    14
    I get:
    Error: checkers.cpp(320,27):Cannot convert 'int' to 'const char *'
    Error: checkers.cpp(320,27):Type mismatch in parameter '__s' in call to 'atoi(const char *)'
    Error: checkers.cpp(330,21):Cannot convert 'int' to 'const char *'
    Error: checkers.cpp(330,21):Type mismatch in parameter '__s' in call to 'atoi(const char *)'

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    oops, sorry about that
    change the vars to

    char column2[10], row2[10];

    that should work

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    14
    alright thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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