scanf没有将值传递给变量

时间:2017-05-20 16:34:08

标签: c

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

这是我的实际代码:

    /* 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);
    }

}

1 个答案:

答案 0 :(得分:0)

这是你的问题:

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

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

这样做是通过格式化字符串(“输入此处:%d”),逐个字符。对于不是'%'的每个字符,它会从stdin中读取一个字符并将它们进行比较。如果它们不匹配,则会将字符推回到stdin的缓冲区并停止扫描。

因此,除非用户输入以Enter here:开头的内容,然后紧跟一个数字,否则会在读取该数字时失败。

您可能想要做的是:

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

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