【问题标题】:scanf not passing value to variablescanf没有将值传递给变量
【发布时间】:2017-05-20 16:34:08
【问题描述】:

我想问一些关于使用 Xcode IDE 在 C 中的 scanf 的问题。如果我最初没有为变量选择设置值,那么每当我打开程序并输入任何选择(1/2)时,它都会每次都进入 else case。所以我在选择任何选项后检查值然后我得到一个奇怪的数字。你能看看我的代码吗?提前谢谢你。

这是我的实际代码:

    /* Bubble Sort using MPI */

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

#define N 1000

double startT,stopT;

double startTime;

void showElapsed(int id, char *m)
{
    printf("%d: %s %f secs\n",id,m,(clock()-startTime)/CLOCKS_PER_SEC);
}

void showVector(int *v, int n, int id)
{
    int i;
    printf("%d: ",id);
    for(i=0;i<n;i++)
        printf("%d ",v[i]);
    putchar('\n');
}

int * merge(int *v1, int n1, int *v2, int n2)
{
    int i,j,k;
    int * result;

    result = (int *)malloc((n1+n2)*sizeof(int));

    /*
     i : pointer of v1
     j : pointer of v2
     k : pointer of k
     */
    i=0; j=0; k=0;
    while(i<n1 && j<n2)
        if(v1[i]<v2[j])
        {
            result[k] = v1[i];
            i++; k++;
        }
        else
        {
            result[k] = v2[j];
            j++; k++;
        }
    if(i==n1)
        while(j<n2)
        {
            result[k] = v2[j];
            j++; k++;
        }
    else
        while(i<n1)
        {
            result[k] = v1[i];
            i++; k++;
        }
    return result;
}

void swap(int *v, int i, int j)
{
    int t;
    t = v[i];
    v[i] = v[j];
    v[j] = t;
}

void sort(int *v, int n)
{
    int i,j;
    for(i=n-2;i>=0;i--)
        for(j=0;j<=i;j++)
            if(v[j]>v[j+1])
                swap(v,j,j+1);
}

int main(int argc, char **argv)
{
    int * data;
    int * chunk;
    int * other;
    int m,n=N;
    int id,p;
    int s;
    int i;
    int step;
    int choice = 0;

    //start asking user to select option between sequential or parallel version of BubbleSort
    printf(":: Welcome to BubbleSort Project for CSS333 ::\n");
    printf("Please select option that you prefer\n");
    printf("Type \"1\" for sequential mode or \"2\" for parallel mode\n");
    printf("");
    fflush(stdout);
    scanf("Enter here: %d", &choice);
    printf("Test value of choice(should be either 1 or 2): %d\n", choice);
    //end asking


    if(choice == 1){
        // do seq
        printf("You have selected option 1 which is running BubbleSort in Sequential mode\n");
        printf("Please wait...");
    }

    else if(choice == 2){
        // do parallel
        printf("You have selected option 2 which is running BubbleSort in parallel mode\n");
        printf("Please wait...");

        MPI_Status status;
        MPI_Init(&argc,&argv);
        MPI_Comm_rank(MPI_COMM_WORLD,&id);
        MPI_Comm_size(MPI_COMM_WORLD,&p);


        if(id==0)
        {
            int r;
            srandom(clock());
            s = n/p;
            r = n%p;
            data = (int *)malloc((n+p-r)*sizeof(int));
            for(i=0;i<n;i++)
                data[i] = random();
            if(r!=0)
            {
                for(i=n;i<n+p-r;i++)
                    data[i]=0;
                s=s+1;
            }

            startT = clock();

            MPI_Bcast(&s,1,MPI_INT,0,MPI_COMM_WORLD);
            chunk = (int *)malloc(s*sizeof(int));
            MPI_Scatter(data,s,MPI_INT,chunk,s,MPI_INT,0,MPI_COMM_WORLD);

            sort(chunk,s);
        }
        else
        {
            MPI_Bcast(&s,1,MPI_INT,0,MPI_COMM_WORLD);
            chunk = (int *)malloc(s*sizeof(int));
            MPI_Scatter(&data,s,MPI_INT,chunk,s,MPI_INT,0,MPI_COMM_WORLD);

            sort(chunk,s);
        }

        step = 1;
        while(step<p)
        {
            if(id%(2*step)==0)
            {
                if(id+step<p)
                {
                    MPI_Recv(&m,1,MPI_INT,id+step,0,MPI_COMM_WORLD,&status);
                    other = (int *)malloc(m*sizeof(int));
                    MPI_Recv(other,m,MPI_INT,id+step,0,MPI_COMM_WORLD,&status);
                    chunk = merge(chunk,s,other,m);
                    s = s+m;
                }
            }
            else
            {
                int near = id-step;
                MPI_Send(&s,1,MPI_INT,near,0,MPI_COMM_WORLD);
                MPI_Send(chunk,s,MPI_INT,near,0,MPI_COMM_WORLD);
                break;
            }
            step = step*2;
        }
        if(id==0)
        {
            FILE * fout;

            stopT = clock();
            printf("%d; %d processors; %f secs\n",N,p,(stopT-startT)/CLOCKS_PER_SEC);

            fout = fopen("result","w");
            for(i=0;i<s;i++)
                if (chunk[i] != 0)
                    fprintf(fout,"%d\n",chunk[i]);
            fclose(fout);
        }
        MPI_Finalize();
    }

    else{
        printf("Invalid value\n");
        printf("Program exiting...\n");
        exit(0);
    }

}

【问题讨论】:

  • 不编译:'int m,n=N;' N 未定义。请复制/粘贴您的实际代码。
  • 我认为scanf("Enter here: %d", &amp;choice);的意思是输入“在此处输入:”,而scanf是匹配包括文本“在此处输入:”
  • 太多代码无法阅读...请阅读以下内容:stackoverflow.com/help/mcve

标签: c


【解决方案1】:

这是你的问题:

scanf("Enter here: %d", &choice);

您可能希望它显示“在此处输入:”,然后接受一个数字作为输入并将其存储在变量 choice 中。但这不是它的作用。

它的作用是逐个字符地遍历格式化字符串(“在此处输入:%d”)。对于不是'%' 的每个字符,它从stdin 中读取一个字符并将它们一起比较。如果它们不匹配,它会将字符推回stdin 的缓冲区并停止扫描。

因此,除非用户键入以 Enter here: 开头的内容,然后紧跟一个数字,否则它无法读取该数字。

你可能想做的是:

printf("Enter here: ");
scanf("%d", &choice);

(然后阅读scanf() 的文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多