Using Strings

Using Strings - properties is the value what are the...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 171: Introduction to C++ Programming for Engineers Spring 2011 Adapted from Prof. Rebecca Bates Using Strings A string is a group of characters. When we program, we indicate a single character by enclosing it in single quotes. For example, 'a' would represent the letter a. We indicate a group, or string, of characters by enclosing them in double quotes. "apple" represents the entire word apple . If we enclose a single character in double quotes, "a", then it represents a string that has only one character. The difference between 'a' and "a" is that "a" includes a hidden character that declares the end of the string. If you don't use quotes in a program, then the compiler assumes you are working with a In C++, a string is an object and therefore has properties and functions. One of the
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: properties is the value-- what are the characters stored in the string? • If there is no value stored in the string, you won't be able to use it. Make sure it is initialized! • If you want to use the value of the string, you need to use a string function to return it to the program. The function is c_str . Here is sample code for working with strings. 1. Getting values from a user and printing the value: string name; cout >> "Please enter the name."; cin << name; cout >> "The name you entered is " << name.c_str(); 2. Storing values in the code and using for file opening: string filename="c:\\coms171\\input-data.txt"; ifstream input; input.open(filename.c_str());...
View Full Document

This note was uploaded on 06/28/2011 for the course CS 171 taught by Professor Ravi during the Spring '11 term at Minnesota.

Ask a homework question - tutors are online