Citat:
Ursprungligen postat av
bosscs2
sch struntsamma jag provar lta programmet ta input med return.
Dremot undrar jag hur man stoppar en while loop med return knappen?
cin >> i;
while(i != return-knappen){
}
Om du istllet anvnder
std::getline stoppar den som default input vid newline och tar bort newline frn strngen.
Allts r lngden p returstrngen lika med noll om du enbart trycker p enter.
Drfr kan du upprepa loopen om lngden r mer n noll.
Om det r heltal som du vill komma t kan du anvnda
stoi() p strngen.
http://cpp.sh/3cpmo
Kod:
// Example program
#include <iostream>
#include <string>
int main()
{
std::string name;
std::size_t l;
do
{
std::cout << "What is your name? ";
std::getline (std::cin, name);
l=name.length();
if (l>0)
std::cout << "Hello, " << name << "!\n";
} while (l>0);
}
Ls mer:
Citat:
std::getline (string)
C++98C++11
(1)
istream& getline (istream& is, string& str, char delim);
(2)
istream& getline (istream& is, string& str);
Get line from stream into string
Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for (2)).
The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
If the delimiter is found, it is extracted and discarded (i.e. it is not stored and the next input operation will begin after it).
Note that any content in str before the call is replaced by the newly extracted sequence.
https://www.cplusplus.com/reference/...tring/getline/