相关疑难解决方法(0)

跳过scanf getchar函数

一个非常简单的问题.为什么在第一个while循环中跳过scanf.我已经尝试使用getchar(),结果是一样的.getchar被跳过了.

如果你们不明白我在说什么,你可以尝试编译它,你们会理解我在问什么.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct rec{
    int num;
    char pref[16];
    float point;
    struct rec* next;
}rec;

void dataInput(float*,char*);
rec* insertNode(rec*);

int main(){

    int i=1;
    rec* entr,*pt = NULL;
    entr = (rec*) malloc(sizeof(rec));
    entr->num = i;
    dataInput(&entr->point,entr->pref);
    entr->next = NULL;
    char key;
    i++;

    while(1){

        printf("Continue ? If YES press 'y',or NO press 'n'\n");
        key = getchar();
        if(key == 'n')break;
        else if(key == 'y'){
            if(!pt){
                pt = insertNode(entr);
            }else{
                pt = insertNode(pt);
            }
            dataInput(&pt->point,pt->pref);
            pt->num = i; …
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
2
解决办法
3577
查看次数

C - scanf被跳过(即使是"%d")

我试图找出为什么我不能让它运行正常.我只想要用户输入四个,并在最后运行计算.

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

int main(){
    double amount; /* amount on deposit */
    double principal; /* what's the principal */
    double rate; /* annual interest rate */
    int year; /* year placeholder and no. of total years */
    int yearNo;

    printf("What is the principal? ");
    scanf("%d", &principal);
    printf("What is the rate (in decimal)? ");
    scanf(" .2%d", &rate);
    printf("What is the principal? ");
    scanf(" %d", &principal);
    printf("How many years? ");
    scanf(" %d\n", yearNo);


    printf("%4s%21s\n", "Year", "Amount on deposit");

    /* calculate …
Run Code Online (Sandbox Code Playgroud)

c scanf

1
推荐指数
1
解决办法
129
查看次数

标签 统计

c ×2

scanf ×1