【发布时间】:2015-04-26 17:11:34
【问题描述】:
有人可以帮我解决这个问题吗?我正在尝试用数字填充向量。问题(我认为)是那里的条件。当我输入“f”时,它应该退出 while 循环,但它不起作用,尽管在询问之前它没有得到最高数量的元素。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int vecto();
int var;
double* vector;
char* bv;
int vecto(){
int cont=0,ch;
char v[10];
free(vector);
printf ("¿Number of elements to order?: ");
scanf("%d",&var);
vector = (double*)malloc(var*sizeof(double));
printf("Input numbers press (f) for finish \n");
while((ch = fgetc(stdin)) != EOF && ch != '\n' ){};
do{
fgets(v,sizeof(v),stdin);
if((strcmp(v,"f"))!=0){ /*maybe here´s the problem*/
vector[cont++]=strtod(v,&bv);
}
} while(!((cont==var) || (strcmp(v,"f")==0)));/*maybe here's the problem*/
printf("\n");
return 0;
}
【问题讨论】:
-
请不要在您的标题中添加“已修复”或“已解决”。如果您自己的答案与给出的答案不同,您可以随意发布。
-
对不起c:我不知道
标签: c while-loop scanf fgets strcmp