onlinegdb tutorial introduction go to onlinegdbuaf49268.ddns.uark.edu › ~jgauch › csce2004_labs...

4
OnlineGDB Tutorial Introduction In this tutorial, we will explain how to use OnlineGBD to compile and run C++ programs. The main advantages of using this system are: The C++ compiler is free It does not require you to download and install a program on your computer The user interface is easy to use. Go to OnlineGDB Go to https://www.onlinegdb.com. You should see an interface like this: Select the C++ compiler The OnlineGDB compiler supports over twenty popular programming languages including C++, Java, and Python. To select C++, go to the “Language” pull down menu on the right side of the command bar, and select “C++”.

Upload: others

Post on 23-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

OnlineGDBTutorialIntroductionInthistutorial,wewillexplainhowtouseOnlineGBDtocompileandrunC++programs.Themainadvantagesofusingthissystemare:• TheC++compilerisfree• Itdoesnotrequireyoutodownloadandinstallaprogramonyourcomputer• Theuserinterfaceiseasytouse.GotoOnlineGDBGotohttps://www.onlinegdb.com.Youshouldseeaninterfacelikethis:

SelecttheC++compilerTheOnlineGDBcompilersupportsovertwentypopularprogramminglanguagesincludingC++,Java,andPython.ToselectC++,gotothe“Language”pulldownmenuontherightsideofthecommandbar,andselect“C++”.

EdityourC++programTheOnlineGDBwebsitehasgivenyouasampleC++program.YourfirsttaskistoreplacetheoldCuserinput/outputlibrarywiththemoremodernC++input/outputlibrary.Todothis,clickyourmouseontheeditwindow,anddeletetheline

#include<stdio.h>andreplaceitwiththefollowingtwolines.

#include<iostream>usingnamespacestd;

Next,youcandeletetheoldoutputmessage

printf(“HelloWorld”);andreplaceitwith

cout<<“HelloMom.Pleasesendmoneyforfood”<<endl;CompileandrunyourC++programsYournexttaskistocompileyourC++program.ThiswilltranslatetheC++commandsyoutypedintothe"main.cpp"fileintomachinelanguagethatcanbeexecutedontheircomputer.Todothis,clickonthegreen“Run”iconabovethetext.Youshouldseeamessagesaying“CompilingProgram”andthentheoutputshouldappearbelowtheprogram.

ExtendtheC++programBeforewecanextendthisprogram,letslookatthecodeinmoredetail.Atthetopofthefile,youcanseetheincludestatements.ThisiswherewetelltheC++compilerwhatbuilt-incommandsweplantouseintheprogram.Inthiscase,the"iostream"librarycontainsthe"cout<<"commandforprintingdatatothescreenandthe"cin>>"commandforreadingdatafromthekeyboardandstoringitinvariables.Todemonstrateprograminput,edityourprogramandaddthefollowingthreelinestoyourprogramjustbeforethe"return0;"line:

intnumber;cin>>number;cout<<number<<endl;

Thefirstlinedeclaresanintegervariablecalled"number".Thesecondlinestopstheprogramandwaitsfortheusertotypeinanintegeronthekeyboard,andthenstoresthisvalueinthevariablenumber.Thethirdlineprintsthevalueofthenumbervariableoutonthescreenfollowedbyacarriagereturn.Nowclickthe“Run”buttontocompileandrunyourmodifiedprogram.Thistimeyoushouldsee"HelloMom.Pleasesendmoneyforfood”butno“programfinished"message.Thisisbecausetheprogramiswaitingforuserinput.Nowtypeintheinteger"42"aftertheword“food”andhitenter.Yourprogramshouldnowprintthisvalueandend.Runyourprogramseveralmoretimesandtypeinsomedifferentintegervalues.Yourprogramshouldsimplyprintoutthevaluesyoutypein.Whathappensifyoutypeinsomethingotherthananinteger?

DownloadfilesfromOnlineGDBToday,youhavecreated,compiledandexecutedasimpleC++program(main.cpp)usinganonlineC++compiler.Thisisgreat,butyourfileiscurrentlybeingstoredontheirwebsite.Ifyouclosethebrowserandgobackagain,yourwillseetheoriginalC++programagain,andyourchangeswillbegone.Inordertosavethis“main.cpp”fileonyourmachine,clickonthelightblue“Download”buttononthetopright.Thiswillsave“main.cpp”inyourdownloadfoldersoyoucanuploaditintoBlackboardwithyourprogrammingprojectreports.Youcanalsocopy/pastetheC++codefromtheOnlineGDBwindowintoanotherapplicationonyourcomputer,orintoanotherwebsite.Thisisprobablytheeasiestwaytocreate,runanddebugtheprogramsontheCSCE2004ProgrammingLabwebsite.OtherOnlineGDBfeaturesAsyoucansee,thereareseveralothericonsontheOnlineGDBinterface.ThesewillletyouusesomeofthemorepowerfulfeaturesliketheGDBdebugger.Onefeaturethatisreallyhandyisthe“Beautify”icon.ThiswillgothroughyourprogramandindentallofthecodeusingstandardC++conventionssoitiseasiertoread.

Author:[email protected],2019