C 每个数字的输出是否相同?

C 每个数字的输出是否相同?,c,pointers,math,output,C,Pointers,Math,Output,该程序要求用户输入一个正整数(整数可以是整数类型范围内的任意位数),并将每个数字替换为该数字加上6模10的总和。然后,程序应在显示输出之前将第一个数字与最后一个数字交换 输入/输出示例: Enter the number of digits of the number: 5 Enter the number: 92828 Output: 48485 出于某种原因,在我的代码中,无论我输入什么数字,所有结果都是6。(因此,如果我输入5个数字,我得到6666)。我不熟悉指针,所以这有问题吗,或者我

该程序要求用户输入一个正整数(整数可以是整数类型范围内的任意位数),并将每个数字替换为该数字加上6模10的总和。然后,程序应在显示输出之前将第一个数字与最后一个数字交换

输入/输出示例:

Enter the number of digits of the number: 5
Enter the number: 92828
Output: 48485
出于某种原因,在我的代码中,无论我输入什么数字,所有结果都是6。(因此,如果我输入5个数字,我得到6666)。我不熟悉指针,所以这有问题吗,或者我只是有一些数学错误?程序运行时没有任何编译器警告

#include <stdio.h>
#include <stdlib.h>

void replace(int *a, int *b, int n);
void swap(int *p, int *q);

int main()
{
    int n = 0;
    int i = 0;
    int a[100], b[100];

    //Prompt user to enter number of digits
    printf("Enter the number of digits you'd like to replace: ");
    scanf("%d", &n);

    //Prompt user to enter the number to use
    printf("Enter  the number to use: ");

    for(i = 0; i < n; i++);
        scanf("%1d", &a[i]);

    //replace function
    replace(a, b, n);

    for(i = 0; i < n; i++)
        printf("%d", b[i]);
    printf("\n\n");
    return 0;
}

void replace(int *a, int *b, int n)
{
    int i;
    for (i = 0; i < n; i++)
    {
      *(b+i) = (*(a+i)+ 6) % 10;
    }
    printf("The output is: ");

    //swap function
    swap(b, (b+ (n-1)));
}

void swap(int *p, int *q)
{
    int t;
    t = *p;
    *p = *q;
    *q = t;
}
#包括
#包括
无效替换(int*a、int*b、int n);
无效交换(int*p,int*q);
int main()
{
int n=0;
int i=0;
INTA[100],b[100];
//提示用户输入位数
printf(“输入要替换的位数:”);
scanf(“%d”和“&n”);
//提示用户输入要使用的号码
printf(“输入要使用的编号:”);
对于(i=0;i
除了以下代码片段中的一个愚蠢错误外,您的代码是绝对正确的

for(i = 0; i < n; i++);
    scanf("%1d", &a[i]);

现在它工作正常。

代码中的罪魁祸首是for循环后面的分号:

for(i = 0; i < n; i++)**;**
    scanf("%1d", &a[i]);
(i=0;i** scanf(“%1d”、&a[i]);
因此,您编写的scanf基本上是for循环之外的,并将第一个数字存储到一个[n]中。

始终是一些小事情。没有任何编译器警告,我不知道去哪里找。谢谢您可以使用-g编译代码,并使用gdb:)@ch.sarath进行调试。我建议
cc-Wall a.c
我不确定gcc是否会将空for循环报告为警告。@ch.sarath它并不总是简单的。谢谢你的帮助!
for(i = 0; i < n; i++)**;**
    scanf("%1d", &a[i]);