Giving input in sublime gdb on ST3? - c++

I am using sublime text 3 with the plugin 'sublimeGdb' and trying to give input to it via sublimegdb input box that appears below on pressing the "shift
+ f5" to a program whilst a debugging session .
int main(int argv, char **argc){
int a;
cin >> a;
cout << a;
cout << "hello";
return 0;
}
Lets say i was running the above program with sublimeGdb and put via breakpoint on the line cout << a;.
Now the gdb session waits for the input of a .When the try to give input by entering in the gdb input text box ,say I entered "1".it then appends some value to the front of the value entered and assign int a = 121;. Also I tried giving input arguments in the project settings with the line "sublimegdb_arguments": "89". didn't work out .
How to properly give input values ?
How to see the stdout of the
program?

Related

Why if I input EOF from keyboard Clion don't print on Run window the output of the program?

I just start to using Clion as my IDE and I'm struggling with EOF inputed from keyboard.
If I execute this simple c++ code
#include <iostream>
int main(){
int sum = 0, value = 0;
//read until EOF, calculating a running total of all values read
while(std::cin >> value){
sum += value;
}
std::cout << "Sum is " << sum << std::endl;
return 0;
}
In the Run window of Clion, after I input from keyboard
1 2 cmd+d I get this
1 2 ^D
Process finished with exit code 0
In MacOS the EOF(with keyboard) is Cmd+D.
In CLion is in settable in:
File -> Preferences -> KeyMap -> search EOF and set Cmd+D.
However, I suggest you to use in your code a Terminating Character such as zero that in this case doesn't change the result.
Example of execution in CLion
This is some known issue with CLion on Windows. Disabling the run.processes.with.pty in Registry (open via Find Action) usually helps.
In some environments the EOF emulation is performed using Ctrl+Z combination not Ctrl+D
To avoid such problems - create file with contents you want to enter "1 2"
and run your program from terminal redirecting the standard input to read from file. In this case the EOF event will always happen at the end of the input.
$./test < myinputfile.txt

Holding the console-screen when end-of-file is involved

Here's the scaled down version of the program which accepts an unknown no. of Integer inputs. I used cin.get() before but to no avail, finally used this but unfortunately it too didn't worked. I am using Notepad++ spawning command prompt to run my programs. Is this something to do with Notepad++ OR the CTRL-Z (end-of-file) character?
EDIT : Works fine using cmd.exe
vector<int> vint;
int val = 0;
cout << "Enter integers..... Press CTRL and \'Z\' when done entering!"
<< "\n GO... : ";
while(cin >> val)
vint.push_back(val);
if (vint.size() > 1)
{
...
}
else
{
...
}
std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n' );
std::cin.get();
When you enter Ctrl+Z in a console programme you tell that it's the end of the file. Any subsequent reading from cin is then doomed to fail.
It works from the command line, because the command processor doesn't close the window when the programme is over.
Possible solutions:
The portable approach would be to interupt the loop cleanly by checking for a special value (for example 0).
If this is not possible, another approach would be to gain more control on the user input and read lines into a string. You could then end the loop when an empty line is entered. This is I think for the user the most intuitive approach. All you have to do is to parse non empty strings with stringstreams (and eventually complain if non numeric values were entered).
An less perfect approach could be to instruct the user to enter some non numeric value to end the loop. You then have to clear the failure that invalid input would generate:
while (std::cin >> val ) {
...
}
if (std::cin.eof()) // display the special case
std::cout <<"End of file encountered !" << std::endl;
std::cout << "Press a key...";
std::cin.clear(); // clear the error state of cin
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
Surprisingly, this works compiled with MSVC2015 on windows when entering Ctr+Z: once the end of file state cleared the console is magically restored and you can continue to read. However you can't assume this to work with console front-ends like Notepad++, nor with other implementations of the standard library, nor on other OS.

Stop new line for C++ cin

In C++, iostream automatically puts in a new line after cin. Is there a way to get rid of this?
I want use iomanip to format information into a table, like so:
cin cout
0.078125 3DA00000
-8.75 C10C0000
23.5 41BC0000
(random numbers)
example code:
#include <iostream>
using namespace std;
int main()
{
int num;
cin >> num; //now a new line.
cout << num << endl;
return 0;
}
You presumably pressed the return key to send your input from the command line to your program's standard input. That's where the newline is coming from. You can't read your number from cin before this newline appears in the console, because the newline is what causes the console to hand your input over to the program in the first place. You could, as a user, configure your console (or whatever is running your program) to act differently, but there's no way for the program itself to force such behavior.
If you really want to have your input and your output on the same line, you need to find a way to "write to the previous line". How that works depends on your console (see also How to rollback lines from cout?). There is no standard way to do this because cin and cout are in no way obligated to be attached to a console or anything resembling one, so it is not clear that "writing to the previous line" even means anything.
'endl' makes a new line just don't use it.
cout << num;

C++ console program closing before completion.

My Program is a simple sum finder. the code of which I will post at the end.
But it asks for the first number. Upon entering it asks you for a second number.
After entering the second number, the console window closes before showing the results. When I first build and tested in Visual C++ 2010 it ran fine, but this problem only occurs when running the .exe from the build location. Any tips?
Here is the code If testing yourself please re-assemble:
#include "stdafx.h" // for Visual Studio users
#include <iostream>
int main()
{
using namespace std;
int no1, no2, sum ;
cout << "\nEnter the first number = " ;
cin >> no1 ;
cout << "\nEnter the Second number = " ;
cin >> no2 ;
sum = no1 + no2 ;
cout << "\nThe sum of "<< no1 <<" and "<< no2 <<" = "<< sum ;
return 0 ;
}
One way to keep the console open until you're satisfied with it is to add a cin at the end of it - Before closing it will then wait for the user to close it or to enter a line of input.
That's because the window closes when the program is finished running. Use std::cin.get() to keep the window open while it waits for input:
int main()
{
// ...
std::cin.get(); // keep the window open; wait for a character
return 0;
}
Add cin.get() before return 0;
Console applications are actually meant to be executed directly from the console. If you run them directly on it, after the program is done you'll be left with your console window, containing all of the output given by your program. Plus, you'll get a little more used to using the command prompt, which is pretty cool and useful sometimes. :-D
If you have any doubts about how to run a program from the console on a windows environment, take a look at this answer (Compiling C-code from the Command Prompt in Windows?) or maybe this one (How to run a c program using command prompt).

is there a way to redirect the input and output to the same file?

I have a C++ program that outputs prompts and takes user input via the standard input stream cin.
I want to get a full transcript including both the program's output and the input in a file.
I know I can redirect input/output with command-line redirection (i.e. ./program < in.txt > out.txt), but this will only fill out.txt with the program's output in response to the input from in.txt.
I want to have a transcript that shows both the input and output. That is, let's say my program outputs a prompt "\nEnter a number: ", takes a user inputted number and outputs its double, "\nTwice your number is: ", and keeps doing this until the user enters a 0.
Let's say I have in.txt containing:
1
3
0
Then I want to have a transcript of input/output:
Enter a number: 1
Twice your number is: 2
Enter a number: 3
Twice your number is: 6
Enter a number: 0
Twice your number is: 0
Sorry if I didn't explain this very well... I didn't really know how to word it.
Is there a way to do this simply, or do I just have to enter the input by hand... and do some save of the terminal...
script doesn't cover your exact use case. You'd like to see the input and output to your program exactly as a user would see it, but without having to do it yourself.
I found Expect, which seems to be exactly what we're looking for. I don't know Tcl, but there's a Python port, pexpect. You'll need to install pexpect:
wget http://pexpect.sourceforge.net/pexpect-2.3.tar.gz
tar xzf pexpect-2.3.tar.gz
cd pexpect-2.3
sudo python ./setup.py install
Then copy this code into an executable file:
#! /usr/bin/env python
import sys, pexpect
executable = sys.argv[1]
infile = sys.argv[2]
proc = pexpect.spawn(executable)
file = open(infile)
for line in file:
proc.send(line)
proc.sendeof()
proc.expect(pexpect.EOF)
print proc.before,
And then you can run it like so:
transcript ./executablefile fileforinput
My sample run gave me this output:
Enter a number: 1
Twice your number is: 2
Enter a number: 2
Twice your number is: 4
Enter a number: 3
Twice your number is: 6
Enter a number: 0
Twice your number is: 0
Assuming I read your question right, that should be the exact answer you're looking for. And it works on any program without any modification at all.
Hope that helps!
-Jake
The UNIX script command will do it.
Interesting question. Something which should be cross platform is like the example below (I have tested on Windows and *nix but do not have a mac to test on unfortunately). Basically, you will read the initial file and extract its data (in the case of the example, it assumes that the file is formatted exactly as you have mentioned above) and store that data somewhere. Then, write the data back to the file from which you read it.
/**
* File Input/Output
*
* Input file is also output file
*
* 12/13/10
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
// Get data and store it
ifstream in("output.txt");
// Simple error checking
if(!in.is_open())
{
cout<< "There was an error opening output.txt!" << endl;
return 0;
}
cout<< "Reading file..." << endl << endl;
// Strings are quicker to implement
string tmp;
string data;
int tmpi = 0;
// Here is where we store the input - the stringstream allows us to handle
// multiple variable types and convert them. NOTE: there is no error checking here,
// so wrong types _WILL_ throw errors (format needs to be one number per line)
stringstream ss(stringstream::in|stringstream::out);
while(getline(in,tmp))
{
tmpi = 0; // Reset
ss.str(string());
ss << tmp;
ss >> tmpi;
tmpi *= 2;
// Reset it again so we can get the doubled value
ss.clear();
ss.str(string());
ss << tmpi;
data.append("Enter a number: "+tmp+"\r\n"+"Twice your number is: "+ss.str()+"\r\n");
}
in.close();
// Output handling
ofstream out("output.txt",ios::binary|ios::trunc); // Delete everything which was in the file?
// Simple error checking
if(!out.is_open())
{
cout<< "There was an error opening output.txt!" << endl;
return 0;
}
cout<< "Generating output..." << endl << endl;
// Write to the file
out.write(data.c_str(),data.size());
out.close();
cout<< "Done!"<< endl;
cin.get(); // Pause momentarily
return 0;
}
My original input file was:
12
2312349
324843
3249
0909
The output was:
Enter a number: 12
Twice your number is: 24
Enter a number: 2312349
Twice your number is: 4624698
Enter a number: 324843
Twice your number is: 649686
Enter a number: 3249
Twice your number is: 6498
Enter a number: 0909
Twice your number is: 1818
Hope this helps!
Good luck!
Dennis M.