This question already has answers here:
Sublime Text with console input for c++ programs
(2 answers)
Closed 8 years ago.
I started to use sublime text 2 editor today to write c/c++ programs, but when I run this code
#include <cstdio>
int main(){
int n;
scanf("%d",&n);
printf("%d\n",n);
return 0;
}
I am expecting the compiler to ask for an input.
but instead, it just prints the initial value of (n) which is garbage.
How can i insert an input ?
I suppose what you want to do is input data in the same panel where Sublime Text show results, well friend, you can't.
The only thing you can do is configure Sublime Text for running the compiled program in an external terminal. The link chris provide you is a good start.
On the other hand when you say: expecting the compiler to ask for an input, is a conceptual error. The compiler is not whom is expecting the input. The input is being expected by the compiled program.
Related
This question already has answers here:
How to get system's decimal separator character using STL?
(1 answer)
How can I set the decimal separator to be a comma?
(2 answers)
Closed 4 months ago.
I've been given some C++ code which (among other things) reads filenames and parses them using sscanf. However, despite using %lf for parsing, sscanf parses the first double truncated, and then fails to parse the rest of the string.
The offending code segment is:
int row, col;
double camx, camy;
char ext[64];
char inp[64] = "out_00_00_-381.909271_1103.376221.png";
int ret = sscanf(inp, //files[i].toStdString().c_str(),
"out_%d_%d_%lf_%lf%s", &row, &col, &camy, &camx, ext);
I get the following parse:
ret=3
row=0, col=0
camy=-381
camx=[gibberish]
ext=[gibberish]
More confusingly, if I run the same code segment in repl.it, it works perfectly.
I'm compiling using CMake on Ubuntu 20.04, GCC 9.4.0 x86_64-linux-gnu. I don't fully understand CMake (since someone else gave me this repo), but I have the following CMakeLists.txt:
include_directories("${PROJECT_SOURCE_DIR}/include" ${OpenCL_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS})
QT5_WRAP_CPP(lf_renderer_HEADERS_MOC LFViewWidget.h LFViewWindow.h)
add_executable(task5_lf_renderer main.cpp LFViewWindow.cpp LFViewWidget.cpp LightFieldRenderer.cpp ${lf_renderer_HEADERS_MOC})
target_compile_features(task5_lf_renderer PRIVATE cxx_range_for)
target_link_libraries(task5_lf_renderer ${OpenCL_LIBRARIES} Qt5::Widgets) #Qt5::OpenGL
where this file is LightFieldRenderer.cpp in the add_executable() call.
I suspect the issue is that some compile flags are being set/unset somewhere which is causing this to compile differently on my machine. Any pointers to where I should look would be much appreciated!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am currently learning how to use text files, but when i put a condition like the one in the code below, even if the condition is true it won't try the while part. Can anyone help out?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string s, d;
ofstream k("t1.txt");
int a;
cin >> a;
if (a > 3)
{
while (k.is_open())
{
getline(cin, s);
k << s;
k.close();
}
}
ifstream r("t1.txt");
while (r.is_open())
{
getline(r, d);
cout << d;
r.close();
}
}
I ran the code in gdb, it runs perfectly. It opens the file, gets the < cr > character from stdin, writes that to the first line which wipes out the first line, then tries to read the first line which is empty so there is no output. Good job 8).
You're just having trouble understanding your own expectations, your code works fine, so just think a little bit more about what you think it's supposed to do.
You'll also need to examine the contents of your text file before and after running the code, and try entering some text in the text file before you start the program, to see what happens to the text.
The getline where you try to read from the console is the issue, that returns without asking for input so it ends up giving you a blank line to write to the file.
From this link:
http://www.cplusplus.com/forum/beginner/39549/
There's a similar question there, and the first of the comments mentions this about the behavior of getline:
std::cin leaves the newline character in the buffer after pressing enter, and getline just grabs it and keeps going, that's why getline doesn't block to wait for input. .......
That's why it's not "doing anything" - it's not asking you for input, so the program has nothing to output and it looks like it's not working.
(this, by the way, is why programmers are snarky - we have to deal with stupid little behavioral issues of machines and we forget that PEOPLE are not normally like this and that PEOPLE hate being held to these kinds of standards)
I put a second getline in your code right after hte first one and now it asks for and outputs a string that I type in and sticks it in the file.
To run the program in gdb, do the following:
g++ -g < your cpp file > -o < whatever you want the binary to be called >
like this:
g++ -g myfile.cpp -o myrunnableprogram
this creates a binary with symbols that you can open in gdb
then
gdb myrunnableprogram
then in gdb
start
next
next
next
....
these commands will start the program and pause it at the first breakable spot, then each time you type next you will step to the next command and you can poke around at variables to see what's going on.
read up on gdb, it's really useful.
This question already has answers here:
GDB: How to remove a variable from the auto display
(2 answers)
Closed 5 years ago.
I am working on a c++ program on linux ubuntu 16.04 and I've done a tutorial on gnu debugger.
I am having some problems with my code and as I step through it it's easier for me to compare two variables at each part fullPath and argv[1]
But once I get past that particular segment I want to remove argv[1].
I called them with the following:
display argv[1]
display fullPath
But when I try to remove argv[1] with undisplay argv[1] I get an error that reads the following:
warning: bad display number at or near 'argv[1]'
It still continues to display argv[1] unless I exit debugger and start it again without displaying it. Is there a way to fix this?
NOTE
I've also tried delete argv[1] which also doesn't work.
The undisplay command is expecting a list number, not an expression. You can see the list numbers for all your auto-display expressions by typing:
info display
Let's say that argv[1] is assigned item 3 in that list. You would then remove it with:
undisplay 3
This question already has an answer here:
how to run a batch file using c++?
(1 answer)
Closed 8 years ago.
I want to run an .exe file from my c++ program.
but I also want to pipe an input file and take output of that file into another file.
I know that this can be done from command line as:
c:> my_program.exe <"input.txt"> "output.txt"
with this command, my_program takes all standard input from input.txt and gives standard output to output.txt
Now I want this should happen from my C++ program.
my my_program.exe is in D: drive. also input.txt is in D: drive.
Please tell me how can I accomplish my goal.
You need to handle input and output pipes inside your c++ program, and read/write data to files accordingly. See MSDN for example.
The question was basically how to redirect stdin and stdout from the inside of C++, which as been answered here.
Just change your directory to D:
cd D:\
D:>my_program.exe <"input.txt">"output.txt"
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.