No console output in simple c++ program - c++

I noticed a weird behaviour of some of my programs in c++ and when I was trying to figure out what coused it, I found out, that something wrong is going on with my console outputs. I used iostream and cstdio functions with the same behaviour. When I print something on console It doesn't display at all. Here are codes that I used for observing this strange behaviour.
This piece of code outputs everything propertly (even if it shouldn't IMO):
#include <cstdio>
using namespace std;
int main(void) {
int a = 0;
scanf("%d", &a);
a++;
printf("result is %d", a);
}
This one however(correct I think) doesn't display anything, only the run finished message
#include <cstdio>
using namespace std;
int main(void) {
int a = 0;
scanf("%d", &a);
a++;
printf("result is %d \n", a);
}
I also tried it with removed space before "\n" with no difference. However, when I place more the same printf functions with "\n" at the end to the program, everything displays correctly (multiple times of course). Iostream behaves in similar way - using endl doesn' t cause anything to appear on console. What am I doing wrong? As to the original code that caused malfunctioning, I noticed that on my output nothing appeared but in my school, the same code output everything correctly. I am working under NetBeans 8.0.2. Thanks in advance for help

All output data is buffered, before it getting printed to console. You can use fflush or \n to flush the output stream and print all data.

Related

C++ cout is not printing to command prompt

I'm getting started with C++ and am trying to work with vectors and printing stuff. I am using Windows 10, writing my code in Visual Studio Code, I just downloaded the MinGW-g++ compiler, and am trying to run my code in CMD.
This is my code, I've tried printing different things, and it's not even printing this "Hello". I've also tried using << endl; which also prints nothing. I also tried adding and remove a "return 0;" at the end but it changed nothing. The program compiles just fine. Any advice?
#include <iostream>
using namespace std;
int main(void)
{
std::cout << "Hello";
std::cout.flush();
}

getline(ifstream, string) on Mac causes EXC_BAD_ACCESS

I've been a C++ developer since it arrived. All was on windows, and I haven't touched it in about 6 years.
Now I'm trying to get an old code-base working using VS Code on my Mac. I'm using clang++ with c++17.
This problem is vexing; I've seen many other posts with the same issue, but the problem always seemed to be something in the code.
Note: this code worked fine with C++11 on Windows.
To simplify, I copied the code to execute right at the top of main. Here is is:
ifstream file("assets/textures/blocks.txt", ios::in);
if( file.is_open() ) {
string s;
getline(file, s); // <-- This line causes the error.
cout << s << endl;
}
As this code worked elsewhere, I assume I've got a setup or environment problem and am looking for hints towards what to check on.
Thank you for any help!
An update:
Thank you. I paired the program down and tried a few things. Here's the deal:
If I leave all my files to be compiled, but replace main.cpp with the below code, the cout line generates the same exception.
If I cull all the unused files, the code works.
Something in some other file is somehow breaking the stream code. I'm clueless.
#include <iostream>
#include <istream>
using namespace std;
int main() // int argc, char** argv)
{
cout << "Hello World" << endl;
}
I should add: this is a GLFW 3D game engine app. It does not subclass or interact with any stream in any way other than the most basic file read/write operations.

Terminal can't print vector in c++

I am new to C++ and trying my hands on std::vector. But for some reason even a single std::cout doesn't work with the vector I have declared.
Here is the sample code:
#include <iostream>
#include <vector>
using namespace std;
int main(){
std::vector <int> test_score;
test_score.push_back(10);
cout << test_score.at(0);
return 0;
}
The terminal output shows nothing. Here it is.
The same code works fine on IDEs like CodeBlocks.
Just for your info, your code works fine on my terminal without the image description part.
Maybe try to comment these lines out as they are not involve in the problem or try to compile a Hello World program first may help.

how can i execute a file kept on desktop in c++?

my file structure for executing a .exe is something like this
c:\Documents and settings\Desktop\Release\abc.exe
i want to execute this from other c++ program in vb c++ after building, it generates an error that c:\Document is not external or internal command
few lines of code are as follows:
#include<stdlib.h>
#include<stdio.h>
int main( void ) {
int result;
result=system("c:\\Documents and settings\\Desktop\\Release\\abc.exe");
getchar();
return 0;
}
As I suspected when writing an earlier comment, the way to do it is to wrap the entire string in double-quotes. 'Escaping the spaces' sounds non-sensical to me. 25 seconds of googling and I don't see (nor have I heard of in over 20 years) an escape-sequence for a space character in C.
The solution is indeed to include quotes in the string - not to just wrap the string in a single pair of them, as you've done. The following will do the trick:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int result;
result = system("\"c:\\Documents and settings\\Desktop\\Release\\abc.exe\"");
getchar();
return 0;
}
However, that said - you shouldn't really be using the system call for this job. Since you're on a windows machine, you should use the ShellExecute function instead. There are many reasons for this, which I wont go into here, you can look them up yourself. But suffice to say it's an infinitely better way to invoke another program.
More on ShellExecute: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx

Output Issue in dev c++

when i run this code mentioned below, The output console appears for fraction of second and then disappear, but every thing is fine in this code.there is no compile error or warning. i also use get character function but same issue.
i am using dev-c++ 4.9.9.2 version on win 7, 32 bits. what to do?
#include <iostream>
using namespace std;
int main ()
{
cout<<"welcm";
return 0;
}
You can use cin.get() before your return statement to avoid the console window from closing.
You need to tell the program to wait before closing.
cout<<"welcm";
system("pause"); // add this line
return 0;