newb question

M

Mitch

when using CIN
How do i make it so user input can be 5 or 6 letters
and
how do i put input into an if else statement ?
Example, i want the user to be able to input a word then program to decide
what word to spit out.

char input;
cout<<"Hi ";
cin>>input
if (input= = "Hello" ){
cout<<"Hello\n";
}
else{
cout<<"goodbye\n";
}
?
TIA
 
N

Nicolas Pavlidis

Mitch said:
when using CIN
How do i make it so user input can be 5 or 6 letters
and
how do i put input into an if else statement ?
Example, i want the user to be able to input a word then program to decide
what word to spit out.

The IMHO best way to do this is to use the getline function defined in
<string> and teh strings themselves, consider the following code (untested)

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;

using std::string;
using std::getline;

int main(int argc, char *argv[])
{
string input;

cout << "Hi " << endl;
getline(cin, input);
if(input == "Hello"
cout << "Hello" << endl;
else
cout << "Bye!" << endl;
return(0);
}

HTH && Kind regards,
Nicolas
 
M

Mitch

thank you so much
I apreciate it..

Nicolas Pavlidis said:
Mitch said:
when using CIN
How do i make it so user input can be 5 or 6 letters
and
how do i put input into an if else statement ?
Example, i want the user to be able to input a word then program to decide
what word to spit out.

The IMHO best way to do this is to use the getline function defined in
<string> and teh strings themselves, consider the following code (untested)

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;

using std::string;
using std::getline;

int main(int argc, char *argv[])
{
string input;

cout << "Hi " << endl;
getline(cin, input);
if(input == "Hello"
cout << "Hello" << endl;
else
cout << "Bye!" << endl;
return(0);
}

HTH && Kind regards,
Nicolas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top