How do i make it that the user can input a phrase instead of a number
Sep 9, 2011 at 12:51am UTC
say i want to make a program that needs a phrase and i dont want them to put the number 1 i want them to use the phrase yes or no how can i do this please give me an example.
Sep 9, 2011 at 12:53am UTC
Try reading up on std::getline() and std::strings.
Sep 9, 2011 at 1:03am UTC
Hello, learningtocode14.
A program such as the one you described doesn't have to be overly complex. Something like this may serve you well:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <string>
#include <iostream>
int main( )
{
std::string In_Buffer;
std::getline( std::cin, In_Buffer, '\n' );
if ( In_Buffer.compare( "Yes" ) == 0 )
{
// The strings match.
}
else if ( In_Buffer.compare( "No" ) == 0 )
{
// The strings match.
}
else
{
// The user entered something that was unexpected.
}
}
Sep 9, 2011 at 1:04am UTC
Thank you very much!
Topic archived. No new replies allowed.