help with Ncurses getch()

Hello, I am trying to use ncurses but I have run into a problem. Here is my code. Any help is greatly appreciated.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <curses.h>
int x, y, o, i = 0, c = 0;
char t[100], z[100];
int main()
{
    initscr();
    erase();
    refresh();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);
    printw("Please choose a mode \n");
    printw("Press M to goto Multiplication \n");
    printw("Press D to goto Division \n");
    printw("Press A to goto Addition \n");
    printw("Press S to goto Subtraction \n");
    printw("Type R to goto SquareRoot \n");
    printw("Press W to goto Website Downloader\n");
    printw("Press Q to exit \n");
    printw("Press F to goto file writer \n");
    printw("Choice: ");
    char d;
    d = getch();
    refresh();
    erase();
    refresh();
    switch(d)
    {
    case 'm':
        initscr();
        refresh();
        printw("Multiplication \n");
        printw("Enter a number: ");
        printw("\n");
        refresh();
        scanw("%d", &x);
        scanw("%d", &y);
        printw("The answer is: %d", x*y);
        refresh();
        endwin();
        main();
        break;
    case 'd':
        initscr();
        refresh();
        printw("Division");
        refresh();
        scanw("%d", &x);
        scanw("%d", &y);
        printw("The answer is: %d", x/y);
        refresh();
        endwin();
        main();
        break;
    case 'a':
        initscr();
        refresh();
        printw("Addition");
        refresh();
        scanw("%d", &x);
        scanw("%d", &y);
        printw("The answer is: %d", x/y);
        refresh();
        endwin();
        main();
        break;
    case 's':
        initscr();
        refresh();
        printw("Subtraction");
        refresh();
        scanw("%d", &x);
        scanw("%d", &y);
        printw("The answer is: %d", &x-&y);
        refresh();
        endwin();
        main();
        break;
    case 'r':
        initscr();
        refresh();
        printw("SquareRoot");
        refresh();
        scanw("%d", &x);
        sqrt(x);
        printw("The answer is: %d", &y);
        refresh();
        endwin();
        main();
        break;
    case 'w':
        initscr();
        refresh();
        printw("Interface to system commands. \n");
        printw("Command: ");
        refresh();
        scanw("%s", &z);
        system(z);
        endwin();
        main();
        break;
    case 'q':
        main();
        break;
    case 'f':
        initscr();
        refresh();
        printw("File Writer \n");
        printw("Enter a word: ");
        refresh();
        scanw("%s", &t);
        printw("Enter a number: ");
        refresh();
        scanw("%d", &o);
        refresh();
        endwin();
        FILE * a;
         a = fopen ("words.txt", "a");
        for(i = 0; i <= o; i++)
        {
           fprintf(a, "%s",&t);
           fprintf(a, "%u", c++, "\n");
           fprintf(a, "\n");
        }
        fclose(a);
        }
        main();
    }


This where I need to get a specific char before continuing:
1
2
3
4
5
6
7
8
9
10
11
12
    case 'a':
        initscr();
        refresh();
        printw("Addition");
        refresh();
        scanw("%d", &x);
        scanw("%d", &y);
        printw("The answer is: %d", x/y);
        refresh();
        endwin();
        main();
        break;
Last edited on
I don't know much about curses.
From the documentation it looks like that addstr() is only used to output strings. It seems that printw() or mvprintw() is better suited for ur needs.

1
2
3
addstr("The answer is:") << x-y; //instead of this...
printw("The answer is: %d", x-y); //try this..
mvprintw(0, 0, "The answer is: %d", x-y); //or this..prints at coordinate 0,0 
Thanks I used printf("%d", x-y) instead. That is solved but one problem remains. The problem is clearing the screen. I cant figure out how to use erase() which should work to clear the screen with
refresh() but doesn't work.
The erase() function only manipulates the internal buffer, it does not set the flags necessary to update the screen. (It is a lower-level function made available to you for optimizing certain screen operations.)

Use clear() or wclear() to get what you want.
http://linux.die.net/man/3/curs_clear

Good luck!
Thanks I tried it but it says
1
2
C:\cpp\OmniTool\Menu.cpp|10|error: too few arguments to function 'int wclear(WINDOW*)'|
C:\cpp\OmniTool\Menu.cpp|11|error: too few arguments to function 'int wrefresh(WINDOW*)'|
when I try to compile 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <cmath>
#include <curses.h>
int y, x, o, i = 0;
std::string t, l = ("wget "), z;
void menu()
{
    wclear(); //errors occur here.
    wrefresh(); //and errors occur here.
    printf("Please choose a mode \n");
    printf("Press M to goto Multiplication \n");
    printf("Press D to goto Division \n");
    printf("Press A to goto Addition \n");
    printf("Press S to goto Subtraction \n");
    printf("Type R to goto SquareRoot \n");
    printf("Press W to goto Website Downloader\n");
    printf("Press Q to exit \n");
    printf("Press F to goto file writer \n");
    printf("Press V to display version \n");
    printf("Choice: ");
    unsigned char d;
    std::cin >> d;
    switch(d)
    {
    case 'm':
        printf("Multiplication \n");
        printf("Enter a number: ");
        printf("\n");
        scanf("%d", &x);
        scanf("%d", &y);
        printf("The answer is: %d", x*y);
        std::cin.clear();
        std::cin.ignore(1,5);
        std::cin.get();
        menu();
        break;
    case 'd':
        printf("Division");
        scanf("%d", &x);
        scanf("%d", &y);
        printf("The answer is: %d", x/y);
        std::cin.ignore(1,5);
        std::cin.get();
        menu();
        break;
    case 'a':
        printf("Addition");
        scanf("%d", &x);
        scanf("%d", &y);
        printf("The answer is: %d", x/y);
        std::cin.ignore(1,5);
        std::cin.get();
        menu();
        break;
    case 's':
        printf("Subtraction");
        scanf("%d", &x);
        scanf("%d", &y);
        printf("The answer is: %d", &x-&y);
        std::cin.ignore(1,5);
        std::cin.get();
        menu();
        break;
    case 'r':
        printf("SquareRoot");
        scanf("%d", &x);
        sqrt(x);
        printf("The answer is: %d", &y);
        std::cin.ignore(1,5);
        std::cin.get();;
        menu();
        break;
    case 'w':
        printf("Enter the URL for the website that you wish to download to download: \n");
        printf("URL: ");
        std::cin >> z;
        l = l + z;
        system(l.c_str());
        std::cin.ignore(1,5);
        std::cin.get();
        menu();
        break;
    case 'q':
        menu();
        break;
    case 'f':
        printf("File Writer \n");
        printf("Enter a word: ");
        std::cin >> t;
        printf("Enter a number: ");
        std::cin >> o;
        std::cout << ("The word ") << t << (" will be written to a file ") << o << (" times.");
        std::ofstream a("out.txt", std::ios::app);
        for(i = 0; i <= o; i++)
            a << t << i << std::endl;
        std::cout << t << i << std::endl;
        a.close();
        std::cin.ignore(1,5);
        std::cin.get();
        menu();
        break;
    }
}
int main() //put this block here so it would shut up about win main.
{
menu();
}
I don't wish to be unkind, but I am going on about three hours of sleep right now. Did you bother to read the stuff I posted for you? Like the documentation?

Your error message clearly says that you are not giving enough arguments to wclear() and that it is expecting a WINDOW*. The documentation says wclear() takes a WINDOW*. Yet you try to call it without any argument at all.

This particularly confounds me as the function both I and the documentation list immediately before wclear() is clear().

Take a look at that one.


Your program is also likely to fail because you neither initialize or finalize the NCurses library -- a necessary step for preparing your terminal for direct control and restoring it to the usual state before your program terminates.


BTW, your program has a perplexing combination of calls to NCurses I/O functions, C I/O functions, and C++ I/O functions. You should try to stick to just one.


Here is a whole slew of links for you to look through to help you get started with NCurses properly. http://www.cplusplus.com/forum/general/41965/#msg226792

I know it is a lot of work. If all you want is to clear the screen, see http://www.cplusplus.com/articles/4z18T05o/
This requires no more difficulty linking libraries than using NCurses.

Good luck now!
Thanks I did read the documentation it was extremely sparse. I looked else where and figured out how to use the functions but when I use them it displays a blank screen with a blink cursor. second I tried revamping it to fix it using printw("") instead of printf("") this time it just crashes. Oh and thanks ofr the links.
Fixed the crashing by removing interfering streams.
I figured it out.
Topic archived. No new replies allowed.