Net Beans C++ Code Giving always error in the output - c++

#include <iostream>
using namespace std;
int main ()
{
cout << "test" << endl;
return 0;
}

Related

why is the sleep() command not work when I am writing in c++

every time I use sleep() it just says its not recognized:
main.cpp: In function 'int main()':
main.cpp:16.5: error: 'sleep' was not declared in this scope
16 | sleep(2);
| ^~~~~
I keep looking it up but nothing seems to work
#include < iostream >
using namespace std;
int main()
{
cout<<"Hello World";
sleep(2);
cout<<"hello world";
return 0;
}
#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;
using std::chrono::system_clock;
auto now_str()
{
auto now = system_clock::to_time_t(system_clock::now());
return std::ctime(&now);
}
int main()
{
std::cout << "Start: " << now_str() << std::flush;
std::this_thread::sleep_for(2s);
std::this_thread::sleep_for(200ms);
std::cout << " Stop: " << now_str() << std::flush;
return 0;
}
Live demo
std::chrono_literals
std::this_thread::sleep_for
system_clock::to_time_t
std::ctime

How to get user name from cpp in Cmake project?

I am trying to just get user name. i.e., it's equivalent to whoami in ubuntu machine. But I am unable to get. I have tried following snippets.
method-1:
std::string get_username() {
struct passwd *pwd = getpwuid(getuid());
if (pwd)
return pwd->pw_name;
else
return "(?)";
}
method-2:
#include<iostream>
#include <cstdio>
using namespace std;
int main()
{
char text[255];
FILE *name;
name = popen("whoami", "r");
fgets(text, sizeof(text), name);
cout << "Name is : " << text;
pclose(name);
cout << endl;
return 0;
}
method-3:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
cout << getenv("USER") << endl;
cout << getenv("HOME") << endl;
return 0;
}
The all methods are returning value as I expected. But, When I integrate this code into my Cmake project, it always returns root. I am confused why I am always getting root as response when I try with Cmake.
How to get right value instead of root?

I found something while playing with VS2017

While I'm trying to learn throw catch I just compiled my code and I found this output what does that mean?
#include "stdafx.h"
#include <iostream>
using namespace std;
void MightGoWrong() {
bool error = true;
if (error) {
throw 8;
}
// -------------------------
int main()
{
cout << MightGoWrong;
return 0;
}
And output is : 012211A4 what does that mean?
Output
Code
You are not calling your function.
cout << MightGoWrong; is simply printing the address of the function. To call it you should do cout << MightGoWrong();.

rapidjson + c++: "abort() has been called" error

I need to parse json in my C++ program. I decided to use RapidJson library for this purpose, but I got "abort() has been called" error. I truncated the code to this:
#include <iostream>
#include <cstdlib>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/encodings.h"
#include "rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson;
typedef GenericDocument<UTF16<> > WDocument;
typedef GenericValue<UTF16<> > WValue;
wchar_t request[] = L"{\"result\":\"OK\"}";
int main()
{
WDocument d;
d.Parse(request);
WValue& v = d[L"result"]; // The exception throws here
if (wcscmp(v.GetString(), L"OK"))
{
cout << "Success!" << endl;
}
else
cout << "Fail!" << endl;
system("pause");
return 0;
}
but I got the error again. Where is the mistake? Thanks in advance!
check this line:
wchar_t request[] = L"{\"result\":\"OK\"}";
there is a character before the left brace.

Launching a program with Voce

I've compiled with success my first program with the Voce library but when I start the program, it stops right after. I can't say any word and it stops
#include <iostream>
#include <string>
#include <voce.h>
#include <windows.h>
using namespace std;
int main()
{
voce::init("voce/lib", false, true, "voce/lib/grams", "digits");
while (voce::getRecognizerQueueSize() > 0)
{
std::string s = voce::popRecognizedString();
std::cout << "You said: " << s << std::endl;
}
system("PAUSE");
return 0;
}
I took this code on : http://voce.sourceforge.net/
Thank you for your help