2016-09-16 70 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; 

    }