0

My code below outputs nothing when it starts. It prints the following when I type 1,2,3:

1Enter the value of argv[1]:49
2Enter the value of argv[2]:50
3Enter the value of argv[3]:51

I am quite confused as to where refresh() should be placed when using a loop. I am trying to achieve something like the comments inside the for loop.

int main()
{
    initscr();
    int argv[3];
    int argvLen = sizeof(argv)/sizeof(*argv);

    for (int i=0; i<argvLen; i++)
    {
        int n = getch();
        printw("Enter value of argv[%d]: %d \n", i+1, n);
        argv[i] = n;
        refresh();

        //cout << "Enter value of argv[" << i+1 << "]:" << endl;
        //cin >> argv[i];
    }

    endwin();
    return 0;
}
Marco Lau
  • 579
  • 2
  • 10
  • 25
  • Side note, `argv` is conventionally the name for the parameter that holds the command-line arguments to `main()`. The use of that name here for something that appears totally unrelated is confusing. – svk Sep 04 '13 at 09:16
  • Oh dear, I may have fumbled on that. I have not really used command-line arguments in C++. The instructions were to take user ints argv[1], argv[2], and argv[3]. Does this mean that main should be like this? int main(argv[]) – Marco Lau Sep 04 '13 at 14:54
  • The signature of main is usually `int main(int argc, char *argv[])`. `argc` is the argument count and `argv` are the arguments. `argv[0]` is usually the name of the program. See [this](http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean). Be certain you don't access `argv[1]` if the program is run without arguments (`argc == 1`) and so on -- if you do this you're going into undefined behaviour and the best case scenario is a crash. All the arguments are strings. You can use something like `atoi()` as a simple way to convert a decimal string to an `int`. – svk Sep 04 '13 at 15:13

4 Answers4

0

getch() returns a char, such as '1', '2' or '3'. The integer values of these are 49, 50, 51. If you need the integer value, then you should subtract '0'.

int n = getch() - '0';

Beware this only works with digits (0 to 9). If you enter anything else it won't give you the expected answer, so you probably want to add additional checks there.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
0

Assuming you're in delay mode, getch() will block to wait for user input.

First do your printw() to put something in the screen buffer, then do your refresh() to make the modified screen buffer visible, then do getch().

svk
  • 5,854
  • 17
  • 22
0

You can use scanf and related functions to read the integer values.

int main()
{
    int i;
    int values[3];
    int valuesLen = sizeof(values)/sizeof(*values);

    for (i=0; i<valuesLen; i++)
    {
        printf("Enter value of values[%d]: ", i+1);
        scanf("%d", &values[i]);
        printf("Value of values[%d]: %d \n", i+1, values[i]);                
    }

    return 0;
}
Jibin Scaria
  • 458
  • 4
  • 12
0

No explicit call to refresh is needed, because getch does a refresh, as noted in the manual page:

If the window is not a pad, and it has been moved or modified since the last call to wrefresh, wrefresh will be called before another character is read.

As written, the program does not appear to display anything because initially curses is in cooked mode. Adding calls to cbreak and noecho after the call to initscr would let individual characters be entered (again, see manual page for ncurses).

With those changes, there is an additional problem: the last printw will not be displayed. Putting a getch() just before the endwin() fixes that.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105