Showing posts with label getline. Show all posts
Showing posts with label getline. Show all posts

Friday, March 17, 2017

C++ Part 30

String | Getline and Cin


string s1, s2;
cin >> s1;
cin >> s2;
If the user types in
I love NavidPlus website.
s1 will receive value "I" and s2 will receive the value "love".
If you want your program to read an entire line of input into a variable of type string , you can use the function getline.
Example:
string line;
cout << "Enter a line of input:\n"; getline(cin, line); cout << line << "END OF OUTPUT\n";

I/O with string Objects
You can use the insertion operator << with cout to output string objects. You can input a string with the extraction operator >> and cin.
  • When using >> for input, the code reads in a string delimited with whitespace.
    You can use the function getline to input an entire line of text into a string object.
    EXAMPLES
    string greeting("Hello"), response, nextLine;
    cout << greeting; cin >> response;
    getline(cin, nextLine);
  • Thursday, March 16, 2017

    C++ Part 29

    C++ Strings Part II

    If you didn't read the first part, you can find it here.
    getline: may input spaces, as before.
    e.g., getline(cin,s1) would assign “Navid Plus” to s1 in the "Navid Plus" example.
    Example :

    #include <iostream>// Coded by https://NavidPlus.blogspot.com#include <string>
    #include<cstdlib>
    using namespace std;

    int main ( )
    {
        string backString;
        cout << "What ever your going to write here, I will reverse it. " <<endl;
        getline (cin,backString);
        int theIndex = backString.length()-1;
        while ( theIndex >= 0)
        {
           cout << backString [theIndex] ;
           theIndex --;
        }
        cout << '\n' ;
    }

    Digital Design Part 3

    4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...