Basic Input/Output


  1. What is the output of this program?
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    int main ()
    {
    string str;
    float p = 0;
    int q = 0;
    cout << "Enter item price: ";
    getline (cin, str);
    stringstream(str) >> p;
    cout << "Enter item quantity: ";
    getline (cin, str);
    stringstream(str) >> q;
    cout << "Total item price: " << p * q << endl;
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    In this program, We are getting the input on runtime and manipulating the value.


  1. What is the output of this program?
    #include <iostream>
    #include <ios>
    #include <istream>
    #include <limits>
    using namespace std;
    template <typename CharT>
    void ignore_line ( basic_istream& in )
    {
    in.ignore ( numeric_limits :: max(), in.widen ( '\n' ) );
    }
    int main()
    {
    cout << "First Any Number or String: ";
    cin.get();
    cout << "Clearing cin Data.\n";
    cin.clear();
    ignore_line ( cin );
    cout << "All done Smoothly.\n";
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    In this program, We are getting the input and clearing all the values.



  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    int main( )
    {
    char line[50];
    cin.getline( line, 50, 'p' );
    cout << line;
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    The program will store all strings entered and will print them only when the character ‘p’ is encountered.


  1. How many parameters are there in getline function?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    There are two or three parameters in getline() function. They are a pointer to an array of characters and maximum number of characters and an optional delimiter.



  1. What can be used to input a string with blank space?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    If a user wants to input a sentence with blank spaces, then he may use the function getline.