Func getch trouble.



  • #include <conio.h>
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <stdlib.h>.
    using namespace std;
    void you_must_enter_a_number(int);
    int main(){
       you_must_enter_a_number(2);
       return 0;
    }
    
    void you_must_enter_a_number(int size){
       int result = 0, controller = 0;
       char* nums;
       nums = new char[size+1];
       char c;
       while((c = _getch() != '\r') && (controller < size)){
        cout << c << endl;
        nums[controller] = c;
        controller++;
        //cout << controller << endl;
       }
       nums[controller] = '\0';
       cout << atoi(nums) << endl;
    }
    

    The purpose of this code is to consider the text introduced with the consoles in a spontaneous manner, placing each symbol in the symbols of nums. Somehow getch counting an established symbol when trying to display it, smiling mugs are displayed.

    What's the reason?



  • I wouldn't recommend using non-standard functions like that. like, _getch

    This requirement in the proposal while

    (c = _getch() != '\r')
    

    equivalent to the following condition

    (c = ( _getch() != '\r') )
    

    I think you meant

    ( ( c = _getch() ) != '\r' )
    

    Remembrances placed under the area addressed by the indicator should also be released nums:

    delete []nums;
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2