为什么我的功能会产生额外的新线?

时间:2017-11-06 08:44:01

标签: c printf

我有function,只接受有效输入。 在这种情况下,有效输入将是integer值更大 零和小UCHAR_MAX。 如果输入无效,则应抛出[ERR] -message 在一个新的行。问题在于,虽然ist工作 应该按字符的方式,它不应该按照它应该的方式工作 当值超出定义范围(0 < input < UCHAR_MAX)时。 这意味着当输入类似于-42或1337时,它就会产生 [ERR] - 消息等待另一行“输入”。

为什么它会创建一个额外的新行,等待另一个“输入”,而它是按字符工作的,我该如何克服这个问题呢?

int get_int(void)
{
  char lsc;   // least significant char - stores the last char at scanf()
  char correct_input = 'f'; // boolean value to exit the while-loop
  int input = 0;            // stores the users input
  int return_value = 0;     // stores the return value of scanf();

  while (correct_input == 'f') {
    printf("Please enter an unsigend integer value greater zero: ");
    return_value = scanf("%d%c", &input, &lsc);

    if (return_value == 2 && lsc == '\n' && UCHAR_MAX > input && input > 0)
    {
      printf("[OK]  Valid input.\n");
      correct_input = 't';
    }
    else if (return_value == EOF) {
      exit(0);
    }
    else
    {
      printf("[ERR] Invalid input!\n");
      while(getchar() != '\n');    // flushes the scanf()-buffer
    }
  }
  return input;
}

产生的结果:

Please enter a unsigend integer value greater zero: foo
[ERR] Invalid input!
Please enter a unsigend integer value greater zero: 1337
[ERR] Invalid input!

Please enter a unsigend integer value greater zero: -42
[ERR] Invalid input!

Please enter a unsigend integer value greater zero: 5
[OK]  Valid input.

如上所示,当值超出范围时,会产生额外的新行。

我很感激任何帮助,链接,参考和提示!

2 个答案:

答案 0 :(得分:2)

  

那么为什么当值超出范围时会产生新的线?

它不再产生一条新线;由于while(getchar() != '\n'),它等待你输入一个。

  

我怎样才能克服这个问题?

while(getchar() != '\n')如果scanf()已读取换行符,则跳过 if (return_value < 2 || lsc != '\n') // do only if \n not already read while(getchar() != '\n'); // flushes the scanf()-buffer

function my_clickable_links( $text ) {
    $patt = array(
        '%\b(?<!href=[\'"])(?>https?://|www\.)([\p{L}\p{N}]+[\p{L}\p{N}\-]*\.(?:[\p{L}\p{N}\-]+\.)*[\p{L}\p{N}]{2,})(?::\d+)?(?:(?:(?:/[\p{L}\p{N}$_\.\+!\*\'\(\),\%;:@&=-]+)+|/)(?:\?[\p{L}\p{N}$_\.\+!\*\'\(\),\%;:@&=-]+)?(?:#[^\s\<\>]+)?)?(?![^<]*+</a>)%u',
        '%\b(?<!http://)(?<!https://)([\p{L}\p{N}]+[\p{L}\p{N}\-]*\.(?:[\p{L}\p{N}\-]+\.)*(?:ru|com|net))(?::\d+)?(?:(?:(?:/[\p{L}\p{N}$_\.\+!\*\'\(\),\%;:@&=-]+)+|/)(?:\?[\p{L}\p{N}$_\.\+!\*\'\(\),\%;:@&=-]+)?(?:#[^\s\<\>]+)?|\b)(?![^<]*+</a>)%u'
    );
    $repl = array(
        '<a href="$0">$1</a>',
        '<a href="http://$0">$1</a>'
    );
    $text = preg_replace($patt, $repl, $text);
    echo $text;
}
add_filter( 'the_content', 'my_clickable_links', 12 );

答案 1 :(得分:1)

<header> <div class="header"> <a href="index.html"><img src="img/logo.svg" width="200"></a> </div> </header> <ul id="navbar"> <li><a href="index.html">Home</a></li> <li><a href="producten.html">Producten</a></li> <li><a href="diensten.html">Diensten</a></li> <li><a href="over.html">Over Ons</a></li> <li><a href="contact.html">Contact</a></li> </ul> <div class="container"> <main> <h1>Lorem Ipsum</h1> <img id="ipsumpic1" src="img/ipsum1.svg"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a pulvinar dolor, a rutrum tortor. Vestibulum auctor finibus odio id efficitur. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Curabitur ullamcorper risus neque, sed vehicula augue finibus sed. Donec pretium ornare libero a venenatis. Donec sed ex id tellus posuere interdum. Nullam tincidunt egestas odio non gravida. Sed ut felis aliquet eros commodo elementum. Aliquam ullamcorper, diam quis aliquet tempor, magna nisi laoreet nisl, at vestibulum arcu enim quis elit.</p> <img id="ipsumpic2" src="img/ipsum2.svg"> <p>Ut faucibus dui lectus, vitae pharetra magna maximus quis. Donec sollicitudin sem non ornare vulputate. Suspendisse pretium lacus nec gravida tristique. Sed sed nibh et eros facilisis accumsan. Nam mi elit, fermentum eu laoreet vel, venenatis sit amet leo. Curabitur erat arcu, dictum eu massa quis, hendrerit mattis turpis. Etiam ultrices quam dolor, a interdum erat eleifend eu. Cras sed blandit nunc. In dictum dignissim enim, vel gravida nisl sagittis pellentesque. Quisque fermentum pellentesque velit a faucibus. Nulla facilisi. In fermentum, mi id feugiat ultricies, nulla lorem elementum felis, et pretium velit mauris sit amet odio. Curabitur facilisis, mi in eleifend commodo, enim ex dignissim quam, ut pulvinar magna nisl et orci. Duis pulvinar imperdiet pretium. Nullam vehicula tortor in ex volutpat egestas.</p> </main> <div id="sidebarleft"> <aside> <h2>Nieuw nieuws!</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed velit leo, finibus sed risus eget, semper molestie lacus. Integer dictum laoreet erat a facilisis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum luctus diam a est accumsan, eget maximus sem bibendum. Pellentesque efficitur tellus maximus elit egestas bibendum. Cras efficitur enim tortor, a fringilla lacus tristique quis. Pellentesque neque felis, faucibus nec nibh vel, dapibus congue lorem. Sed scelerisque ligula vitae convallis condimentum. Nulla a ante pulvinar, tristique turpis a, sagittis ipsum. Aliquam fringilla convallis justo, eu eleifend quam placerat ac. Fusce sit amet turpis id lacus consectetur sollicitudin.</p> </aside> </div> <div id="sidebarright"> <aside> <h2>Social media!</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed velit leo, finibus sed risus eget, semper molestie lacus. Integer dictum laoreet erat a facilisis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum luctus diam a est accumsan, eget maximus sem bibendum. Pellentesque efficitur tellus maximus elit egestas bibendum. Cras efficitur enim tortor, a fringilla lacus tristique quis. Pellentesque neque felis, faucibus nec nibh vel, dapibus congue lorem. Sed scelerisque ligula vitae convallis condimentum. Nulla a ante pulvinar, tristique turpis a, sagittis ipsum. Aliquam fringilla convallis justo, eu eleifend quam placerat ac. Fusce sit amet turpis id lacus consectetur sollicitudin.</p> </aside> </div> </div> <footer> <div class="container"> <h2>Footer stuff</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed suscipit faucibus libero quis lacinia. In in varius tortor. Donec nec finibus sapien. Curabitur tincidunt at quam vel commodo. In ut suscipit metus. In hac habitasse platea dictumst. Praesent dictum justo ut odio consectetur consectetur.</p> </div> </footer>让您的程序等待输入密钥