When I look in windows task manage it says it's using 3 threads? Why is this? I was expecting just 1 thread to be used.
I used Netbeans IDE and MinGW-Windows g++ to compile it.
Thanks
Code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
int input;
cin >> input;
return (EXIT_SUCCESS);
}
Maybe Netbeans put some wrapper for internal purpose ? (debugging, profilling, ...) Anyway it don't matter because you didn't create it : these threads should not interfere with your program and your program will not interfere with them.
I don't use Task Manager or Netbeans, but can I suggest you may have misread the output:
one thread to start a shell
one thread for the shell to execute your program
one thread for your program's executable
Total 3. None except the last have anything to do with C++.
Related
Running the code returns no errors, the problem is when I try to exit the program.
Simply, when i use the exit, return or abort functions, i get this (picture below)
see code below
//First 2 libs handle info exchange
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
using namespace std;
main(){
string username;
cout << "Please enter a general use handel (\"username\")\n";
cin >> username;
fstream blockUsername;
blockUsername.open("Player_Data/username.txt", ios::out); //out is for writing, in is for reading
blockUsername << username;
blockUsername.close();
return 0;
}
I'm aware that similar questions have been asked, but I have not succeeded in fixing my code with those.
What I want is for the program to close with no exit values, even though I know that they are useful.
PS: It very well could be something to do with my code editor (Dev C++), or my compiler which i broke and had to fix recently.
Also side note how would i go about removing that address at the top of the terminal (and potentially replacing it with something else)?
These are printed by your IDE....if u execute the .exe output of this code...the console would just disappear after program exits and it would not be shown
Peace
try using return 0; wherever you want to exit, it's okay to have more than one return 0; in your code or to simply put it after an if statement.
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.
My program has many different threads handling different things, and one of them deals with user input.
The other threads don't have much in the way of blocking calls, and those that do block are network based so will be interrupted or return gracefully when the socket is shut down.
However the user thread has calls to std::cin in order to grab the user input. The effect this has is while all the other threads are dead the user thread is still blocking on user input, and will only die the next time input is given.
Is there any way for me to check if there is any user input to grab before blocking?
I understand cin.peek() exists but from my experience, it blocks if there is nothing to read in. Assuming I'm using it correctly
My code is basically an infinite loop that stops when another thread switches the condition variable:
void doLoop()
{
while (running) //running is shared between all threads and all others die quickly when it is false. It's set to true before the threads are started
{
string input = "";
getline(cin, input);
//Handle Input
}
}
I'm on windows, using VS2013, and cannot use external libraries. I'm using windows.h and std throughout.
I believe that the C++ Standard does not offer a way of checking the standard input without blocking. Since you are willing to use platform specific functions, 'kbhit()' might suit your needs but it has been deprecated in Windows. An alternative is offered, _kbhit(). Of course this is not portable to other platforms.
This is the link to MSDN: _kbhit
What you could do is using futures to allow the user to input something with a time limit. You can then insert this code into your main loop
#include <iostream> // std::cout
#include <future> // std::async, std::future
#include <chrono> // std::chrono::milliseconds
#include <string>
using namespace std;
bool myAsyncGetline(string & result)
{
std::cout<<"Enter something within the time limit"<<endl;
getline(cin,result);
return true;
}
int main()
{
// call function asynchronously:
string res;
std::future<bool> fut = std::async (myAsyncGetline,res);
std::chrono::seconds span (20);
if (fut.wait_for(span)==std::future_status::timeout)
std::cout << "Too Late!";
else
cout<<"You entered "<<res<<" "<< endl;
return 0;
}
This is available in VS2012 so you should be able to reproduce it.
The output is "Tool Late!" if getline is still working after the timeout (20s), otherwise it outputs the result.
I think that it is simpler than messing around with killing thread as the function stop by itself if the time limit is hit.
Tell me if you need help integrating it into your existing code I can assist.
I'm trying to execute a very simple program that runs "ls" command
Im working under Mac OS 10.7, with XCode and C++
This is the code:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world" << endl;
execl("/bin/ls","ls",NULL);
return 0;
}
It crashes after following output
Hello world
No memory available to program now: unsafe to call malloc
I tried to google it but no luck, any ideas on what I might be doing wrong?
This is just "my opinion"
From man page:
The exec family of functions replaces the current process image with a
new process image.
It could be that it tries to replace the debugger process and so it crashes (the app is run from Xcode..). If you execute the app from command line it works...
Seems to work fine:
http://ideone.com/8AoZ3
But seems like on your platform some sort of weird recursion is taking place. Can you change your call to:
execl("/bin/ls","/bin/ls",0);
I know this may not be exactly what you want to do, but the following SO question is using execv to execute echo:
how-to-create-a-process-on-mac-os-using-fork-and-exec
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to stop C++ console application from exiting immediately?
I am running a simple program, written in Dev C++ 4.9.9.2 IDE in Windows 7:
// my second program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
system("pause");
return 0;
}
This compiles successfully, but when I run it, the terminal screen comes for a second and then vanishes. How can I keep the output screen of the program visible?
You need to do cause the program to stop and wait for input. Use
system("pause");
at the end of your program before the return from main.
In addition to the options already presented (std::getchar, cin, system("pause"), if the only reason you want the window to persist is to read the output of your program (i.e. debugging), you can simply run the executable from a command prompt.
If you don't mind running the application this way, you can avoid having extra code to prompt a user for input (even if it just a single line) - and if you don't need the window to stay open under normal usage, you don't have to modify anything about your code.