468,512 Members | 1,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

C++ using keyboard system inputs

RADAR
21
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:
Expand|Select|Wrap|Line Numbers
  1. char select;
  2. string str;
  3.  
  4. while(select != char(26)) //--char(26) is the ASCII equivalent of Ctrl+Z , i guess
  5. {
  6.              cout     <<" Enter a string";
  7.             cin >>str;
  8.             locate_asterisk(str);//--a function that adds '*' after each char in a cpp          
  9.                                         //--string
  10.             cout       <<"If you want to continue, press any character"
  11.                          <<"or to exit perform Ctrl+Z from keyboard"<<endl;
  12. }
  13.  
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
Dec 27 '07 #1
7 3137
gpraghuram
1,275 Expert 1GB
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:
Expand|Select|Wrap|Line Numbers
  1. char select;
  2. string str;
  3.  
  4. while(select != char(26)) //--char(26) is the ASCII equivalent of Ctrl+Z , i guess
  5. {
  6.              cout     <<" Enter a string";
  7.             cin >>str;
  8.             locate_asterisk(str);//--a function that adds '*' after each char in a cpp          
  9.                                         //--string
  10.             cout       <<"If you want to continue, press any character"
  11.                          <<"or to exit perform Ctrl+Z from keyboard"<<endl;
  12. }
  13.  
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
Dec 27 '07 #2
weaknessforcats
9,207 Expert Mod 8TB
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.
Dec 27 '07 #3
RADAR
21
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
Dec 28 '07 #4
weaknessforcats
9,207 Expert Mod 8TB
Your code sampoe does not work since select is never initialized and you never use it to get data from the keyboard. Try this:
Expand|Select|Wrap|Line Numbers
  1. string str;
  2. cout     <<" Enter a string";
  3.  
  4. while(cin>>str)
  5. {
  6.              locate_asterisk(str);//--a function that adds '*' after each char in a cpp          
  7.                                         //--string
  8.             cout       <<"If you want to continue, enter a string"
  9.                          <<"or to exit perform Ctrl+Z from keyboard"<<endl;
  10. }
  11.  
Dec 29 '07 #5
RADAR
21
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?
Dec 29 '07 #6
weaknessforcats
9,207 Expert Mod 8TB
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.
Dec 30 '07 #7
RADAR
21
thank you so much.
that helped me a lot...
Dec 30 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Brian Basquille | last post: by
3 posts views Thread by bwgames | last post: by
2 posts views Thread by =?Utf-8?B?TXJOb2JvZHk=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.