第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

scanf跳過

scanf跳過

C
慕容森 2019-11-04 10:07:01
我試圖為一個(gè)類編寫一個(gè)簡單的C程序,其中一項(xiàng)要求是我必須對(duì)所有輸入和輸出使用scanf/ printf。我的問題是為什么我scanf的主程序中的for循環(huán)后被跳過而程序剛剛終止。這是我的代碼#include <stdio.h>void main() {    int userValue;    int x;    char c;    printf("Enter a number : ");    scanf("%d", &userValue);    printf("The odd prime values are:\n");    for (x = 3; x <= userValue; x = x + 2) {        int a;        a = isPrime(x);        if (a = 1) {             printf("%d is an odd prime\n", x);        }    }       printf("hit anything to terminate...");    scanf("%c", &c);    }int isPrime(int number) {    int i;    for (i = 2; i < number; i++) {        if (number % i == 0 && i != number)            return 0;    }    return 1;}我可以通過scanf在第一個(gè)之后添加另一個(gè)相同項(xiàng)來“修復(fù)”它,但是我寧愿只使用一個(gè)。
查看完整描述

1 回答

?
瀟瀟雨雨

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊

輸入stdin上一個(gè)字符后出現(xiàn)的換行字符int不會(huì)被最后一次調(diào)用所占用scanf()。因此,scanf()在for循環(huán)之后對(duì)to的調(diào)用將占用換行符,并且無需用戶輸入任何內(nèi)容即可繼續(xù)進(jìn)行。


要進(jìn)行糾正而不必添加其他scanf()調(diào)用,可以在循環(huán)之后的format說明符" %c"中使用。這將跳過所有前導(dǎo)空格字符(包括換行符)。請(qǐng)注意,這意味著用戶將不得不輸入換行符以外的其他內(nèi)容來結(jié)束程序。scanf()forscanf()


另外:


檢查的結(jié)果scanf()以確保它實(shí)際上為傳入的變量分配了一個(gè)值:


/* scanf() returns number of assigments made. */

if (scanf("%d", &userValue) == 1)

這是一個(gè)賦值(并且永遠(yuǎn)是正確的):


if (a = 1){ /* Use == for equality check.

               Note 'a' could be removed entirely and

               replace with: if (isPrime(x)) */


查看完整回答
反對(duì) 回復(fù) 2019-11-04
  • 1 回答
  • 0 關(guān)注
  • 477 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)