1

Possible Duplicate:
Have you used any of the C++ interpreters (not compilers)?

Hi,

I am currently learning C++ and a beginner in programming in general. I've been trying to write some code to a few programming problems from the book I'm using. What I find is that often I make mistakes in what I write and those mistakes come up when the program is run. Its usually quite obvious where in the program I've gone wrong when there is regular output. But in a long computation I'm often not sure why a particular code has acted a certain way. I've also looked at Python recently. Python works with an interpreter, which can take any piece of Python code and compute its output.

I was wondering if there was something similar for C++. Right now when I want to check a line or block of code I have to comment out a lot, save it, compile it, and then run it from a command line. And I have to do that many times for a single error until I've solved it. Is there a way to type code into an active terminal which would run code and show me output? What would be better still would be a way to select a block of code (like you select text) or multiple blocks (to see how a function is being handled) within the IDE and click run to run just that block of code and see its output without having comment out irrelevant lines or to save the file. The compiled code could just reside in memory.

6
  • 3
    Would simply using the debugger, setting breakpoints, inspecting variables, etc. be enough?
    – Uwe Keim
    Jan 9, 2011 at 21:00
  • I'd suggest spending some time learning an IDE like Emacs to automate build/debug/run cycles
    – Anycorn
    Jan 9, 2011 at 21:02
  • While, I use and like cint, I would suggest Uwe and aaa's approach to improving your development cycle rather than trying to force c++ into a RAD mode. Jan 9, 2011 at 21:04
  • What are you hoping to gain by learning C++ specifically, when you already know about a language (Python) which does what you want (provide an interpreter) as built-in functionality, and which is a hell of a lot easier to use? Jan 9, 2011 at 21:23
  • 1
    Take it from someone who's been through it all: you don't "understand what is going on behind the scenes better" simply because you are "using a hard language". The only way you will get a proper mental model is by studying (as opposed to working) on either a theoretical (computer science) level or else right up against the metal (assembly language). Jan 9, 2011 at 23:44

6 Answers 6

2

CINT is a c & C++ interpretter that accepts nearly all valid C++. Unfortunately many Linux distros do not offer it, and you'll probably have to build it from source... and that is a non-trivial task.

1
  • This looks promising, I'll try it out, thanks for the info.
    – Abid Ullah
    Jan 10, 2011 at 9:32
1

Typically a debugger is used to step through code line by line, starting at a chosen breakpoint, and keep watch of all variables/values.

Unit testing is a technique to test smaller pieces of code.

1
  • 1
    You can debug a c++ program only after successful compilation. But the OP's problem is getting to compile successfully. He wants a way to test each line of a program like you do in matlab or python.
    – yasouser
    Jan 9, 2011 at 21:13
1

A stepping debugger, as found in most IDEs will help you with this.

Here (for example) is a description of how to set the Execution point in In Visual Studio, which sounds like what you want to do.

For certain situations, the "Immediate Window" may be of use to you. It allows you to type in expressions to evaluate immediately.

Rather than just running individual lines independently, or relying on print statements to tell you the state of whatever variables you have decided to print, you can use the debugger to run to the point of interest (where you will have set a breakpoint), then you can examine the state of any in-scope variables, or even alter the normal flow of the program.

2
  • Suppose I have a large Source file, can I choose multiple lines/blocks, say I want to see only a few functions, a class and some code in main(), so I choose those lines and then can I run only those lines without having to comment out the rest?
    – Abid Ullah
    Jan 9, 2011 at 22:36
  • You can place a breakpoint before anything you don't want to run, then (in visual studio terminology) use "Set Next Statement", to move the execution point to the start of the code you want to run. However, I don't understand quite why you want to run certain lines completely independent of the rest of the program, rather than running the program as normal until you reach the point you want to examine. Jan 10, 2011 at 7:51
1

There are some solutions that try to do this - the ones I know are Ch and TextTransformer.

However, I doubt that this works very well. C++ is not at all designed to run as an interpreted language.

One of the problems is that C++ is very, very hard to parse. And this makes it very hard to provide certain types of tools that are usual for other languages. For example, I don't think there is any C++ refactoring tool that really works well.

1
  • 2
    They are few and far between, but they exist. See the duplicate candidate and several other questions linked to/from it (i.e. the Linked sidebar). Jan 9, 2011 at 21:01
1

C++ is a compiled language not like python. But there are few c/c++ interpreters out there but not sure about their features. Check these out: Ch interpreter and CINT

If you really want to learn c++ please do not use the c/c++ interpreters.

0

If you insist on using a interactive interpreter there is since a long time CINT which is the default interpreter used in the ROOT project. It got better over the years, but still has only limited capabilities when dealing with templates. Also, there is a move to replace it with a JIT compiling interpreter based on clang inside the ROOT project.

If I were you I would learn how to run compiler and an interactive debugger like suggested in some comments already.

Not the answer you're looking for? Browse other questions tagged or ask your own question.