getline
Բայց բնական հարց կարող է առաջանալ։ Ի՞նչ անել եթե կարիք կա ստանալու տողը մինչև վերջ նույնիսկ եթե տողի մեջ կա բացատ։ Դա հնարավոր է անել
getline
հրամանի միջոցով։#include <iostream>
#include <string>
using namespace std;
int main() {
string full_name;
cout << "Please enter your name and surname:";
getline(cin, full_name);
cout << "Your full name is: " << full_name << endl;
return 0;
}
Այս ծրագիրը արդեն կկարդա անունը լիովին անկախ նրանից թե քանի բացատ կա տողի մեջ։ Եթե մենք ներմուծենք
Daenerys Stormborn of the House Targaryen,First of Her Name, the Unburnt, Queen of the Andals and the First Men, Khaleesi of the Great Grass Sea,Breaker of Chains, and Mother of Dragons
։ Ապա ծրագիրը կհասկանա, որ սա մեկ տող է և վերջում կտպի՝Your full name is: Daenerys Stormborn of the House Targaryen,First of Her Name, the Unburnt, Queen of the Andals and the First Men, Khaleesi of the Great Grass Sea,Breaker of Chains, and Mother of Dragons
Վարժություն՝
Մուտքում ստացեք երեք տողեր (string-եր)։ Հայտարարեք
all3
փոփոխական, որին վերագրեք այդ երեքի գումարը, որոնց արանքում կլինի <=>
։ Ելքում տպեք all3
փոփոխականի արժեքը։Մուտք | Ելք |
Hi, I am line 1
and I am line 2
and I am line 3 | Hi, I am line 1 <=> and I am line 2 <=> and I am line 3 |
Constraints
Time limit: 0.2 seconds
Memory limit: 512 MB
Output limit: 1 MB