I'm using the following application
#include <iostream>
using namespace std;
void print(char* s)
{
cout << s << endl;
}
int main(int argc, char* argv[])
{
print("a");
print("b");
print("***");
print("c");
print("d");
}
When I run it with the debugger under VSCode (using "Native Debug" extension), it never pass the second print and this is my output:
a
b
It never pass print("***), even if I put a breakpoint after that line it doesn't reach it.
if I comment out print("***"), the application finish with the correct output:
a
b
c
d
The only thing I managed to figure out is that there is and issue printing "*" character and if I replace it with any other character all works fine. Why do I see this behavior and how can I fix this without having to change the code?
#molbdnilo, seem to be correct. It looks like a bug in "Native Debug" extension. The problem is not reproduced when another extension is used (I tried with Microsoft's "C/C++" extension)
Related
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();
}
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.
I am beginning desktop programming with c++, and when I run the following code:
#include <stdio.h>
int main (int argc, char ** argv){
enum{ max_string = 127};
static char string[max_string + 1] = "";
printf("Type in a line\n");
fgets(string,max_string,stdin);
printf("the string is %s\n",string);
return 0;
}
I don't see the "Type in a line" prompt when I run the program inside Eclipse. Instead if I just type in a response, I see what I typed followed by:
Type in a line
the string is Hello World
Why doesn't it first show the prompt "type in a line" before I type in my input?
You need to flush the output buffer. I just added std::cout << std::endl; and include <iostream>. I would suggest you start using std::cout instead of printf. It's type safe. I tested this inside of Eclipse, without the flush, the line doesn't show up, with the flush it does.
If you don't want to move to <iostream> I just tested fflush(stdout) from <stdio.h> and it works too. However, I strongly encourage the move, stdio.h is the standard for C, the iostream is the standard for C++ and since you're learning C++...
Why is it that when I run this code, the cout statement is only executed after I press the enter key twice?
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
char buf[128];
cin.getline(buf, 128, '\n');
cout << buf;
return 0;
}
How can I fix it?
I'm using Xcode Version 4.5.2 on OS X Lion. I tried running the program in Terminal and it requires two return presses too.
This is similar to this question, but with less code so the solution should be clearer. I.e. in the other question, people were saying that the problem is the cin.ignore(), however, my code doesn't have that.
Other code I tried which failed:
This produces the same exact behavior:
cin.getline(buf, 128);
This never moves on to the next line (no matter how many times the return key is pressed):
cin.getline(buf, 128, '\r');
I found the problem. It appears to be a problem with the compiler.
When I go to my project in Xcode and switch the compiler from Default compiler (Apple LLVM compiler 4.1) to LLVM GCC 4.2 everything works properly.
I know there's been a handful of questions regarding std::ifstream::open(), but the answers didn't solve my problem. Most of them were specific to Win32, and I'm using SDL, not touching any OS-specific functionality (...that's not wrapped up into SDL).
The problem is: std::ifstream::open() doesn't seem to work anymore since I've switched from Dev-C++ to Code::Blocks (I've been using the same MinGW-GCC back-end with both), and from Windows XP to Vista. (It also works perfectly with OS X / xcode (GCC back-end).)
My project links against a static library which #includes <string>, <iostream>, <fstream> and <cassert>, then a call is made to functionality defined in the static library, which in turn calls std::ifstream::open() (this time, directly). Following this, the stream evaluates to false (with both the implicit bool conversion operator and the good() method).
Code:
#include "myStaticLibrary.hpp"
int main(int argc, char **argv)
{
std::string filename("D:/My projects/Test/test.cfg");
std::cout << "opening '" << filename << "'..." << std::endl;
bool success(false);
// call to functionality in the static library
{
std::ifstream infile(filename.c_str());
success = infile.good();
// ...
}
// success == false;
// ...
return 0;
}
stdcout.txt says:
opening 'D:/My projects/Test/test.cfg'...
When I open stdcout.txt, and copy-paste the path with the filename into Start menu / Run, the file is opened as should be (I'm not entirely sure how much of diagnostic value this is though; also, the address is converted to the following format: file:///D:/My%20projects/test/test.cfg).
I've also tried substituting '/'s with the double backslash escape sequence (again, slashes worked fine before), but the result was the same.
It is a debug version, but I'm using the whole, absolute path taken from main()'s argv[0].
Where am I going wrong and what do I need to do to fix it?
Please create a minimal set that recreates the problem. For example, in your code above there's parsing of argv and string concatentation, which do not seem like a necessary part of the question. A minimal set would help you (and us) see exactly what's going wrong, and not be distracted by questions like "what's GetPath()?".
Try to do this instead of assert(infile.good()):
assert(infile);
I have overseen the importance of the fact that the function in question has close()d the stream without checking if it is_open().
The fact that it will set the stream's fail_bit (causing it to evaluate to false) was entirely new to me (it's not that it's an excuse), and I still don't understand why did this code work before.
Anyway, the c++ reference is quite clear on it; the problem is now solved.
The following code:
#include <string>
#include <iostream>
#include <fstream>
#include <assert.h>
using namespace std;;
int main(int argc, char **argv)
{
std::string filename("D:/My projects/Test/test.cfg");
std::cout << "opening '" << filename << "'..." << std::endl;
std::ifstream infile(filename.c_str());
assert(infile.good()); // fails
return 0;
}
works fine on my Windows system using MinGW g++ 4.4.0, if I create the required directory structure. Does the file test.cfg actually exist? If you are opening a stream for input, it wioll fail if the file is not there.
Edit: To remove any DevC++ to CB issues:
build using command line only
make sure you rebuild the static library too