scanf()输入问题

[复制链接]
查看11 | 回复10 | 2021-1-27 06:52:08 | 显示全部楼层 |阅读模式
#include
intmain()
{
inta,b;
floatx,y;
charc1,c2;
scanf("a=%db=%d",&a,&b);
scanf("%f%e",&x,&y);
scanf("%c%c",&c1,&c2);
printf("a=%db=%dx=%fy=%fc1=%cc2=%c",a,b,x,y,c1,c2);
return0;
}

我输入c1=A,c2=a而输出却是c1=c2=A???????
分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:52:08 | 显示全部楼层
不知道楼主在说什么的飘过。
回复

使用道具 举报

千问 | 2021-1-27 06:52:08 | 显示全部楼层
应该是读到缓冲区中的回车符,就是之前输入数字的时候输入的回车符
你在第一个%c前面加一个空格可能就可以了
scanf("%c%c",&c1,&c2);
回复

使用道具 举报

千问 | 2021-1-27 06:52:08 | 显示全部楼层
把完整的输入写出来。
不过估计就是2楼说的原因。
回复

使用道具 举报

千问 | 2021-1-27 06:52:08 | 显示全部楼层
scanf();是格式化输入,你从控制台输入格式要一样,输入的空格逗号要和scanf中的一样
scanf("%c%c",&c1,&c2);这样的话输入2个字母中间就不能有别的,空格逗号都不行
回复

使用道具 举报

千问 | 2021-1-27 06:52:08 | 显示全部楼层
#include
intmain()
{
inta,b;
floatx,y;
charc1,c2;
scanf("%d%d",&a,&b);//a=什么的去掉
scanf("%f%e",&x,&y);
scanf("%s%s",&c1,&c2);//把%c改成%s
printf("a=%db=%dx=%fy=%fc1=%cc2=%c",a,b,x,y,c1,c2);
return0;
}

回复

使用道具 举报

千问 | 2021-1-27 06:52:08 | 显示全部楼层
使用scanf需要先清除缓冲区,还有两个输入数之间最好用逗号隔开,故将程序改成如下:
#include
intmain()
{
inta,b;
floatx,y;
charc1,c2;
fflush(stdin);
scanf("%d,%d",&a,&b);
fflush(stdin);
scanf("%f,%e",&x,&y);
fflush(stdin);
scanf("%c%c",&c1,&c2);
fflush(stdin);
printf("a=%db=%dx=%fy=%fc1=%cc2=%c",a,b,x,y,c1,c2);
scanf("%d",&a);//暂停用,这样会看到结果,不会一闪而过
return0;
}
回复

使用道具 举报

千问 | 2021-1-27 06:52:08 | 显示全部楼层
FormatSpecificationFields:scanfandwscanfFunctions
Aformatspecificationhasthefollowingform:
%
  • [width][{h|l|I64|L}]type
    Theformatargumentspecifiestheinterpretationoftheinputandcancontainoneormoreofthefollowing:
    White-spacecharacters:blank('');tab('\t');ornewline('\n').Awhite-spacecharactercausesscanftoread,butnotstore,allconsecutivewhite-spacecharactersintheinputuptothenextnon–white-spacecharacter.Onewhite-spacecharacterintheformatmatchesanynumber(including0)andcombinationofwhite-spacecharactersintheinput.

    Non–white-spacecharacters,exceptforthepercentsign(%).Anon–white-spacecharactercausesscanftoread,butnotstore,amatchingnon–white-spacecharacter.Ifthenextcharacterinstdindoesnotmatch,scanfterminates.

    Formatspecifications,introducedbythepercentsign(%).Aformatspecificationcausesscanftoreadandconvertcharactersintheinputintovaluesofaspecifiedtype.Thevalueisassignedtoanargumentintheargumentlist.
    Theformatisreadfromlefttoright.Charactersoutsideformatspecificationsareexpectedtomatchthesequenceofcharactersinstdin;thematchingcharactersinstdinarescannedbutnotstored.Ifacharacterinstdinconflictswiththeformatspecification,scanfterminates,andthecharacterisleftinstdinasifithadnotbeenread.
    Whenthefirstformatspecificationisencountered,thevalueofthefirstinputfieldisconvertedaccordingtothisspecificationandstoredinthelocationthatisspecifiedbythefirstargument.Thesecondformatspecificationcausesthesecondinputfieldtobeconvertedandstoredinthesecondargument,andsoonthroughtheendoftheformatstring.
    Aninputfieldisdefinedasallcharactersuptothefirstwhite-spacecharacter(space,tab,ornewline),oruptothefirstcharacterthatcannotbeconvertedaccordingtotheformatspecification,oruntilthefieldwidth(ifspecified)isreached.Iftherearetoomanyargumentsforthegivenspecifications,theextraargumentsareevaluatedbutignored.Theresultsareunpredictableiftherearenotenoughargumentsfortheformatspecification.
    Eachfieldoftheformatspecificationisasinglecharacteroranumbersignifyingaparticularformatoption.Thetypecharacter,whichappearsafterthelastoptionalformatfield,determineswhethertheinputfieldisinterpretedasacharacter,astring,oranumber.
    Thesimplestformatspecificationcontainsonlythepercentsignandatypecharacter(forexample,%s).Ifapercentsign(%)isfollowedbyacharacterthathasnomeaningasaformat-controlcharacter,thatcharacterandthefollowingcharacters(uptothenextpercentsign)aretreatedasanordinarysequenceofcharacters,thatis,asequenceofcharactersthatmustmatchtheinput.Forexample,tospecifythatapercent-signcharacteristobeinput,use%%.
    Anasterisk(*)followingthepercentsignsuppressesassignmentofthenextinputfield,whichisinterpretedasafieldofthespecifiedtype.Thefieldisscannedbutnotstored.

  • 回复

    使用道具 举报

    千问 | 2021-1-27 06:52:08 | 显示全部楼层
    你要弄清楚标准IO
    (1)行缓冲
    (2)无缓冲
    (3)全缓冲
    这三个的概念,有些问题本身其实没有价值,问题本身引发的东西也许更有价值。
    回复

    使用道具 举报

    千问 | 2021-1-27 06:52:08 | 显示全部楼层
    你向两个%c中间加个空格试试
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    主题

    0

    回帖

    4882万

    积分

    论坛元老

    Rank: 8Rank: 8

    积分
    48824836
    热门排行