C++ using keyboard system inputs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RADAR
    New Member
    • Oct 2006
    • 21

    C++ using keyboard system inputs

    i use VC++ 6,0 and i have problems with getting keyboard system inputs and to use them to check to proceed or break the loop.
    for example if i want to proceed until Ctrl+z is written for input.What will the compiler do?Does it accept Ctrl+Z as a normal string input , char* input, or a char input which has ASCII 26...
    suppose i have a loop that does the following:
    -first check the choice
    -if not Ctlr+Z continue
    -else go to the next stage.


    "%
    i only need when Ctrl+Z is performed using keyboard and it is gotten by select,it should exit the loop..
    %"

    i tried to do sth:
    Code:
    char select;
    string str;
    
    while(select != char(26)) //--char(26) is the ASCII equivalent of Ctrl+Z , i guess
    {
                 cout     <<" Enter a string";
                cin >>str;
                locate_asterisk(str);//--a function that adds '*' after each char in a cpp          
                                            //--string
                cout       <<"If you want to continue, press any character"
                             <<"or to exit perform Ctrl+Z from keyboard"<<endl;
    }
    by pressing Ctrl+Z some strange characters appear; like ^Z
    i was trying this for more than 10 hours but i was really confused on what to do.
    I will be very pleased if any helpful ideas are provided by you;
    thanks for now
    RADAR
    Last edited by RADAR; Dec 27 '07, 12:13 AM. Reason: adding some detailed info
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by RADAR
    i use VC++ 6,0 and i have problems with getting keyboard system inputs and to use them to check to proceed or break the loop.
    for example if i want to proceed until Ctrl+z is written for input.What will the compiler do?Does it accept Ctrl+Z as a normal string input , char* input, or a char input which has ASCII 26...
    suppose i have a loop that does the following:
    -first check the choice
    -if not Ctlr+Z continue
    -else go to the next stage.


    "%
    i only need when Ctrl+Z is performed using keyboard and it is gotten by select,it should exit the loop..
    %"

    i tried to do sth:
    Code:
    char select;
    string str;
    
    while(select != char(26)) //--char(26) is the ASCII equivalent of Ctrl+Z , i guess
    {
                 cout     <<" Enter a string";
                cin >>str;
                locate_asterisk(str);//--a function that adds '*' after each char in a cpp          
                                            //--string
                cout       <<"If you want to continue, press any character"
                             <<"or to exit perform Ctrl+Z from keyboard"<<endl;
    }
    by pressing Ctrl+Z some strange characters appear; like ^Z
    i was trying this for more than 10 hours but i was really confused on what to do.
    I will be very pleased if any helpful ideas are provided by you;
    thanks for now
    RADAR
    Usually Ctrl+Z generates the SIGHUP signal in Unix and i am sure that you can handle it only with catching the signal.
    But i am not sure whether yopu will be able to read it using cin/scanf


    Raghuram

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The crl+Z is the end-of-all-input signal. You must press <enter> to put that in the input buffer. Without it, an inout loop will run forever askign for more input.

      Everything you type is echoed to the screen.

      Comment

      • RADAR
        New Member
        • Oct 2006
        • 21

        #4
        thanks for trying but i still couldn't do it.
        i only want if ctrl+z is entered , the while loop will end the program will continue until the return 0; statement as usual.
        thanks again guys.
        respects,
        radar

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Your code sampoe does not work since select is never initialized and you never use it to get data from the keyboard. Try this:
          [code=cpp]
          string str;
          cout <<" Enter a string";

          while(cin>>str)
          {
          locate_asterisk (str);//--a function that adds '*' after each char in a cpp
          //--string
          cout <<"If you want to continue, enter a string"
          <<"or to exit perform Ctrl+Z from keyboard"<<endl ;
          }
          [/code]

          Comment

          • RADAR
            New Member
            • Oct 2006
            • 21

            #6
            thank you very much for your help , this is so clean and strong answer for my question.i wonder if you can only answer one more question , why do i have to initialize the string variable?

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by RADAR
              why do i have to initialize the string variable?
              Every variable needs to be initialized otherwise it just contains the random bits in memory where it was allocated. One of the biggest error areas is in using uninitialized variables. C++ constructors are a way to automate this process.

              Comment

              • RADAR
                New Member
                • Oct 2006
                • 21

                #8
                thank you so much.
                that helped me a lot...

                Comment

                Working...