My code is not running in Xcode, scanner issue

时间:2018-03-25 20:48:26

标签: c scanf series

#include <stdio.h>
#include <math.h>

#define M_PI 3.14

int main(void)
{
    // Declaring Variables
    float time, p1, p2, p3, voltage, p3_num, p3_dem, epsilon;
    int index, type;
    // Asking user for time and index values into equation
    printf("Please enter a time value: ");
    scanf("%f", &time);
    printf("Please enter an index value: ");
    scanf("%d", &index);
    // Calculate value for given time, part a, b, or c
    printf("Do you want to run type a, b, or c?: ");
    scanf("%d", &type);
    char again = 'Y';
    while (again == 'Y')
    {
        if (type == 'a') {
            int going = 'Y';
            while (going == 'Y')
            {
                // Different parts of formula to ensure accuracy
                printf("Please enter an index value: ");
                scanf("%d", &index);
                p1 = ((3 / 2) - 12 / pow((M_PI), 2));
                p2 = (1 / pow((double)(2 * index - 1), 2));
                p3_num = ((2 * index - 1) * M_PI * time);
                p3_dem = 3;
                p3 = cos(p3_num / p3_dem);
                voltage = p1 * p2 * p3;
                printf("time = %f", time);
                printf("index = %i", index);
                printf("voltage = %f", voltage);
                printf("Again? (Y/N)");
                scanf("%c", &going);
            }
        }
    }
}

There is more underneath, but this is the main bulk. What I am confused about is that none of this running, none. I start at the very beginning and comment out everything else, not even my print statement is running. What is wrong?

1 个答案:

答案 0 :(得分:0)

您应该将程序划分为更小的功能,以便您可以单独测试它们。

例如,这里是一个读取一个整数的函数。

int GetInteger(const char * msg)
{
    //read up on c style formatting here is a source http://en.cppreference.com/w/c/io/fscanf
    int i = 0;
    printf("%s", msg);
    scanf("%i", &i);
    printf("Program confirms: %i\n", i);
    return i;
}

这将有助于您前进。阅读SOLID原则和测试驱动开发。

相关问题