What is the output of this program if the string bar is supplied as input? #include <iostream> #include <string> using namespace std; int main() { string s1 = "foo"; string s2; getline(cin,s2); cout << s2.append(s1); return( 0 ); } bar foo foobar barfoo
Question
What is the output of this program if the string bar is supplied as input?
#include <iostream> #include <string> using namespace std; int main() { string s1 = "foo"; string s2; getline(cin,s2); cout << s2.append(s1); return( 0 ); }
bar
foo
foobar
barfoo
Solution
The output of the program will be "barfoo". Here's the step by step explanation:
-
The program starts by defining two strings,
s1ands2.s1is initialized with the value "foo", whiles2is left empty. -
The
getline(cin,s2)function is used to get a line of input from the user. In this case, the user inputs "bar". -
The
append()function is then used to append the value ofs1("foo") to the end ofs2("bar"). This results in "barfoo". -
Finally, the program outputs the value of
s2, which is now "barfoo".
Similar Questions
What is the output of this program if the string bar is supplied as input? #include <iostream> #include <string> using namespace std; int main() { string s1 = "foo"; string s2; getline(cin,s2); cout << s2.append(s1); return( 0 ); } bar foo foobar barfoo
What will be the output of the following program?
What is the output of the following code?string str = "Hello";cout << str.length();4560
What is the output of this C++ program? #include <iostream> using namespace std; int main( ) { char line[100]; cin.getline( line, 100, 't' ); cout << line; return 0; } t 100 None of the mentioned It will print what we enter till character t is encountered in the input data
What is the first output on the screen after the following code is compiled and its executable run?1 #include <iostream>2 #include <string>3 #include <sstream>4 using namespace std;5 int main (){6 string str;7 float price = 0;8 int quantity = 0;9 cout << "Enter price: ";10 getline( cin, str );11 stringstream( str ) >> price;12 cout << "Enter quantity: ";13 getline( cin, str );14 stringstream( str ) >> quantity;15 cout << price * quantity << endl;16 return 0;17 }Select one:a.Enter:b.Enter price:c.Enter quantity:d.Enter >>price
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.