Home | | Computer Science 12th Std | How Python is handling the errors in C++

Chapter: 12th Computer Science : Chapter 14 : Integrating Python with MySql and C++ : Importing C++ Programs In Python

How Python is handling the errors in C++

Python not only execute the successful C++ program, it also helps to display even errors if any in C++ statement during compilation.

How Python is handling the errors in C++

Python not only execute the successful C++ program, it also helps to display even errors if any in C++ statement during compilation. For example in the following C++ program an error is there. Let us see what happens when you compile through Python.


// C++ program to print the message Hello

//Now select FileNew in Notepad and type the C++ program

#include<iostream>

using namespace std;

int main()

{

std::cout<<"hello"

return 0;

}

// Save this file as hello.cpp


#Now select FileNew in Notepad and type the Python program as main.py

Program that compiles and executes a .cpp file

Python main.py -i hello

import sys, os, getopt

def main(argv):

cpp_file = ''

exe_file = ''

opts, args = getopt.getopt(argv, "i:",['ifile='])

for o, a in opts:

if o in ("-i", "--ifile"):

cpp_file = a + '.cpp'

exe_file = a + '.exe'

run(cpp_file, exe_file)

def run(cpp_file, exe_file):

print("Compiling " + cpp_file)

os.system('g++ ' + cpp_file + ' -o ' + exe_file)

print("Running " + exe_file)

print("-------------------------")

print

os.system(exe_file)

print

if __name__ =='__main__':

main(sys.argv[1:])

Output of the above program

C:\Program Files\OpenOffice 4\program>Python c:\pyprg\main.py -i c:\pyprg\hello [('-i', 'c:\\pyprg\\hello')]

Compiling c:\pyprg\hello.cpp c:\pyprg\hello.cpp: In function 'int main()': c:\pyprg\hello.cpp:6:21: error: expected ';' before 'return'

std::cout<<"hello"

^

;

return 0;

~~~~~~

Running c:\pyprg\ch14\cpp_file.exe

-------------------

'c:\pyprg\hello.exe' is not recognized as an internal or external command, operable program or batch file.

C:\Program Files\OpenOffice 4\program>

Note

In the above program Python helps to display the error in C++. The error is displayed along with its line number. The line number starts from python script.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 14 : Integrating Python with MySql and C++ : Importing C++ Programs In Python : How Python is handling the errors in C++ |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.