2015-10-19 160 views
-2

我不确定scan_f是否导致我的程序在调试时跳过行。 第一个printf(“”);运行并接受和输入,但第二个输出但不接受和输入并跳转到第三个printf(“”); 。我最初使用的是一个getchar,但因为我不熟悉C,所以我回到scanf_s直到我更熟悉它。scanf在visual studio中跳过行2015

//libary 
#include <stdlib.h> 
#include <stdio.h> 
#include<ctype.h> 

//global variables 
float wPrice = 1.80, bPrice = 1.50, sPrice = 1.75, mPrice = 2.00, deliveryPrice = 2.00; 

//define globalconstants 

//define Poundsign 

#define POUNDSIGN 156 

//THe breadtype chars will be uppercase 
char w, b, s, m, delivery; 

//void main 

void main() { 

    //local variables 
    float totalCost = 0.00; 

    //prompt user for what breads they want? 

     printf("Hello do you want Wholemeal bread?(Y/N): "); 
     //w = getchar(); 
     scanf_s("%c", &w, 1); 
     fflush(stdin); 
     w = toupper(w); 



    printf("\nDo you want Brown bread?(Y/N): "); 
    scanf_s("%c", &b, 1); 
    fflush(stdin); 
    //b = getchar(); 
    b = toupper(b); 


    printf("\nDo you want Seeded bread?(Y/N): "); 
    scanf_s("%c", &s, 1); 
    fflush(stdin); 
    //s = getchar(); 
    s = toupper(s); 


    printf("\nDo you want Multigrain bread?(Y/N): "); 
    scanf_s("%c", &m, 1); 
    fflush(stdin); 
    //m = getchar(); 
    m = toupper(m); 


    printf("\n Will you need the bread delivered?(Y/N)\nYOU MUST LIVE WITHIN 8 MILES OF THE BAKERY TO BE ELIGIBLE FOR DELIVERY:"); 
//delivery = getchar(); 
    scanf_s("%c", &delivery, 1); 
    fflush(stdin); 
    delivery = toupper(delivery); 

    //work out price with if statment 

    //wholegrain bread 
    if (w = 'Y') { 

     totalCost = totalCost + wPrice; 
    } 

    //brown bread 
    if (b = 'Y') { 
     totalCost = totalCost + bPrice; 
    } 
    //seeded 
    if (s = 'Y') { 

     totalCost = totalCost + sPrice; 

    } 
    //multigrain 
    if (m = 'Y') { 
     totalCost = totalCost + mPrice; 
    } 

    if (delivery = 'Y') { 
     totalCost = totalCost + deliveryPrice; 
    } 

    //if no bread has been purchased 

    else { 

     printf("You have purchased no bread"); 
     system("pause"); 
     exit(0); 
    } 

    //print the total to them 

    printf("Wholemeal bread cost %c%1.2f\nBrown bread costs %c%1.2f\nSeeded bread costs %c%1.2f\nMultigrain bread costs %c%1.2f", POUNDSIGN, wPrice, POUNDSIGN, bPrice, POUNDSIGN, sPrice, POUNDSIGN, mPrice); 
    //total price 
    printf("\nYour total price including delivery is %c%1.2f", POUNDSIGN, totalCost); 

    //hang system 
    system("pause"); 
    exit(0); 


} 
+1

'fflush(stdin)'是未定义的行为。 –

+2

'w ='Y'':使用'=='而不是'='。 – BLUEPIXY

+0

这是一个在缓冲区中留下的换行符。比Jon Skeet代表的更多。 OP失败搜索.... –

回答

1

您需要忽略尾随'\n'被插入到缓冲区,当你按下回车。要这样做,请尝试像这样

scanf(" %c", &character); 
/* ^this will skip all white spaces */ 

所有其他spcifiers正常跳过它们,除了"%c"

另外:

  1. 不要这样做fflush(stdin)
  2. 您的比较分配为@BLUEPIXY指出。

另外,您应该关心代码格式和样式。你可能会说它不影响代码的运行方式,这是真的。但它影响整体质量和可维护性。如果你的代码比较漂亮,那么你会有更多的错误。

+0

感谢您的帮助,我很乐意为此做出这样的事情。 – Lombax

+1

如果你是新手,你不应该感到抱歉。相反,学习,不要再犯同样的错误。 –

2
w = 'Y' 

那不是一个比较,而是一个任务。做

w == 'Y' 

改为。

fflush(stdin); 

那是不允许的(未定义行为)。

要scanf的输入之前跳过空格,做

scanf(" %c", &char); 
+0

谢谢!我是编程新手,所以我正在为班级作业工作。 – Lombax

+0

@Lombax如果这解决了你的问题,考虑接受iharob的回答,因为他是第一个。 – Magisch

0

只需添加程序一行代码以上

的#define _CRT_SECURE_NO_WARNINGS

实施例:

#define _CRT_SECURE_NO_WARNINGS 
#include<stdio.h> 
int main() 
{ 

int n; 
scanf("%d",&n); 
printf("value:%d \n",n); 


return 0; 
}