OnlineGDB Tutorial Introduction Go to OnlineGDBuaf49268.ddns.uark.edu › ~jgauch › csce2004_labs...

Preview:

Citation preview

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:jgauch@uark.edu,2019