简体   繁体   中英

C++ Access Violation with atoi() function

#include "stdafx.h"
#include <list>
#include <iostream>
#include <string>
using namespace std;

class Canvas
{
public:
    void CleanCanvas(Canvas * Canvas);
    int canvas[10][10];
    void setCoords(int X,int Y,bool run);
    bool Win(Canvas * Canvas,string * WinningPlayer);
}CANVAS;

int _tmain(int argc, _TCHAR* argv[])
{
    string coords;
    string temp;
    int X,Y;
    bool flag = true;
    bool run = false;
    string  WinningPlayer = "";
    system("color 02");
    printf("------------------------------//TIC-TAC-TOE//------------------------\n\n\n\n\n");
    printf("          ***********************************************           \n\n\n");
    printf("Example: player1- 3,3 to put a mark on 3 line of the 3 column\n\n\n\n\n\n");
    CANVAS.CleanCanvas((Canvas*)&CANVAS.canvas);
    cin.get();
    while(flag)
    {
        printf("player1 make the mov: "); 
        cin >> coords;
        X = atoi((char *)coords.at(0));
        Y = atoi((char *)coords.at(2));
        if(CANVAS.canvas[X][Y] != 0)
        {
            printf("Coords alrady taken...set new");
            cin >> coords;
            X = coords.at(1);
            Y = coords.at(3);
        }
        run = false;
        CANVAS.setCoords(X,Y,run);
        if(CANVAS.Win((Canvas*)&CANVAS.canvas,&WinningPlayer) == true)
        {
            printf("%s Win",WinningPlayer);
            flag = false;
        }

        printf("player2 make the mov: ");
        cin >> coords;
        X = coords.at(1);
        Y = coords.at(3);
        if(CANVAS.canvas[X][Y] != 0)
        {
            printf("Coords alrady taken...set new");
            X = coords.at(1);
            Y = coords.at(3);
        }

    } 

    cin.get();
    return 0;

}

void Canvas::setCoords(int X,int Y,bool run)
{
    if(run == false)
    {
        CANVAS.canvas[X][Y] = 1;
    }else
    {
        CANVAS.canvas[X][Y] = 2;
    }
}

void Canvas::CleanCanvas(Canvas * Canvas)
{
    for (int i = 0; i <= 3; i++)
    {
        for(int a = 0; a <= 3; a++)
        {
            Canvas->canvas[i][a] = 0;
        }
    }
}

bool Canvas::Win(Canvas * Canvas,string * WinningPlayer)
{
    int count = 0;
    for(int i = 0; i <= 3; i++)
    {
        for(int a = 0; a <= 3; a++)
        {
            switch(Canvas->canvas[i][a])
            {
            case 1: count +=1;
                break;
            case 2: count *=2 + 1;
                break;
            }
            if(count == 9)
            {
                WinningPlayer = (string *)"player2";
                return true;
            }else if (count == 3)
            {
                WinningPlayer = (string *)"player1";
                return true;
            }
        }
    }
}

The code above give me an access violation that i saw come from the

X = atoi((char *)coords.at(0));
Y = atoi((char *)coords.at(2));

I've tried to cast coords.at in a temporany variables but it didn't work Don't look at the code because it's just a shot so it's not suppose to handle exception... i need help to figure out what the real prblem is...thanks!

coords.at(0) returns a char , not a char* . Try to parse your std::string first, transform each number at a char* , and so, you use atoi .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM