【发布时间】:2017-03-19 23:08:15
【问题描述】:
好的,所以我第一次在 Visual Studio 2015 中做一些学校作业,我的程序不会进入 if 语句,只是以代码 0 退出,我尝试删除 if 语句,它可以工作,但我需要拥有它。
#include <stdio.h>
void main() {
char c1, c2;
int controlPoint;
int counter, counterTwo;
float intSingle;
int isTrue = 0;
float* x;
float* y;
float* fx;
float sum = 0;
float temp;
int k;
printf(" \n Please select an option");
printf(" \n Press i to do an interlopation");
printf(" \n Press q to quit");
scanf("%c", &c1);
if (c1 == 'q' || c1 == 'Q') {
printf("Now Quiting");
exit(0);
}
else if (c1 == 'i' || c1 == 'I') {
printf(" \n Do a Lagrange Interlopation");
printf(" \n How many control points are there?");
scanf("%d", &controlPoint);
x = malloc(controlPoint * sizeof(float));
y = malloc(controlPoint * sizeof(float));
for (counter = 0; counter < controlPoint; counter++) {
printf("Please Enter the x coordinate for control point #%d: ", counter);
scanf("%f", &x[counter]);
printf("Please Enter the y coordinate for control point #%d: ", counter);
scanf("%f", &y[counter]);
}
}
else {
printf("Invalid Option Quitting");
}
printf("\nPlease select an option");
printf("\n Press s to do single interlopation");
printf("\n Press r to interlope in increments over the entire range");
printf("\n Press q to quit");
scanf("%c", &c2);
if (c2 == 's' || c2 == 'S') {
printf(" \n Interlope Single");
printf(" \n Please enter the value of x you wish to interlope for: ");
scanf("%f", &intSingle);
fx = malloc(controlPoint * sizeof(float));
for (counter = 0; counter < controlPoint; counter++)
{
temp = 1;
int k = counter;
for (counterTwo = 0; counterTwo < controlPoint; counterTwo++)
{
if (k == counterTwo)
{
}
else
{
temp = temp * ((intSingle - x[counterTwo]) / (x[k] - x[counterTwo]));
}
}
fx[counter] = y[counter] * temp;
}
for (counter = 0; counter < controlPoint; counter++)
{
sum = sum + fx[counter];
}
printf("\n Interpolated pair is (%f, %f) ", intSingle, sum);
}
else if (c2 == 'q' || c2 == 'Q'){
printf("Now Quiting");
exit(0);
}
int holder;
scanf("%d", &holder);
}
问题是当用户输入第二个选项时,如果我用代码 0 按下 s,它就会关闭,我还删除了 if 语句,代码工作正常,但我需要它们
【问题讨论】:
-
您是否考虑过在调试器中单步执行您的代码?不确定您指的是哪个 if 语句或您的输入是什么。也许你可以澄清这些事情。
-
当代码到达这里时 if (c2 == 's' || c2 == 'S') {
-
在调用任何
scanf()系列函数时,始终检查返回值(而不是参数值)以确保操作成功。 -
在调用任何堆分配函数时:(malloc, calloc, realloc),始终检查 (!=NULL) 返回值以确保操作成功。
标签: c visual-studio visual-studio-2015 exit exit-code