Re: [問題] scanf 的引數
想藉此問一個問題
先貼上code:(under linux gcc)
=======================================
#include <stdio.h>
int main( int argc, char *argv[] )
{
char a, b;
scanf( "%c\n", &a );
scanf( "%c\n", &b );
printf( "a = %c, b = %c\n", a, b );
return 0;
}
=======================================
我希望輸入兩個字元分別存在a和b
然後因為知道輸入a的字元後會接enter
所以先補一個\n,然後輸入b的字元後也會接enter,
這裡希望按兩次enter結束,也補一個\n
問題是:
如果以上述code去跑,
理論上當我輸入b字元後按兩次enter
程式應該會結束不是嗎
但是不管按幾次enter都會卡在第二個scanf
這是怎麼回事呢???
※ 引述《Neisseria (Neisseria)》之銘言:
: 開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
: cygwin + gcc
: 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
: 無
: 問題(Question):
: 在練習寫 queue ,但問題不是在 queue 上
: 為什麼在 scanf 輸入 char 時需要輸入 \n,例:scanf( "\n%c", &data );
: 而我用類似的方法試寫一個 stack
: 如果是在 int 時不需要加 \n,例:scanf( "%d", &data );
: 餵入的資料(Input):
: 單一的 char 例如 u, n, c, ...
: 預期的正確結果(Expected Output):
: u --> n --> c --> NULL
: 照書上範例的列印方式
: 錯誤結果(Wrong Output):
: -->
: -->
: -->
: --> NULL
: 程式碼(Code):(請善用置底文網頁, 記得排版)
: 很長 請包涵 = =
: #include<stdio.h>
: #include<stdlib.h>
: struct queueNode {
: char data;
: struct queueNode *nextPtr;
: };
: typedef struct queueNode QueueNode;
: typedef QueueNode *QueueNodePtr;
: void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr, char value );
: char dequeue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr );
: int isEmpty( QueueNodePtr headPtr );
: void printQueue( QueueNodePtr currentPtr );
: void instructions( void );
: int main( void ){
: QueueNodePtr headPtr = NULL;
: QueueNodePtr tailPtr = NULL;
: int choice;
: char value;
: instructions();
: printf( "? " );
: scanf( "%d", &choice );
: while ( choice != 3 ) {
: switch ( choice ) {
: case 1:
: printf( "Please input a character: " );
: scanf( "\n%c", &value );
: enqueue( &headPtr, &tailPtr, value );
: printQueue( headPtr );
: break;
: case 2:
: if ( !isEmpty( headPtr ) ) {
: value = dequeue( &headPtr, &tailPtr );
: printf( "%c has been dequeued.\n", value );
: }
: printQueue( headPtr );
: break;
: default:
: printf( "Invalid choice.\n" );
: instructions();
: break;
: }
: printf( "? " );
: scanf( "%d", &choice );
: }
: printf( "End of run.\n" );
: return 0;
: }
: void instructions( void )
: {
: printf( "Enter your choice:\n"
: " 1 to add an item to the queue.\n"
: " 2 to remove an item from the queue.\n"
: " 3 to end the program.\n" );
: }
: void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr, char value )
: {
: QueueNodePtr newPtr;
: newPtr = malloc( sizeof( QueueNode ) );
: if ( newPtr != NULL ) {
: newPtr->data = value;
: newPtr->nextPtr = NULL;
: if ( isEmpty( *headPtr ) ) {
: *headPtr = newPtr;
: }
: else {
: ( *tailPtr )->nextPtr = newPtr;
: }
: *tailPtr = newPtr;
: }
: else {
: printf( "%c not inserted. No memory available.\n", value );
: }
: }
: char dequeue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr )
: {
: QueueNodePtr tempPtr;
: char value;
: tempPtr = *headPtr;
: value = tempPtr->data;
: *headPtr = (*headPtr)->nextPtr;
: if ( *headPtr == NULL ) {
: *tailPtr == NULL;
: }
: free( tempPtr );
: return value;
: }
: int isEmpty( QueueNodePtr headPtr ){
: return headPtr == NULL;
: }
: void printQueue( QueueNodePtr currentPtr )
: {
: if ( currentPtr == NULL ) {
: printf( "The queue is empty.\n" );
: }
: else {
: printf( "The queue is: \n" );
: while (currentPtr != NULL ) {
: printf( "%c --> ", currentPtr->data );
: currentPtr = currentPtr->nextPtr;
: }
: printf( "NULL\n" ); }}
: 補充說明(Supplement):
: 無
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.32.79.38
推
09/12 23:07, , 1F
09/12 23:07, 1F
→
09/12 23:08, , 2F
09/12 23:08, 2F
推
09/14 12:32, , 3F
09/14 12:32, 3F
推
09/14 14:26, , 4F
09/14 14:26, 4F
→
09/14 14:26, , 5F
09/14 14:26, 5F
→
09/14 14:28, , 6F
09/14 14:28, 6F
→
09/14 14:28, , 7F
09/14 14:28, 7F
→
09/14 14:29, , 8F
09/14 14:29, 8F
→
09/14 14:29, , 9F
09/14 14:29, 9F
討論串 (同標題文章)