简单c++程序Id的问题返回1退出状态

Problems with simple c++ program-Id returned 1 exit status

本文关键字:返回 退出 状态 问题 c++ 程序 Id 简单      更新时间:2023-10-16

所以我这样做是为了学校,我不知道问题出在哪里程序的任务是读取输入的数字,然后像123一样从后面开始写,它会返回321我写的代码对我来说似乎很好,但唯一的错误是Id返回1退出状态。我完全不知道这么小的代码会有什么错这里是。为了更容易理解,Nc是数字,s是乘数。c就像我在while循环中从n除以10之前提取的最后一个数字。n是输入数,n1是所需数。

    int main(int argc, char** argv) {
 int n,nc,n1,c,s;
 printf("Enter number: n");
 scanf("%d",&n);
 while(n>0){
    n/=10;
    nc+=1;
 }
 s=pow(10,nc);
 while(n>0){
    c=n%10;
    n1+=c*s;
    s/=10;
    n/=10;
 }
printf("New number is %d",n1);
system("pause");
return 0;

}

int string2int(string input)
/// converts a string into an integer
{
    int output = 0;
    unsigned len = input.length();
    for (unsigned i = 0; i < len; i++)
    {
        char u = input[i]; // getting char from string
        int y = int(u); y -= '0'; // using character subtraction to make int
        output += y * pow(10, len - i - 1); // moving digit to appropriate spot
    } // the len - i - 1 is what makes the output go in correct order
    return output;
}

需要注意的是,pow在这种情况下是我创建的一个函数,它只是使用一个循环,如果你想使用标准的pow,你应该对结果进行四舍五入,还应该注意这个函数使用字符串。

为了阅读用户的输入,我建议使用std::cin,它使用与std::cout类似的语法,并且更方便