[try Beta version]
Not logged in

 
Need help with console

May 8, 2008 at 9:33am
Hi all,

I'm trying to read the console after I press CTRL+C.
Now I've been able to catch the CTRL+C and then do some stuff. However I can't get the input. It's like it needs a '/n' to get the input.

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "main.h"

using namespace std;
string* filename,*input;

void processInput(int sig)
{
    cout<<endl<<"Processing input.."<<endl;
    cout<< "Input = "<< *input << endl;
}

int main(int argc, char* argv[])
{
    Main* main = new Main(argc,argv);
    system("PAUSE");
    return 0;
}

Main::Main(int argc, char* argv[])
{
    signal(SIGINT,&processInput);
    if(argc >1)
    {
        for(int i=1;i<argc;i++)
        {
            filename = new string(argv[i]);
        }
        cout << "Reading file named: "<<filename->c_str()<<".txt"<<endl;
    }
    else
    {
        cout<<"::You've chosen to enter code manually::"<<endl<<"::Press CTRL+C to exit::"<<endl<<endl<<"::Start entering your code::"<<endl;
        
        input = new string("");
        char c;
        while(cin.get(c))
        {
            *input+=c;
        }
    }
}

main.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef MAIN_H
#define MAIN_H

#include <cstdlib>
#include <iostream>
#include <string>
#include <signal.h>

using namespace std;

class Main
{
      public:
             Main(int argc, char * argv[]);
};

#endif 

Here's an example of what I enter.
If I enter "test" in the console it prints out this:"Input = "
If I enter "test" end press enter "bla" it prints out this: "Input = test"

So my question is, How do I get the first line without having to press enter?
Same goes for multiple lines in which case I need to get the last line.

Thanks in advance, Rope.
May 14, 2008 at 7:30am
I'm I not giving enough information? Or does nobody know?
May 14, 2008 at 8:31am
closed account (z05DSL3A)
You could try using a bool to control the input while loop.

1
2
3
4
5
6
7
    bool finished = false;  //member var
...
    while(!finished)
    {
        cin.get(c)
        *input+=c;
    }


Then set finished to true in your CTRL-C handeler
May 14, 2008 at 12:52pm
Hi Rope,
I have a small piece of code.
But the problem with this is that while inputting the data the cursor does not move to new line when user enters carriage return. But after CTRL + C ur required data is getting displayed.

If somebody can sort out this then it will solve ur problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
		int c = 0;
		while(true)
        {
			c=getch();

			if(3==c)/// Int value of CTRL+C
			{
				raise(SIGINT);
				break;
			}
			else if(13==c)/// Int value of new line.
			{
				cout<<(char)c;
				*input+="\n";
			}
			else
			{
				cout<<(char)c;
	            *input+=c;
			}
        }


I know this have some problems with it still den if it helps :-O
May 14, 2008 at 5:07pm
The answer depends entirely on what OS you are using.
Are you using Windows or Unix/Linux?

BTW. Ctrl-C is a poor choice. You'd be better off using ^Z (on Windows) or ^D (on Unix) to terminate input, then simply cin.clear() to reset the input stream.

Or even better, watch the input for a specific string, or two consecutive newlines, or something.

Let me know what you want to do.
May 15, 2008 at 2:08am
Hi Rope,
A small correction to my above post...
Please make a small change to the below code.

1
2
3
4
5
			else if(13==c)/// Int value of new line.
			{
				cout<<(char)c;
				*input+="\n";
			}


to

1
2
3
4
5
			else if(13==c)/// Int value of new line.
			{
				cout<<endl;
				*input+="\n";
			}


For Windows platform and U have to include conio.h header file.
May 15, 2008 at 7:33am
For Windows platform and U have to include conio.h header file.


Really? For which function?
May 15, 2008 at 8:32am

Really? For which function?


What header file needs to be included for getch() in Windows then???
May 15, 2008 at 8:44am
needs to be included for getch()


Ok. I suggest two better alternatives:
 
c = std::getchar();

http://www.cplusplus.com/reference/clibrary/cstdio/getchar.html

 
c = std::cin.get();

http://www.cplusplus.com/reference/iostream/istream/get.html


Don't make your code depend on deprecated headers from the MS-DOS era when there's no reason for it.
Last edited on May 15, 2008 at 8:45am
May 15, 2008 at 11:35am
Thnx Ropez for the links..

I had earlier tried the same[std::getchar() n std::cin.get()] instead of getch().

But that didn't fulfill the requirement of Rope [:-(].
I Think [Please clarify]....
The problem with std::getchar() and std::cin.get() is that it doesn't execute the next line until I press carriage return.

Thus the inputted data is only available iff user presses carriage return, which is the main problem of Rope.

Waiting for suggestion in this regard.
Last edited on May 15, 2008 at 11:36am
May 15, 2008 at 4:37pm
That has nothing to do with the functions. It is an OS-dependent quality of the console input device. You have to turn it off.

On windows:
1
2
3
4
5
6
7
8
9
10
11
#include <windows.h>

void linebuffered( bool b )
  {
  DWORD  mode;
  HANDLE hConsole = GetStdHandle( STD_INPUT_HANDLE );
  GetConsoleMode( hConsole, &mode );
  if (b) mode |=  ENABLE_PROCESSED_INPUT;
  else   mode &= ~ENABLE_PROCESSED_INPUT;
  SetConsoleMode( hConsole, mode );
  }


To turn off line buffering, use:
linebuffered( false );

and to turn it back on, use:
linebuffered( true );
Topic archived. No new replies allowed.