如何将2d字符数组转换为2d int数组? [英] How to convert a 2d char array to a 2d int array?

查看:142
本文介绍了如何将2d字符数组转换为2d int数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  char puzzle [i] [j]; 
int i,j,count = 0;
char value [81];

for(i = 0; i <9; i ++){
for(j = 0; j< 9; j ++){
cin> value [count];
puzzle [i] [j] = value [count];
count ++;
}}

这是我到目前为止。我尝试使用atoi,但我需要一个char * str。
输入是:..4545 ..(数字和句点)

我试图将字符拼图[i] [j]转换为int拼图[i] [j] 。 char数组目前拥有..4545 ..,我想隐藏它,所以它只保存整数00454500。

解决方案

p> 参与精神调试器



执行Jedi的技巧...
$ b

这是你想要的代码:

  int puzzle [9] [9] // changed type 
int i,j,count = 0;
char value [81];

for(i = 0; i <9; i ++){
for(j = 0; j< 9; j ++){
cin> value [count];
puzzle [i] [j] = value [count] - '0'; //从ASCII数字转换为整数
count ++;
}
}


char puzzle[i][j];  
int i,j,count=0;  
char value[81];

for( i = 0; i < 9; i++){  
  for( j = 0; j < 9; j++){  
    cin >> value[count];  
    puzzle[i][j] = value[count];  
    count++;  
  }}

This what I have so far. I tried using atoi but I needed a char * str. The input is: ..4545.. (numbers and periods)
I'm trying to convert the char puzzle[i][j] to an int puzzle[i][j]. The char array currently holds "..4545.." and I want to covert it so it holds just integers "00454500".

解决方案

Engaging psychic debugger...

Performing Jedi mind tricks...

This is the code you want:

int puzzle[9][9];  // changed type
int i,j,count=0;  
char value[81];

for( i = 0; i < 9; i++ ) {  
  for( j = 0; j < 9; j++ ) {  
    cin >> value[count];  
    puzzle[i][j] = value[count] - '0';  // convert from ASCII digit to integer
    count++;  
  }
}

这篇关于如何将2d字符数组转换为2d int数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆