㈠ c語言 如下源碼,運行的時候 有些語句直接被跳過,原因何在求詳解,
那是因為你讀入單個字元的方式不對,
純粹的scanf("%c"),或者getchar()都會把你上一次輸入的回車符( )讀入了。
然後你就讀入了一個回車符。
正確的方式是讀入到一個字元串,再取字元串首位。
代碼:
//老師學生信息放在一起並列印出來
#include<stdio.h>
unioncondition
{
intscore[4];//學生4科成績
charsituation[40];//教師工作情況
};
structpersonal
{
intnum;//編號
charname[10];//姓名
charsex;//性別
charkind;//篩選t或s
unionconditionchange;
};
structpersonalinformation[2];//創建兩個個人信息表
voidmain()
{
inti,j;
for(i=0;i<2;i++)
{
printf("Pleaseinputnum:");//編號
scanf("%d",&information[i].num);
charsex[16];
printf("EntertheMortheW:");//性別
scanf("%s",sex);
information[i].sex=sex[0];
printf("Enterthename:");//名字
scanf("%s",information[i].name);
charts[16];
printf("pleasechangetors:");//t或s
scanf("%s",ts);
information[i].kind=ts[0];
if('t'==information[i].kind)//t為教師工作情況
{
printf("Pleaseenterthesituation:");
scanf("%s",information[i].change.situation);
}
else//s為學生成績
{
for(j=0;j<4;j++)
{
printf("pleaseenterno.%dscore:",j);
scanf("%d",&information[i].change.score[j]);
}
}
}
for(i=0;i<2;i++)//列印以下
{
printf("%d ",information[i].num);//編號
printf("%s ",information[i].name);//姓名
printf("%c ",information[i].sex);//性別
if('t'==information[i].kind)
{
printf("%s",information[i].change.situation);//工作情況
}
else
{
for(j=0;j<4;j++)
{
printf("%d",information[i].change.score[j]);//成績
}
}
}
}
運行:
㈡ C語言語法沒錯誤但是運行起來會跳過某一行
你沒給代碼,猜一下問題原因:
因為你輸入編碼後回車,比如你輸入1後回車,回車符留在了緩沖區,當你的代碼接收姓名時,直接得到了回車符,所以改善方法是,在你的接收姓名的代碼前加一行:getchar();
代碼類似於:
......
printf("請輸入編碼");
scanf("%d",&code);
getchar(); /*這行是要新加的*/
printf("請輸入姓名:");
scanf("%s",name); /*name是字元數組*/
......
㈢ C語言求助 總是跳過scanf語句,為什麼
這個是本身編譯軟體的問題,也不算是屬於BUG。那是由於上一個輸入輸入的函數最後判斷輸入完畢的回車會在這里生效,而且你寫getchar函數也會一同運行,解決辦法就是寫兩個getchar。
㈣ 我的C語言程序直接跳過了一個輸入的步驟是怎麼回事。剛開始學c
因為scanf在輸入數只時,碰到非數值時就停下來了,回車、空格等等都是非數值。
代碼中要求輸入兩個數值,此時在第二個數值的後面,一定會有其他非數值字元,如回車符,而接下來輸入字元時就將那個回車符作為了輸入的字元了
㈤ 為什麼我的C語言程序會跳過一條輸入語句呢
老兄,是這樣的,你的語句中有輸入字元型數據或著字元串的,如果連續輸入兩個比如:
scanf("%c",&a);
scanf("%c",&b);
運行的時候你如果輸入了 r回車,s回車,也就是輸入字元S後按了回車,然後輸入字元S 輸入回車,此時變數 b並不能夠接受你輸入的S,而是接受了回車符號,所以造成好象有個輸入語句沒有執行一樣.
所以你把你的程序中,連續的接受字元或字元串的語句後面都加上個 getchar(),用來接受你的那個回車就沒有問題了,源程序我不在幫你該了,你自己加上吧.
㈥ 為什麼總是跳過這一步(C語言編程)
在
while(getchar()!='y')
語句前加入一個getchar();
因為在之前,你會輸入一個回車鍵,getchar()得到的是回車鍵,因此跳過這步
㈦ c語言意外跳過判斷語句
完整代碼如下,編譯通過,附上結果,若有疑問,請追問。若滿意,望採納.
#include<stdio.h>
#include<stdlib.h>
intmain()
{
voidswap(float*,float*);
floatfirstnum,secnum;
charsortOrder;
printf("Entertwonumbers:");
scanf("%f%f",&firstnum,&secnum);
printf(" Beforethecalltoswap(): ");
printf("Thevalueinfirstnumis%5.2f ",firstnum);
printf("Thevalueinsecnumis%5.2f ",secnum);
if(firstnum>secnum)
swap(&firstnum,&secnum);
getchar();
while(1)
{
printf(" Pleaseinputthesortorder(e(exit),a(ascending),d(descending)):");
scanf("%c",&sortOrder);
getchar();
if(sortOrder=='d')
{
printf(" Afterthecalltoswap(): ");
printf("Thevalueinfirstnumis%5.2f ",firstnum);
printf("Thevalueinsecnumis%5.2f ",secnum);
}
elseif(sortOrder=='a')
{
printf(" Afterthecalltoswap(): ");
printf("Thevalueinsecnumis%5.2f ",secnum);
printf("Thevalueinfirstnumis%5.2f ",firstnum);
}
elseif(sortOrder=='e')
return0;
else
printf("Error!");
}
system("pause");
return0;
}
voidswap(float*num1Addr,float*num2Addr)
{
floattemp;
temp=*num1Addr;
*num1Addr=*num2Addr;
*num2Addr=temp;
}
㈧ c語言的問題。。。跳過了語句
這是因為你前面有scanf("%d",&entry);
這句輸入,你輸入完後會敲一下回車鍵,表示你輸入的結束,這個回車鍵會留在輸入緩沖區內,當你使用a=getchar();是,他發現緩沖區內有字元,也就是那個回車鍵,他會在動將回車鍵賦給a,所以才出現跳過的現象。
你可以做如下修改:
scanf("%d",&entry);
後面接一句:getchar();用來吃掉回車鍵,這樣就可以了。
㈨ C語言為什麼跳過了while語句
因為我們在輸入第一組的2個數時會輸入「回車」,這個回車會被你第二個「scanf」函數捕獲,你只要在第二個「scanf」語句前加一句「getchar();」就行了,代碼如下:
#include
"stdio.h"
int
main(){
int
a,b;
char
operator;
printf("please
input
two
number:");
scanf("%d
%d",&a,&b);
printf("please
input
operater(+
or
-):");
getchar();
scanf("%c",&operator);
switch(operator)
{
case
'+':
printf("%d+%d=%d\n",a,b,a+b);break;
case
'-':
printf("%d-%d=%d\n",a,b,a-b);break;
default
:
printf("input
error\n");
}
return
0;
}
㈩ 請問為什麼這一行命令被跳過了簡單的C語言
我推測是因為 %c 把你輸入的回車符給抓到了
你可以把ch的值的數值形式列印出來,八成是32或者10