7
C++ Notes Headers #include <iostream> //for stream IO #include <algorithm> //for algorithms implemented in the library #include <time.h> //for time related functions #include <fstream> //for file IO #include <vector> //for vector functions #include <string> //for STL string functions Working with STL Strings //Unlike character arrays STL strings can hold strings of any length. int intNumberGuessed = 0; string numberGuessed; do { cout << "Guess a number between 1 and 10" << endl; getline(cin, numberGuessed); /*The getline function gets a string from the user (std input) and puts it in the string numberGuessed.*/ intNumberGuessed = stoi(numberGuessed); //stoi = string to integer } while (intNumberGuessed != 4); double eulerConstant = 0.57721; string eulerGuess; double yourGuess = 0; cout << "Guess the Euler Constant." << endl; getline(cin, eulerGuess); yourGuess = stod(eulerGuess); //convert string to double if (yourGuess == eulerConstant) cout << "You are right" << endl; else cout << "You are wrong" << endl; cout << "Size of the answer: " << eulerGuess.size() << endl; //This returns the size of the string cout << "Is your string empty: " << eulerGuess.empty() << endl; //This returns 0 if the string is not empty cout << eulerGuess.append( " was your guess" ) << endl; //This appends the literal to the string. string dog = "dog" ; //using single quotes to initialise a string generates a compiler error. string cat = "cat" ; cout << dog.compare(cat) << endl; //Compares two strings. Returns 1 since c > d. cout << cat.compare(cat) << endl; //Compares two strings. Returns 0 since c = c.

C++ Short Notes

Embed Size (px)

DESCRIPTION

C++

Citation preview

C++ NotesHeaders#include //for stream IO#include //for algorithms implemented in the library#include //for time related functions#include //for file IO#include //for vector functions#include //for STL string functionsWorking with STL Strings//Unlike character arrays STL strings can hold strings of any length.int intNumberGuessed = 0;string numberGuessed;do {cout