Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C程序调用字符串到int函数,我无法转换输入_C_String_Int_Main - Fatal编程技术网

C程序调用字符串到int函数,我无法转换输入

C程序调用字符串到int函数,我无法转换输入,c,string,int,main,C,String,Int,Main,我想将字符串转换为int,并从main调用函数。其中,第一个字符是一个字母,声明数字的基数,字符串中的其余字符是数字。我可以让函数单独工作,但当使用main函数调用时,它将无法打印出正确的值 使用二进制文件的用户输入示例: b1000 b1010 结果应该是: b b 1000 1010 代码如下: #include <stdio.h> #include <string.h> #include <math.h> int str_to_int(inputb

我想将字符串转换为
int
,并从
main
调用函数。其中,第一个字符是一个字母,声明数字的基数,字符串中的其余字符是数字。我可以让函数单独工作,但当使用
main
函数调用时,它将无法打印出正确的值

使用二进制文件的用户输入示例:

b1000
b1010
结果应该是:

b
b
1000
1010
代码如下:

#include <stdio.h>
#include <string.h>
#include <math.h>

int str_to_int(inputbase) {
    char num1[50];
    num1[50] = inputbase;

    char numcpy1[sizeof(num1) - 1];

    int  i, len1;
    int result1 = 0;

    //printf("String: ");
    //gets(num1);

    //Access first character for base
    printf("%c \n", num1[0]);

    //Remove first character for number1 and number 2
    if (strlen(num1) > 0) {
        strcpy(numcpy1, &(num1[1]));
    } else {
        strcpy(numcpy1, num1);
    }
    len1 = strlen(numcpy1);

    //Turn remaining string characters into an int
    for (i = 0; i < len1; i++) {
        result1 = result1 * 10 + ( numcpy1[i] - '0' );
    }

    printf("%d \n", result1);
    return result1;
} 

int main() {
    char *number1[50], *number2[50];
    int one, two;

    printf("\nAsk numbers: \n");

    gets(number1);
    gets(number2);

    one = str_to_int(number1);
    two = str_to_int(number2);

    printf("\nVerifying...\n");
    printf("%d\n", one);
    printf("%d\n", two);

    return 0;
}
#包括
#包括
#包括
int str_to_int(inputbase){
charnum1[50];
num1[50]=输入基;
char numcpy1[sizeof(num1)-1];
inti,len1;
int result1=0;
//printf(“字符串:”);
//获取(num1);
//访问基的第一个字符
printf(“%c\n”,num1[0]);
//删除数字1和数字2的第一个字符
如果(strlen(num1)>0){
strcpy(numcpy1和num1[1]);
}否则{
strcpy(numcpy1,num1);
}
len1=strlen(numcpy1);
//将剩余的字符串转换为int
对于(i=0;i
我想您的代码无法编译,因为有些错误

第一个已经排好了

 int str_to_int(inputbase)
其中,
inputbase
的定义没有类型

如果更改为

 int str_to_int(char * inputbase)
下一个需要改进的点是一致的

 num1[50] = inputbase;
这样的赋值有一组错误:

  • num1[50]
    表示访问第51项,但只有50项从0到49索引
  • 语句
    num1[0]=inputbase(以及任何其他正确的索引)是错误的,因为类型不同:
    num1[0]
    char
    ,但
    inputbase
    是指针
  • num1=inputbase也是错误的(对于复制字符串<代码>=/COD>不能在C中使用,所以考虑制作循环或使用标准库函数<代码> STRNCPY )

  • 由于这只是问题的开始,我建议从十进制输入开始,使用一些标准函数将
    char*
    字符串转换为
    int
    (例如
    atoi
    ,或
    sscanf
    ),然后,在检查程序并发现其正确性(如果需要)后,您可以避免使用标准转换,并编写自己的
    str\u to\u int
    函数的原型
    str\u to\u int()
    应指定
    intputbase
    的类型。您正在传递一个字符串,而
    str_to_int
    没有理由修改此字符串,因此类型应为
    const char*inputbase

    此外,您不需要字符串的本地副本,只需访问第一个字符以确定基数,并相应地解析其余数字:

    #include <stdlib.h>
    
    int str_to_int(const char *inputbase) {
        const char *p = inputbase;
        int base = 10;    // default to decimal
    
        if (*p == 'b') {  // binary
            p++;
            base = 2;
        } else
        if (*p == 'o') {  // octal
            p++;
            base = 8;
        } else
        if (*p == 'h') {  // hexadecimal
            p++;
            base = 16;
        }
        return strtol(p, NULL, base);
    } 
    
    #包括
    int str_to_int(常量字符*inputbase){
    const char*p=inputbase;
    int base=10;//默认为十进制
    如果(*p=='b'){//binary
    p++;
    基数=2;
    }否则
    如果(*p=='o'){//octal
    p++;
    基数=8;
    }否则
    如果(*p=='h'){//十六进制
    p++;
    基数=16;
    }
    返回strtol(p,NULL,base);
    } 
    
    int str\u to\u int(inputbase)
    是否在您的机器上编译?你在用什么编译器?对我来说,这看起来很像一个家庭作业问题。我在用gcc编译器。函数int str_to_int(inputbase)未编译。由于不提倡使用
    strncpy()
    ,因此它从来都不是正确的工具,特别是对于初学者。还不鼓励使用
    get()