如何解决用scanf-十进制读取双精度字导致换行问题
我正在尝试填充3个“ slot”结构(我知道我分配了更多的内存),其中包括一个int和double [2]。但是,当我输入带小数点的数字时,会占用多行-这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "sshm.h"
struct Slot
{ int x;
double y[2];
} ;
int main(){
int shm_key = 123456,shmid,i;
struct Slot slot;
struct Slot* shared;
shmid = shm_get(shm_key,(void**)&shared,5 * sizeof(struct Slot));
if (shmid<0) {printf("error with get"); return 0;}
for(i = 0; i < 3; i++){
printf("Enter an integer: ");
scanf(" %i",&shared[i].x);
printf("Enter a double: ");
scanf(" %f",&shared[i].y[0]);
printf("Enter a second double: ");
scanf(" %f",&shared[i].y[1]);
}
return 0;
}
编辑: 消耗几行的示例-
Enter an integer: 5
Enter a double: 6.6
Enter a second double:Enter an integer:Enter a double:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。