[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] getline under en switch



Dennis Hansen wrote:

> Hej,
> Jer er ved at lave et lille C++ program.
> Jeg bruger gcc 2.96 under Mandrake 8.0
>
> getline opfører sig mystisk hvis jeg bruger den under en switch / case,
> så er jeg nød til at kalde den to gange ellers virker det som om den bare
> ignorerer det første kald.
>
> Jeg har kun oplevet dette hvis den bliver kaldt inde i en swich, udenfor
> virker det som jeg ville forvente.
>
> Her er en stump kode som viser problemet.
>
> //------------------------------------
> #include <string>
> #include <iostream>
> int main()
> {
>    char valg;
>    std::string noget;
>
>    std::cout << "Indtast valg : ";
>    std::cin >> valg;
>    switch (valg)
>    {
>       case '1':
>          std::cout << "Indtast noget : ";
>          std::getline(std::cin,noget);
>
>          // nød til at insætte en ekstra getline her
>          std::getline(std::cin,noget);
>          std::cout << "Du tastede " << noget << std::endl;
>          break;
>
>       default:
>          break;
>    }
>    return 0;
> }
> //-------------------------------------
>
> Hvorfor skal den have den to gange for at virke ?

Det kan jeg ikke lige fortælle dig, men det er sikkert det der med
un-flushede buffere. Dog vil jeg kraftigt opfordre dig til at bruge readline
hvis du skal lave bruger interaktion, da stdin/cin er pænt ringe at bruge
når man skal taste ting ind. Dit program ville se sådan her ud  med readline

//-------------------------------------
#include <string>
#include <iostream>
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>

int main()
{
  char *valg;
  char *noget;

  valg = readline("Indtast valg: ");
  switch ((char)*valg)
    {
    case '1':
      noget = readline("Indtast noget: ");

      std::cout << "Du tastede " << noget << std::endl;
      break;

    default:
      break;
    }
  return 0;
}
//-------------------------------------

Du skal huske at kompilere med -lreadline og -ltermcap

VH
Christian Hemmingsen



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *