2016-09-16 65 views
-2

所以我已經開始學習C++,並且我已經編寫了這個簡單的程序,當用戶輸入錯誤的數字時,它給出了一個選項,然而當用戶輸入任何字符時,它給出了選項再試一次,直接退出程序爲什麼會發生這種情況? `錯誤的輸入將退出程序C++

#include<unistd.h> 
#include<stdio.h> 


int main(){ 


int a; 
char b ,c; 

start: 


    printf("INPUT ONLY NUMBER 1 : "); 

    scanf(" %d", &a); 

    if(a==1) 
    { 
     printf(" you entered correctly \n"); 
     printf("do you want to try again? <Y> <N> \n"); 
     scanf(" %c", &c); 

     if(c=='Y' ||c=='y') 

     { 
      goto start; 
     } 

    } 
else { 
    sleep (1); 
    printf("wrong number , do you want to try again? <Y> <N> \n"); 
    scanf(" %c" , &b); 



} 

if (b=='Y'||b=='y') 
{ 
sleep(1); 
goto start; 
} 

else 
if(b=='n'||b=='N') 
{ 

sleep(1); 
printf("thank you and goodbye"); 
exit (1); 
} 
} 

`

+0

如果用戶輸入了錯誤的號碼,它工作正常,但只有當用戶輸入一個字符 –

+5

退出「,所以我已經開始學習C++「 - 這看起來像C,而不是C++。當然,你選擇了正確的書嗎?無論如何,獲得一本更好的書! 'goto'有其應用,但初學者不應該從**開始! 1970年代/ 80年代早已逝去,使用結構化代碼!並正確地格式化和縮進代碼。 – Olaf

+1

使用while循環代替goto語句 –

回答

0

scanf(" %d")
我敢打賭,你的問題來自於%d之前的空間。嘗試scanf("%d")。稍晚:scanf("%c")而不是scanf(" %c ")

無論如何,你的代碼非常髒。這看起來像C,而不是C++。
不要在這裏成爲一個瘋子,但你應該正確縮進並避免goto BY THE MEANS。您的程序結構非常簡單,並且循環將爲您解決問題。你也應該避免明確地呼叫exit(1)exit主要用於挑起提前終止。在你的情況下,你的程序正常結束,你應該退出main函數的return 0

#include <unistd.h> 
#include <stdio.h> 
#include <stdbool.h>  //Reauired to use boolean type in C 

int main() 
{ 
    bool stop = false;  //boolean type only has two states: true and false. Very useful for loops! 
    while(!stop)   //Read as "While we don't need to stop, execute the loop's contents 
    {      //Much easier to read! 
     printf("INPUT ONLY NUMBER 1 : "); 
     scanf("%d", &a); 
     if(a == 1) 
     { 
      printf("you entered correctly \n"); 
      printf("do you want to try again? <Y> <N>\n"); 
      scanf("%c", &c); 

      if(c == 'N' || c == 'n') 
      {     //We only need to tell the loop when to stop 
       stop = true; //by setting stop to true 
      }     //The loop's default behavior is to loop execution of its content 
     } 
     else 
     { 
      sleep(1); 

      printf("wrong number , do you want to try again? <Y> <N> \n"); 
      scanf("%c" , &b); 

      if(b=='n'|| b=='N') 
      { 
       stop = true; //Same as above 
      } 

      sleep(1); 
     } 
    } 

    printf("thank you and goodbye"); 
    return 0; 
} 
-1

不讀取輸入的類型int的變量,但入式char*的變量。然後檢查該值是否僅包含小數,如有必要,將其轉換爲int

(詩,我不知道,雖然通過心臟的方法,但它不應該是很難谷歌他們)

0

也有一些是從scanf的,有時你可以清理一下刷新它留下來的,但在你的代碼似乎沒有工作。

在看看這個問題該遇到: http://c-faq.com/stdio/stdinflush2.html

這基本上會告訴你,如果你的方法是行不通的,那麼你應該改變它。 希望這有助於你還問到,而在你的問題有一個,所以:

#include <iostream> 

int main(){ 

    char input; 
    char try_again; 

    do { 
     std::cout << "INPUT ONLY NUMBER:"; 
     std::cin >> input; 
     // http://en.cppreference.com/w/cpp/string/byte/isdigit 
     // does not return a bool, you can check that >0 
     if (std::isdigit(input)) { 
      // do what you want. 
      std::cout << "digit\n"; 
      continue; 
     } else { 
      // you can as for more imput like: 
      std::cout << "Not a number, try again?(y/Y):"; 
      std::cin >> try_again; 
      std::tolower(try_again); 
      if (try_again == 'y') { 
       continue; // will start the loop again. 
      } else { 
       break; // it will exit the loop. 
     } 
     } 
    } while(true); 

    return 0; 

    }