Advertisement
Guest User

Interactive input with C++ and Ncurses

a guest
Dec 16th, 2015
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. //: Link with -lncurses
  2.  
  3. #include <string>
  4. #include <sstream>
  5.  
  6. extern "C" {
  7.  
  8. #include <ncurses.h>
  9.  
  10. }
  11.  
  12. int read_value(void) {
  13.     std::string buffer;
  14.  
  15.     do {
  16.         buffer.push_back(getch());
  17.     } while (buffer.back() != ' ' && buffer.back() != '\n');
  18.  
  19.     int value;
  20.     std::stringstream(buffer) >> value;
  21.  
  22.     return value;
  23. }
  24.  
  25. void interact(void) {
  26.     printw("Input your height:\n");
  27.     refresh();
  28.     int feets = read_value();
  29.     printw("feet(s) and ");
  30.     refresh();
  31.     int inches = read_value();
  32.     printw("inche(s).\nAnd your weight is ");
  33.     refresh();
  34.     int pounds = read_value();
  35.     printw("pound(s).");
  36. }
  37.  
  38. void pause(void) {
  39.     printw("\n\nPress any key...");
  40.     refresh();
  41.     getch();
  42. }
  43.  
  44. int main()
  45. {
  46.     initscr();
  47.     interact();
  48.     pause();
  49.     endwin();
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement