CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    How to Stop input?

    Hello my friends i've one question:

    i want to stop the input of an user, when he inserts a certain string es "x". Now this part of code:

    Code:
    char * risposte [dim];
    	for (int i=0;i<dim;i++)
    		risposte[i]=new char [dim];	
    	int contarisp=0;
    	
    	cout<<"Inserisci le risposte del cruciverba (max 30). Usa il carattere terminatore 'x' per finire l'immissione di parole.\n";
    	
    cin>>risposte[contarisp];
    	
    	while(strcmp(risposte[contarisp],"x"))
    	{
    		cout<<"Hai inserito "<<risposte[contarisp]<<endl;
    		cout<<"Conta risp &#232; "<<contarisp<<endl;
    		contarisp++;
    		cout<<"ora &#232; "<<contarisp<<endl;
    		cin>>risposte[contarisp];
    	}
    doesn't always work why? How i can stop the "While"?
    Last edited by Falko-tux; February 28th, 2011 at 02:27 PM.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: How to Stop input?

    What is your intent with this:
    Code:
    char * risposte [dim];
    for (int i=0;i<dim;i++)
       risposte[i]=new char [dim];
    Viggy

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How to Stop input?

    Quote Originally Posted by Falko-tux View Post
    i want to stop the input of an user
    You can't using standard C++. All you can do is "ask for some input" and wait till the user/os to return control to your code, with input in your buffer.

    From there, you can choose to ask for more input, or to do something else, but it is not possible to interrupt the user when he inserts a specific character, for example (again, using standard C++).
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    Re: How to Stop input?

    It's an array of pointers, and then I create a new char [] for each position of the previous array.

  5. #5
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    Re: How to Stop input?

    Thank you, so I have to say the max number of inputs the user can write, right?

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    Re: How to Stop input?

    Been lookin' at code too long... I missed the '[dim]' part! D'OH!

    When does the check not work?

    Viggy

  7. #7
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    Re: How to Stop input?

    it doesn't work when I put the "x" string..it must stop but it waits for other strings!

  8. #8
    Join Date
    Feb 2002
    Posts
    4,640

    Re: How to Stop input?

    I don't see a check to see if you've overrun the array...

    Viggy

  9. #9
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    Re: How to Stop input?

    I tried to print the array, but it doesn't arrive there so I put some cout after the cin in the while but the program when I insert "x" seems to 'freeze' and waits for other input!

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: How to Stop input?

    What are you doing after the while loop?

    Viggy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured