Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

can u tell me whats wrong pls!!

Ask ALL your questions here

can u tell me whats wrong pls!!

Postby Kopilatis on Mon Nov 02, 2009 12:16 pm

guys pls help me this is driving me crazy!!. i have a runtime error in that part of my code(tic tac toe) and i dunno why. can u spot the bug out for me?
Code: Select all
#include <iostream>
#include <string>

using namespace std;

int main()
{
    //Display the board
   
   
   
    const int ROWS = 3;
    const int COLLUMNS = 3;
   
    char board[ROWS][COLLUMNS] = {{'-', '-', '-'},
                                  {'-', '-', '-'},
                                  {'-', '-', '-'}};
   
    for(int i = 0;i < ROWS; ++i)
    {
       for(int j = 0;j < COLLUMNS; ++j)
       {
       cout << board[i][j];
       }
       cout<<endl;
    }
    //X or O?
   
   
   
    char charSelect, chara, oppChara;
    do
    {
       cout << "Please select whether you want to use an X or an O\n";
       cin >> charSelect;
       
if(charSelect == 'X'||charSelect == 'x')
        chara = 'X';
        if(charSelect == 'O'||charSelect == 'o')
        chara = 'O';
        else
        cout << "Invalid choice\n";
    }
        while(charSelect!='x'&&charSelect !='X'&&charSelect != 'o'&&charSelect != 'O');
       
        if (chara == 'X')
        oppChara = 'O';
        if(chara == 'O');
        oppChara = 'X';
       
        cout << "OK then your opponent has "<<oppChara<<endl;
       
       
       
       system("pause");
       
       return 0;
}
Kopilatis
 
Posts: 13
Joined: Fri May 29, 2009 7:47 pm

Re: can u tell me whats wrong pls!!

Postby noobgrammer on Mon Nov 02, 2009 1:32 pm

I don't now if your compiler didn't give you a warrning or not but you had a semicolon after a if statement

Code: Select all
  if(chara == 'O');


also here is a problem with your if at the top

Code: Select all
if(charSelect == 'X'||charSelect == 'x')
        chara = 'X';
        if(charSelect == 'O'||charSelect == 'o')
        chara = 'O';
        else
        cout << "Invalid choice\n";


you need to make the second if a else if

Code: Select all
  else if(charSelect == 'O'||charSelect == 'o')


because if you choose X then the second if will be false and run the else statement

I did not get an error myself so try that first and see if that was your problem
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: can u tell me whats wrong pls!!

Postby Kopilatis on Mon Nov 02, 2009 4:55 pm

thanks so much
Kopilatis
 
Posts: 13
Joined: Fri May 29, 2009 7:47 pm


Return to QA

Who is online

Users browsing this forum: No registered users and 0 guests