简体   繁体   English

scanf无法读取输入的问题

[英]Problems with scanf not reading the input

These were the instructions: 这些是说明:
Write a program that starts out asking the user for the mean u and standard deviation s for the normal distribution (see the wiki article http://en.wikipedia.org/wiki/Normal_distribution ) The program then asks for an N, and then asks for N values x. 编写一个程序,开始向用户询问正态分布的均值u和标准差s(请参阅Wiki文章http://en.wikipedia.org/wiki/Normal_distribution ),然后该程序要求输入N,然后输入N要求N值x。 For each x it writes out f(x) to the screen. 对于每个x,它将f(x)写入屏幕。 Note that the program asks the user for u, s, and N just once. 请注意,该程序仅向用户询问u,s和N。 After that it asks for N values for x, one by one. 之后,它要求x的N个值,一个接一个。 After each value x it writes out the corresponding value of the function. 在每个值x之后,它将写出函数的相应值。

And this is my code: 这是我的代码:

#include <stdio.h>

#define PI 3.141592653589793238462643383
#define E 2.7182818284590452353602874713526624977572470937
#include <math.h>
#include <stdlib.h>

int main()
{
    double u,s, N, x1,math1, math2, math3,n, v, x;
    printf("Enter Mean: \n");
    scanf("%d", &u);
    printf("Enter Standard Deviation: \n");
    scanf("%d", &s);
    printf("Enter number of x's \n");
    scanf("%d", &N);

for (v=1; v<=N; v++)
{
     printf("Enter Value \n");
     scanf("%d", &x);
     n=1/2;
         math1 =1/(u*sqrt(2*PI));
         math2= (x-u)/s * (x-u)/s;
         math3= E * exp(n);
         x1 = math1 * exp(math3)*exp(math2);
         printf("%d \n", x1);
    }
    system("Pause");
 }

My program just stops after "Enter number of X's. Can anyone help me figure out why this is? 我的程序在“输入X的数量”之后停止。有人可以帮助我找出原因吗?

Passing incorrect format strings to scanf() invokes undefined behvaiour. 将错误的格式字符串传递给scanf()会调用未定义的行为。 All format specifiers should be %lf as the values are double : 所有格式说明符都应为%lf因为值是double

scanf("%lf", &u);

etc. 等等

Due to this the for loop is not entered and stops at pause . 因此,不会进入for循环,并在pausepause

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM