Você está na página 1de 31

Table of Contents

Strings in
By: Shannon Hart

Table of Contents

Table of Contents

Overview

Overview

Overview
Welcome to Strings in C++! C++ is an intermediate level, general purpose programming language t at evolved from t e C language! C++ allows for ob"ect#orientation as well as generic programming features suc as t ose found in C! Tip$ T is manual assumes t at %ou are familiar wit strings in C language! &f %ou are not familiar wit C strings, t is manual ma% be difficult to understand!

Why Use the C++ String Class


Strings are an indispensable and e'tremel% convenient tool in t e programming world! T e string class of C++ is a simple, safe alternative to using t e ambiguous and problematic c aracter strings found in C! (sing t e string class in C++ eliminates t e need to include t e end of line c aracter t at is necessar% in C strings! allows %ou to store an undefined number of c aracters in t e string! o &n C, w en declaring a c aracter arra% t at would represent a string, %ou were re)uired to input t e lengt of t e arra%! T is places a limit ow man% c aracters t e string can old! o T is is e'tremel% elpful w en a program re)uires t e user to input information t at will be placed into a string! T e lengt of t e string could var% greatl% in eac program e'ecution! removes t e need to d%namicall% allocate memor% for t e string! o &n C, if %ou created a c aracter pointer t at would represent a string, %ou would need to d%namicall% allocate enoug space for t e string using eit er malloc or calloc before %ou are able to store an% information in it! * string of t e string class in C++ will never re)uire t e use of eit er of t ese functions! o &f %ou wis ed to elongate t e lengt of t e string in C, %ou would ave to use t e function realloc to d%namicall% allocate additional space for t e string! *s previousl% mentioned, t e string class of C++ does not ave a limit to ow muc a string can old+ t erefore, erasing t e need to use t e realloc function entirel%!

What to Expect in this Manual

Overview

T is manual is intended to elp %ou better understand t e advantages and differences of using t e string class in C++ as opposed to t e C string met ods! T e manual is organi-ed as follows$ C apter .$ /efining Strings C apter2$ (sing Operations on Strings o &nde' Operator o Concatenation Operator o Comparison Operators C apter 3$ (sing t e String Class o Creating t e 0rogram 1eginning t e 0rogram Creating t e Strings 0rinting out Strings Comparing Strings Concatenating Strings &nde'ing Strings 2nding t e 0rogram o Troubles ooting 2rrors Semicolons (ndeclared 3ariables (ninitiali-ed 3ariables

1% t e end of t is manual, %ou will be able to declare and initiali-e members of t e string class, step t roug a string a letter at a time, combine two strings end#to#end, and alp abeticall% compare strings! 4ou will also learn about some commonl% made errors t at programmers are fre)uentl% confronted wit w en programming in C++! 2ac of t ese tools will elp %ou develop %our programming met ods, increase %our 5nowledge of t e C++ language, and e'pand %our abilities as an efficient programmer!

Defining Strings

Strings in C++

Defining Strings
7enerall% spea5ing, a string is a se)uence of c aracters! * 8c aracter9 refers to a letter, number, s%mbol, or w ite space c aracter+ t erefore, an arra% of c aracters, or a string, includes e'amples suc as 8 ello world9, 83+:;<9, and 8water is =2O9! &n C, %ou must decide w et er to declare t e string as a c aracter pointer >e'ample .? or a c aracter arra% >e'ample 2?! 2ac t%pe of declaration as its own advantages, and it ma% be difficult to decide w ic declaration ma% be more useful or easier to manage t roug out t eir program! Example 1: char *p=hello; Example 2: char array1[]=hello; char array2[6]=hello; char array3[6]={h e l l o !"#; char array$[%]={&h e l l o#; char array%[6]; array%=hello;

@or man% programmers, t is is a difficult and confusing decision to ave to ma5e! @or eac met od of declaring a string, t ere are different wa%s to andle and manipulate t e variable, w ic can cause even furt er confusion! @or instance, notice t e difference in t e declaration of array3 and array$' T e small difference in t eir declarations ma5es a uge impact+ array3 is declared to represent a string w ile array$ is actuall% "ust an arra% of c aracters!

/efining Strings

C++ recogni-es and addresses t ese common issues wit strings in C b% using t e string class to solve t ese issues! 4ou must include t e librar% (string) if %ou wis to use t e string class in %our program! C++ allows %ou to declare all strings, no matter w at t e% will be used for, in one simple wa%, rat er t an aving to c oose between two met ods >t e met od used on array$ again, does not create a string?! T usl%, strings in C++ eliminate t e confusion of ow to andle, access, and manipulate t e string in t e program! 2'ample 3 s ows ow to properl% declare a string class string$ Example 3: string string1*+hello,; string string2=hello; string string3; string3=hello;

Bust li5e ever% ot er variable declaration, %ou can declare and initiali-e string in multiple w en using t e string class! To access t e string, %ou simpl% use t e name of t e string! T is is similar to ow %ou would access a string in C if %ou ad declared it as a c aracter pointer t at pointed to t e string! On t e ot er and, if %ou ad declared t e string in C as a c aracter arra%, %ou would ave to include a brac5et after t e name of t e arra% and access eac member of t e arra%, w ic is a single c aracter, and print eac letter one at a time! 2'ample : s ows ow to properl% access a C++ string class string in order to print it out using co-t! Example $: string string1*+.ello /orl011,; co-t((string1;

Cotice t at t e string is named string1 as opposed to "ust string! T is is because t e word +string is a 5e%word t at as a predetermined value or function in t e s%stem! T e function co-t prints out t e content string1 onto t e screen, t erefore displa%ing 8.ello /orl019 on t e screen!

.D

Strings in C++

Using Operation s on Strings

(sing Operations on Strings

..

Using Operations on Strings


&n an% language, strings ave a closel% associated set of operators t at are commonl% used to alter or access t e content of t e string! T e most fundamental of t ese operators are &nde' Concatenation Comparison

n!ex Operator
T e inde' operator, also 5nown as t e subscript, allows %ou to step t roug t e string, accessing one c aracter at a time! T is is t e same met od used to access one c aracter from a C st%le string t at was declared as a c aracter arra%! T is is useful w en %ouEre tr%ing to access a particular c aracter in a string to alter, compare, or retrieve t e c aracter! 2'ample 5 s ows an e'ample of t e implementation of stepping t roug a string b% using a 2or loop and t e variable n' Example %: string string1=*+hello,; int n="; 2or*n=";n(6;n33, co-t((string1[n];

&n e'ample 5, t e string string1 as , inde'es >F E, FeE, FlE, FlE, FoE, FGDE?, w ic we can access t roug t e use of t e rectangular brac5ets t at contain a number >or in t is case, a variable, n, representing a number?! 4ou must remember t at inde'es alwa%s start at D! T erefore, string["] represents t e F E in 8 ello9, string[1] represents t e FeE in 8 ello9, and so on until t e loop reac es string[%] w ic represents t e end of string operator$ FGDE! T e variable n is incremented b% one ever% time t e 2or is e'ecuted, w ic allows t e value of n to count up to t e number 5! 7iven t at t e inde'es start at t e D, t is allows t e 2or loop to access all , members of string1!

.2

Strings in C++

(sing t e function co-t to print out t e member of string at inde' n, t e resulting output from t e 2or loop is 8hello9!

Concatenation Operator
* ver% common operation t at is used on strings is concatenation! Concatenation is t e operation of "oining two strings toget er! @or e'ample, if %ou were to concatenate t e strings 8 ello9 and 8world9, t e result would be 8 elloworld9! T is is an e'tremel% useful tool in programming >see e'ample ,?! 1asicall%, in order to concatenate strings x and y, %ou would write$ -;'+%! Example 6: string 2ile4ame; string 2ileExt; string 2ile; co-t((!n!n5lease enter the name o2 the 2ile yo- 6ish to open:; cin))2ile4ame; co-t((!n!n5lease enter the 2iles extention:; cin))2ileExt; 2ile=2ile4ame32ileExt; o2stream 2ile.an0le; 2ile.an0le'open*2ile,; 7*8nsert co0e here*7 2ile.an0le'close*,;

(sing Operations on Strings

.3

T e purpose of t e program in e'ample , is to as5 t e user for a file name and a file e'tension! T e program t en uses t e concatenation operator to "oin t e file name and e'tension toget er into a new string called 2ile w ic is t en used to open t e file t e user input' &n C++, t e concatenation function can be used wit a mi' of C++ strings, a C st%le string >a c aracter arra% or a c aracter pointer! See e'amples . and 2?, a c aracter, and a double#)uoted string! =owever, one of t e ob"ects being concatenated must be of t e C++ string class! T e resulting string will also be of t e C++ string class! 2'ample < s ows an implementation of a mi'ed concatenation! Example 9: string x=+hello; char y[]= to; char :[]={& & t h &e !"#; char *p= 6orl0; string string1; string1=x3y3:3p313 :,; co-t((string;

2'ample <Es output is +hello to the 6orl01 :,!

.:

Strings in C++

Co"parison Operators
4et anot er commonl% used function associated wit strings is comparison operators! T e , comparison operators include$ ;;, !;, H, H;,I, and I;! T ese operators respectivel% mean$ e)ual to, not e)ual to, less t an, less t an or e)ual to, greater t an, and greater t an or e)ual to! T e comparisons are alp abetical, meaning t at t e greater t an and less t an comparisons are based on w ic letter or word comes first alp abeticall%! T e outcome of eac of t e , comparisons is 1oolean in nature, meaning t e outcome could be eit er tr-e or 2alse! T ese comparisons onl% wor5 if at least one of t e items being compared is a C++ string! T e ot er item can be a C++ string, a C st%le sting, or a double#)uoted string! *n implementation of t ese comparisons is s own in e'ample 6! Example ;: string x=hello ; char y[]=to the ; char *p=6orl0; i2*x(y, co-t((x(y!n; i2*x)goo0<ye, co-t((x)goo0<ye!n; i2*+hi1=x, co-t((hi1=x!n; i2*p(=x, co-t((p(=x!n; else co-t((p)x!n;

(sing Operations on Strings

.5

2'ample 6Es output is x(y x)goo0<ye hi1=x p)x

=owever, %ou must recogni-e t at t e comparison operators ta5e t e case of t e letter into consideration! T is means t at +a and += would not e)ual eac ot er **&a===, is false?! &n t e *SC&& codes, capitali-ed letters come before lower case letters! *SC&&, t e *merican Standard Code for &nformation &nterc ange, is a c aracter coding s%stem t at gives a uni)ue code identifier to eac of t e .26 c aracters used in programming!

Using the String Class

(sing t e String Class

.<

Using the String Class


Cow t at %ou are familiar wit t e basics of string class strings in C++ and t e common operators associated wit t em, let us loo5 at a complete program t at includes eac of t e operators we covered in t is manual! T is c apter includes a full% functioning program in C++ t at will contain several uses of eac operator t at was covered in t is manual! @ollowing t e e'ample will be a section t at will e'plain ow to create eac line of code in t e program! Jastl%, t e c apter will close wit a troubles ooting section t at will cover common errors t at programmers fre)uentl% come across w en dealing wit t e C++ and t e string class! =owever, t is is section also contains man% of t e errors t at commonl% occur wit an% program, suc as simple s%nta' errors or missing c aracters! 2'ample A simpl% displa%s ow eac operator would be used in a real, functioning program! T e program includes a c aracter pointer to a string >e'ample .?, a c aracter arra% >e'ample 2?, as well as C++ string class strings >e'ample 3?! T is variet% allows t e e'ample to displa% t e proper wa% to use t e inde', concatenation, and comparison operators wit a mi'ture of string t%pes!

Creating the #rogra"


T is section of t e c apter will include t e program itself followed b% a guide of ow to create eac line of code contained in t e program! @or %our convenience, t e program will begin and end on t e ne't page!

Example >: ?incl-0e(iostream) ?incl-0e(string) -sing namespace st0; int main*, { string string1=/elcome; string string2; string2=*to,; char array[$]={&@ &3 &3 !"#; const char *p=Aoo0 l-cB1;

(sing t e String Class co-t((string1= ((string1((en0l; co-t((string2= ((string2((en0l; i2*string1)string2, co-t((string2 comes <e2ore string1 alpha<etically((en0l; else co-t((string1 comes <e2ore string2 alpha<etically((en0l; i2*string2(p, co-t((string2 comes <e2ore character pointer p alpha<etically((en0l; else co-t((@haracter pointer p comes <e2ore string2 alpha<etically((en0l; string string3 string$; string3=string13 &3string23 &3array["]3array[1]3array[2]313 &3p; string$=string13 &3string23 &3array313 &3p; i2*string3==string$, co-t((Ctring 3 an0 $ are the same111 Dhey <oth say: +((string1((en0l; co-t((Eo- haFe complete0 the man-al Ctrings in +((en0l; 2or*int i=";i(3;i33, co-t((array[i]; ret-rn "; # Ctring 1= /elcome Ctring 2= to Ctring 1 comes <e2ore Ctring 2 alpha<etically @haracter pointer p comes <e2ore Ctring 2 alpha<etically Ctring 2 an0 1 are the same111 Dhey <oth say: /elcome to @331 Aoo0 l-cB1 Eo- haFe complete0 the man-al Ctrings in @33 2'ample AEs output is

.6

(sing t e String Class

.A

T e guide to creating eac line of code will begin below! 2ac step will e'plain ow to create one line of code and w at t e line of code does! T e steps are divided into sections of code$ 1eginning t e 0rogram Creating t e Strings 0rinting out Strings Comparing Strings Concatenating Strings &nde'ing Strings 2nding t e 0rogram

$eginning the #rogra"


1efore we can do an% coding, we must first create a new file! T en we can begin wit t e actual creation of t e program! Jibraries are necessar% in programs! * librar% is basicall% a collection of functions! &ncluding libraries into %our program allows %ou to use t e functions t at t e librar% contains! 1e sure t at %ou include t e libraries at t e beginning of t e file! T e order in w ic %ou include t e libraries does not matter! .! @irst, we must create a program file to write t e code! 4ou can name t e program an%t ing %ou wis , but t e name must end wit 'cpp! @or e'ample, we will name t e program example>! To create t e file example>, t%pe Fim example>'cpp! Cow %ou ave created t e file to create t e program in! 2! &nclude t e iostream librar%! To do t is, t%pe ?incl-0e(iostream) at t e beginning of %our program! T is allows %ou to use basic input and output cin and co-t' 3! &nclude t e string librar% at t e beginning of %our program! T is librar% can go before or after t e librar% in step one! To do t is, t%pe ?incl-0e(string)! T is allows %ou to use t e string class as well as t e operators t at we covered in t is manual! :! &nclude t e line -sing namespace st0; on a new line! @or consistenc%, alwa%s place t is p rase after libraries t at ave been included in t e program! T is ma5es t e program easier to create b% s ortening t e lengt of eac function call from somet ing suc as st0::co-t((.ello /orl0; to simpl% co-t((.ello /orl0;'

Creating the Strings


&n order to e'emplif% ow to use eac operator wit t e different t%pes of strings, we must first declare and initiali-e t e strings! .! 2nter t e main function! /o t is b% t%ping int main*,{' T e open curl% brac5et represents t e beginning of t e main function! *ll of our code will be inside t is function! T e closed curl% brac5et will be located at t e end of t e program!

(sing t e String Class

2D

2! Create a string of class string b% t%ping string string1' To demonstrate one of t e met ods to declaring a string class string, we will initiali-e t e string in t e same line! T%pe =/elcome; on t e same line of code! Toget er, t is s ould loo5 li5e string string1=/elcome;' 3! Create a second string of class string b% t%ping string string2;' To demonstrate a second met od of declaring a string class string, we will end t is line of code ere! :! To initiali-e string2, t%pe string2=*+to,;' 5! Create and initiali-e a c aracter arra% t at will represent a string b% t%ping char array[$]={&@ &3 &3 &!"#;' ,! Create and initiali-e a c aracter pointer b% t%ping const char *p=Aoo0 G-cB1;'

#rinting out Strings


To see w at is in a string, we print it out using an output function! &n C++, t is function is co-t' T is section will cover ow to properl% use t e co-t function wit string class strings! .! 0rint out +string1= followed b% t e contents of string1' To do t is, t%pe co-t((string1= ((string1((en0l;' *s previousl% stated, co-t is t e function used for outputting information in C++! To ensure t at t e line is printed, t e function ends wit en0l;' T is line of code will produce t e output +string1= /elcome' 2! 0rint out +string2= + followed b% t e contents of string2! To do t is, t%pe co-t((string2= +((string2((en0;'

Co"paring Strings
&n t is section, we will create two e'amples of string comparisons! We will use i2Helse statements to displa% t e result of eac comparison! &f t e i2 condition is true, t en t e code after t e statement will be e'ecuted! &f t e i2 condition is false, t en t e code after t e else statement will be e'ecuted! T is will displa% ow t e comparison operator wor5s! .! Compare string1 and string2 using t e ) operator to determine if string1 is greater t an string2, meaning t at string1 as a greater *SC&& value t an string2! &f t e *SC&& value in string1 is greater t an t e *SC&& value of string2, t is means t at string2 comes before string1 alp abeticall% >remember, capitali-ed letters come before lower case letters, so += comes before +a?! *s e'plained earlier, we will use an i2 statement to displa% if t e comparison is true or false! T erefore t e statement will be i2*string1)string2,' Cotice t at t e statement does not end wit a semicolon! 2! Create a co-t function t at prints +string2 comes <e2ore string 1 alpha<etically if t e i2 statement in step . is true! To do t is, t%pe co-t((string2 comes <e2ore string1 alpha<etically((en0l;' 3! Create an else statement t at will e'ecute if t e i2 statement in step . is false! To do t is, simpl% t%pe else! Cotice, again, t at t e statement does not end wit a semicolon!

(sing t e String Class

2.

:! Create a co-t function t at prints +string1 comes <e2ore string2 alpha<etically if t e i2 statement is step . is false! To do t is, t%pe co-t((string1 comes <e2ore string2 alpha<etically((en0l; 5! Compare string2 and c aracter pointer p using t e ( operator to determine if string2 is less t an p, meaning t at string2 as a smaller *SC&& value t an p! We will use anot er i2 statement to displa% if t e comparison is true or false! To do t is, t%pe i2*string2(p,! ,! Create a co-t function t at prints +string2 comes <e2ore character pointer p alpha<etically if t e i2 statement in step 5 is true! T%pe co-t((string2 comes <e2ore character pointer p alpha<etically((en0l; to do t is' <! Create an else statement t at will e'ecute if t e i2 statement in step 5 is false! Simpl% t%pe else to do t is! 6! Create a co-t function t at prints +@haracter pointer p comes <e2ore string2 alpha<etically if t e i2 statement in step 5 is false! To do t is, t%pe co-t((@haracter pointer p comes <e2ore string2 alpha<etically((en0l +!

Concatenating Strings
&n t is section, we will cover ow to concatenate strings of mi'ed t%pes! We will use two met ods to s owcase t e two different was to concatenate arra%s! 2ac met od will concatenate all t e string t%pes we created at t e beginning of t is c apter! @irst, we will create two new string class strings t at we will save t e concatenated strings in! T en we will actuall% preform t e concatenations and set t em e)ual to t e newl% created strings! .! Create t e two new strings! T%pe string string3 string$;' T is creates two strings of class string at t e same time! 2! Concatenate t e strings toget er! &n t is concatenation, we will use t e inde' operator to displa% one of t e wa%s to concatenate arra%s! T is concatenation will be stored into string3! We will insert spaces between eac of t e t%pes of strings to ma5e t e concatenation readable! To do t is, t%pe string3=string13 &3string23 &3array["]3array[1]3array[2]313 &3p;' T is met od displa%s ow t e inde' operator is used+ it adds one member of t e arra% at a time to t e string! 3! Concatenate t e strings toget er again, but t is time, we will not use t e inde' operator! &nstead, we will concatenate t e w ole arra%! T is s ould be t%ped string$=string13 &3string23 &3array313 &3p;' :! To be certain t at t e string3 and string$ are t e same, create anot er comparison! We will again use an i2 statement to displa% t e outcome of t e comparison! T e operator we will use is t e == operator! Kemember, t is operator is used to see if two items are e)ual to eac ot er! To create t e comparison and if statement, t%pe i2*string3==string$,' 5! Create a co-t function t at prints +string3 an0 string$ are the same111 Dhey <oth say: + if t e i2 statement in step : is true! To do t is, t%pe co-t(( Ctring 3 an0 $ are the same111 Dhey <oth say: +((string1((en0l;'

(sing t e String Class

22

n!exing Strings
Jastl%, t is section covers ow to inde' t roug strings! *lt oug t is sub"ect was touc ed on a little in t e last section, t is section covers anot er e'ample of inde'ing! T e e'ample in t is section is a more commonl% used met od of inde'ing! * 2or loop is utili-ed to step t roug t e c aracter arra% one c aracter at a time until t e predetermined number is reac ed! .! Create a co-t function t at prints +Eo- haFe complete0 the man-al Ctrings in +' /o t is b% t%ping co-t((Eo- haFe complete0 the man-al Ctrings in +((en0l; ! 2! Create a for loop t at will increment from D to 2! 4ou must create a variable to increment in t e loop! To do t is, t%pe 2or*int i=";i(3;i33,' Cotice t at t e statement does not end wit a semicolon >;?! T is function call tells t e program to run t roug t e loop 3 times, w ic also e'ecutes t e statement t at follows to be e'ecuted 3 times! T is allows t e variable i to e)ual D, ., and 2 before t e loop is e'ited and t e program moves on! 3! Create a co-t function t at prints out t e c aracter in t e inde' of t e arra%! 4ou do t is b% placing t e variable inside t e brac5ets t at follow array! To do t is, t%pe co-t((array[i];' T is allows t e inde'es D, ., and 2 to be accessed in array!

En!ing the #rogra"


T is section of t e c apter simpl% finis es t e program! T is includes return statement and t e closing of t e curl% brac5ets as well as some tips to ma5ing sure %our program runs correctl%! *t t e end of t is section, %our program s ould be a replica of e'ample A! &f %our program loo5s different, ma5e an% c anges necessar% to ma5e %our program reflect t e program in e'ample A! .! We must add a ret-rn statement to end t e program! 1ecause our main function as a return t%pe of an int, we will ret-rn "! T is will loo5 li5e ret-rn ";! 2! We must close t e curl% brac5ets t at were opened at t e beginning of t e program w en we entered t e main function! To do t is, we simpl% t%pe # on t e last line, after all t e code we ave created! T is now officiall% ends t e program! 3! To ma5e sure t at t e program runs correctl%, e'it t e program and compile it! To e'it t e file, it t e escape 5e% and t%pe :6I and t en it enter to bot save and e'it t e program file! To compile t e file and ma5e sure t at t e code was written correctl%, t%pe g33 example>'cpp! :! &f an% errors appear after %ou complete step 3, %ou must fi' t em! * good strateg% to use w en fi'ing a program is to start from t e top of t e list of errors! T%picall%, solving t e first error can remove ot er errors t at appear later in t e list! &f %ou followed t e steps in t is c apter closel% and compared %our file to t e program in e'ample A, %ou s ould not ave an% errors after %ou compile! 5! To run t e program, t%pe '7a'o-t! 4our output s ould matc t e output listed previousl% in t is c apter! 4ou ave now officiall% created and e'ecuted a C++ program! Congratulations!

(sing t e String Class

23

Troubleshooting
T is section will cover some common errors t at programmers face w enever t e% create programs, suc as t e one in e'ample A! *lt oug t e errors t at will be covered in t is section will be specificall% aimed towards issues t at ma% occur in e'ample A, t e same errors could occur in man% ot er t%pes of programs! T%picall%, t e line and c aracter t at t e error occurs is in t e error message after t e file name! @or %our convenience, t e errors covered in t is section will be related to t e example> program we created previousl%!

Se"icolons
&n C and C++, semicolons are t%picall% placed at t e end of function calls or declarations because semicolons act as a separator of functions! Semicolons are re)uired after most lines of code, and it can be eas% to accidentall% forget one! T usl%, t is is a common error programmerEs face! Juc5il%, t e error message t at will be produced b% a missing semicolon is ver% straig t forward and even includes a int to w ere t e semicolon mig t ave been left off! *n e'ample of w at t is error would loo5 li5e is below$ example'cpp:21:2: error: expecte0 &; <e2ore &co-t *s previousl% stated, t e error message gives %ou t e line at w ic t e error occurred after t e name of t e file >example'cpp:21:2,, w ic in t is case is line 2. and c aracter 2 of t at line! To fi' t e error, open %our program and go to t e line mentioned in t e error message! T en, loo5 before t e given line a searc for a missing semicolon! T is would t%picall% be at t e end of t e function t at came before t e location of t e error message! &n t is particular e'ample, %ou s ould begin loo5ing at line 2D for t e missing semicolon!

Un!eclare! %ariables
&n an% programming language, a common error t at occurs is an undeclared variable! T is error could be t e result of 2 possibilities! .! T e variable was mist%ped w en %ou declared t e variable originall% or on t e line t at t e error message refers to! 2! T e variable %ou used on t e line t e message refers to was never declared! Co matter t e cause of t e error, t e message s ould loo5 t e same$ example'cpp:2":21: error: &strng1 6as not 0eclare0 in this scope To fi' t e error, c ec5 bot t e line t at contained in t e error message as well as t e declaration of t e variable! &f t ere is no declaration of t e variable, add it! &f t e declaration does e'ist,

(sing t e String Class

2:

ma5e sure t at t e spelling is identical in eac line! &f t ere is a t%po in one, fi' t e spelling and t e error s ould be resolved! @or instance, t e error message displa%ed above sa%s t at strng1 was not declared! &n e'ample A, t is is supposed to sa% string1' Simpl% fi' t e spelling mista5e b% adding t e i and t e error message will go awa% w en %ou recompile t e program file!

Uninitiali&e! %ariables
* common error t at can occur in programming is tr%ing to use a variable t at as not been initiali-ed! T e error occurs w en %ou tr% to access or add to w at t e variable olds! Tip$ 1eware! *n error message will not alwa%s appear!

4ou will be able to notice t e error w en functions wor5 improperl%! 3ariables in C and C++ need to be initiali-ed at w en t e% are declared! Ot erwise, t e variable could be olding old data, w ic can greatl% affect t e e'ecution of %our program! @or instance, in e'ample A, %ou must initiali-e t e i in t e 2or loop D, t en w atever data was previousl% eld in t at memor% location would be used in t e 2or loop! T is means t at int i could be olding an% value, and c ances are t at i would not be olding t e value we desire$ D!

Index

&nde'

2,

n!ex
A
*rra%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 5, .D

C
C aracter C aracters!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ,, A

Glossary

3D

Strings in C++

'lossary
A
*rra%$ * se)uence of variables of t e same t%pe! *SC&&$ t e *merican Standard Code for &nformation &nterc ange! * c aracter coding s%stem t at gives a uni)ue code identifier to eac of t e .26 c aracters used in programming!

B
1oolean$ * binar% variable t at as two possible values! &n C, t e values are eit er . or D, w ic represents true and false, respectivel%! &n C++, t e two possible values are t e strings 8 tr-e9 or 82alse9!

C
C aracter$ * letter, number, w ile space, or s%mbol! cin: * function in C++ in t e (iostream) librar% t at is used for standard input! T is is t e e)uivalent of scan2 in C, but wit different parameters! Class$ *n e'tension of t e idea of str-ct in traditional C! * class allows %ou to implement a programmer#defined data t%pe t at as its own set of associated functions and operations! Concatenate$ T e operation of "oining two strings end#to#end >t e beginning of one string to t e end of anot er?! co-t: * function in C++ in t e (iostream) librar% t at is used for standard output! T is is t e e)uivalent of print2 in C, but wit different parameters!

F
2or loop$ * function t at ta5es t ree parameters, w ic are t%picall%$ a variable to increment>t%picall% initiali-ed to D for t e loop?, a conditional statement, and an incrementLdecrement of t e variable of t e first parameter! T e loop allows code to be repeatedl% e'ecuted! @unction$ 7enerall% bloc5s of code t at preforms predefined commands! 4ou can eit er use t e built#in librar% functions or create %our own functions! T e protot%pes of t e built#in librar% functions are found in t e librar%!

I
&nde'$ *lso referred to as subscript! T e inde' refers to an element of an arra%! T e inde' alwa%s starts at D! T%picall%, t e inde' is placed in brac5ets t at follow t e name of t e arra%! &nitiali-e$ T e assignment of an initial value for a variable!

K
Me%word$ * predefined, reserved identifier t at as a special meaning to t e complier! T e% cannot be used as variables or identifiers in %our program'

7lossar%

3.

L
Jibrar%$ * group of functions and declarations t at are e'posed for use b% ot er programs!

P
0ointer$ *n address of an ob"ect in memor%! T e name of a declared arra% is itself a pointer as well >t e name of t e arra% wit no brac5ets after it?!

S
String$ *n arra% of c aracters!

V
3ariable$ Cames used to refer to some location in memor%!

Você também pode gostar