I am using ncurses to develop an application in C++ but both the newwin() and mvwin() functions are not working as expected. Whenever I give either of these function argument values in an attempt to make a new window that is of different size then the initial terminal window, nothing appears to happen. For example, the following code should create a new window with a height of 50, a width of 10, at location (10,10). None of this appears to be happening:
#include <ncurses.h>
#include <iostream>
using namespace std;
int main(){
initscr();
WINDOW * win = newwin(50,10,10,10);
wrefresh(win);
getch();
endwin();
return 0;
}
From the documentation I have read, it appears that a window will be created with default sizing and location if any of the arguments passed to it are invalid values, but to my knowledge all of these are valid values. Does newwin() not work like I think it does? Thank you in advance
The question asks about mvwin, but that does not create windows, nor is there an example in the question of a problematic call.
For newwin, zero parameters are handled specially, not "invalid values". The question shows an apparently valid call (and in a quick check, it runs as expected).
However, OP is likely confused that there is no output. That is because the program creates a window, which is empty and then reads from the standard screen.
This altered program produces output by drawing a box on the new window and then reading from that window (preventing the wrefresh associated with getch from painting over the new window):
#include <ncurses.h>
#include <iostream>
using namespace std;
int main(){
initscr();
WINDOW * win = newwin(50,10,10,10);
box(win,0,0);
wrefresh(win);
wgetch(win);
endwin();
return 0;
}
It sounds like there's some misunderstanding here of what ncurses does.
You mention an "attempt to make a new window that is of different size then the initial terminal window" -- if you mean a terminal emulator running in a windowing system like X/OSX/Windows, that is not what ncurses does. A "window" in ncurses is a rectangle within an existing terminal that can be updated/refreshed independently.
This might be why the comments on the other answer seem to be talking past each other.
Related
I'm taking an online computer science class for grade 12 and we're using c++. I've never touched c++ and I'm starting to wish I never had. The teacher is comparing c++ to java (a language I can use perfectly fine) and we're currently learning how to input and output strings and chars. The simple practice problem was
Use one of fputc(), putc(), or putchar() to print your name one char at a time.
Since I have no clue how to use fputc() or putc() I decided to go with putchar()
#include <iostream>
using namespace std;
#include <stdio.h>
int main() {
cout << "My name is :" << endl;
putchar('J');
putchar('a');
putchar('c');
putchar('o');
putchar('b');
return 0;
}
I tried just using putchar(), and then added the cout, and have tried restarting eclipse, etc. but every time I run the program, the console asks for an input. There should not be an input for this program at all.
Try running your program from outside of the IDE and see what happens. When you launch a console program from inside of an IDE, a new console window is created to run the program in. When the program ends, the console window will close. Many IDEs setup the console to wait for you to press a key, giving you a chance to see the program's output, before the window closes.
I am too confused with console screen. I am not able to figure out how to move cursor to random position. This is my code:
#include<iostream>
#include<conio.h>
main()
{
cout<<"Hello World";
return 0;
}
I want to move cursor to random position, not at the beginning after pressing Enter.
There is no standard way to affect the terminal cursor in C++. So, the first step in implementing this is to figure out what system you are targetting, and what API it offers.
Why does my C++ output screen disappear immediately? I'm a beginner in cpp. Can anyone help me to find the problem please?
You should either launch your application inside of a terminal, or add a line of code that waits for the input in order for the window to not close. E.g. add in the end of the function main a line:
std::cin.get();
And also add at beginning of the file the include that holds that function.
#include <iostream>
This is hard to answer since there can be many things that can cause your output box to close immediately. First try having a cout statement and then a cin statement. Something like:
cout<<"Hello"<<endl;
cin>>input>>endl;
Also make sure to have the necessary include statement at the top and whatever you want to return at the bottom.
#include<iostream>
return 0;
As you have said that you are beginner in C++, you should keep in mind ,three major things while coding in C++. You've mentioned that your screen disappears , then following things you should try.
1). in C++ conventionally main returns value of type int.And the format of your program should be like...
int main()
{
-------
//body of your program
-------
return 0;
}
If the function returns 0, that means it ran successfully.
2). You have to inlcude #include<iostream> on the top of your program.
3).check whether the IDE you are using is compatibale to your operating system or not.
Hope this will help you.
Why the program just exits, in vs 2012, while in 2010 does it wait for my input?
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string path = "c:\somepath";
cout << "Path is" + path << endl;
getchar();
return 0;
}
The getchar() function is not used in the program flow and only the side effect of waiting for input is used in your case. Instead of hacking your program to avoid closing the window you should use the proper way to keep it open.
Choose one of:
Command window
open a command prompt
navigate to the folder where you want your program to execute
type the path to the .exe and hit Enter. The command prompt is still open after execition of your program and there is not need for getchar().
Start without debugging
just use the menu item Debug->Start Without Debugging. The new console window is still open after execition of your program and there is not need for getchar().
Start with debugging
Set a breakpoint at the closing '}' of your main function.
use the menu item Debug->Start. The debugging session is still active and the window for your console program is still open. There is not need for getchar().
Why you should avoid getchar() to keep a window open?
It's a hack that depends on the program before. There can be some characters in the input buffer a s pointer out here: getchar() doesn't work well?
Your program can't be used in batch file if you come beyond the phase where you try to learn C or C++.
It's not necessary.
This question already has answers here:
Keeping console window open when debugging
(3 answers)
Closed 8 years ago.
Hi I am using visual studio express 2013.I have never used vs before and so just to test it out I ran a simple c++ program where the user enters 2 integers and their sum is then displayed. The problem is that the console window appears and takes the inputs, but then immediately closes once the output is displayed. Please note that this happens right after all the inputs are taken and when the output is shown. Is there anyway to fix this? I have looked all over and cant find a solution. I have tried including a bunch of things such as the getch() function at the end of my program, and pressing ctrl F5 to debugg my programs,but nothing seems to work. Please help!!!
I have been using this,
int getchar ( void );
Get character from stdin
Returns the next character from the standard input (stdin).
OR
use this from process.h
system("PAUSE");
This approach is for beginners. It's frowned upon because it's a platform-specific hack that has nothing to do with actually learning programming, but instead to get around a feature of the IDE/OS - the console window launched from Visual Studio closes when the program has finished execution, and so the new user doesn't get to see the output of his new program.
One decent approach is Debug.WriteLine
// mcpp_debug_class.cpp
// compile with: /clr
#using <system.dll>
using namespace System::Diagnostics;
using namespace System;
int main()
{
Trace::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
Trace::AutoFlush = true;
Trace::Indent();
Trace::WriteLine( "Entering Main" );
Console::WriteLine( "Hello World." );
Trace::WriteLine( "Exiting Main" );
Trace::Unindent();
Debug::WriteLine("test");
}
In debug mode, Before your main return you could put a breakpoint and you will be able to see your console and the result you are waiting for.
Put
system("PAUSE");
wherever you need the program execution window to pause. If you're using int main(), usually it'll be right before you return 0