2015-10-19 73 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; 
}