Home Classroom C++ C++ with Barney! Class 3

C++ with Barney! Class 3

1133

Getting user input and conditional statements

[You're Ted and you decide to learn some C++ from your friend Barney. This is the third day of the class]

You: Hey, Barney! I’m here again!

Barney: Hi, Ted! So? Ready to proceed with C++?

You: Oh, yes. What shall we be doing today?

Barney: Today we’ll go to a more practical stage where you let the user to enter some input so you can process it. This is actually quite easy. To give an output to the console we used the command cout. In the same way to get an input from the console we use the command ‘cin’.

You: Hmm, sounds easy.

Barney: Yes. Let’s write a small programme to demonstrate this. Look at this programme:
#include<iostream>

 

using namespace std;

 

int main()

{

int age;

 

cout<<”Enter your age: “;

cin>>age;

 

cout<<”You’re “<<age<<” years old”<<endl;

system(“pause”);

return 0;

}

 

You: That seems greek to me. What does this do?

Barney: It’s like this. You need to get the age from the user and output it. Now this input we get from the user should be stored in a variable, and obviously we should choose int.

You: So we first declare this variable called age.

Barney: Yes. Then we need to get the input. And before that we must ask the user to enter it. So we use cout to do that. Then, to get the input we use cin. Together with cin we must use the operator >>.

You: With cout this is the other way around, right?

Barney: Yeah, that’s the difference. And right after that we mention the variable where the user’s input should be stored. Get it?

You: Yep, and I think I understand the rest. You use cout again to output that age again.

Barney: That’s it. So let’s run the programme and see.

You: That’s interesting!

Barney: Yeah, by being able to take input from the user you are able to all sorts of useful things in your programme. Can you now write a programme to ask the user to input two integers and output their sum?

You: Mmm, well, yes, that won’t be too difficult, will it?

Barney: Of course not. Go ahead!

You: Here we go:
#include<iostream>

 

using namespace std;

 

int main()

{

int a,b,c;

cout<<”Enter a number: “;

cin>>a;

cout<<”Enter another number: “;

cin>>b;

 

c = a + b;

cout<<”The sum is “<<c<<endl;

system(“pause”);

return 0;

}

 

Note that I have included the string header file so that you’ll be able to play with strings.

You: Right. So here we go!

Wow!

 

Barney: That’s great! You’re a real fast-learner.

 

You: Ha ha! Thanks. Can one input a string?

 

Barney: Of course, but then you will have to use the string data type. Let’s try that too:

 

#include<iostream>

#include<string>

 

using namespace std;

 

int main()

{

string name;

int age;

cout<<”Enter your name: “;

cin>>name;

cout<<”Enter your age: “;

cin>>age;

 

cout<<”You’re “<<name<<” and you’re “<<age<<” years old”<<endl;

system(“pause”);

return 0;

}

 

Note that I have included the string header file so that you’ll be able to play with strings.

 

You: Right. So here we go!

 

Hmm, that’s awesome!

Barney: Wait. Run this programme again and enter your full name and see.

You: Why? Okay.

Oh, what happened?

Barney: This is because once you press space, cin stops reading from the console. It receives the next thing you type as the input for the next variable. And that’s why it didn’t ask for your age.

You: Oh, that’s bad. What can we do about it?

Barney: Simple. If you need to get a string of words, you use the getline method, like this:

cout<<”Enter your name: “;

getline(cin,name);

Try that now, again.

You: Hmm, that works fine.

Barney: Yes. And there are times when you need more control over the strings your user inputs. Suppose you have a string which contains two integer values and you need to extract them. This is when string stream comes to the rescue.

cout<<”Enter your age and her age separated by spaces: “;

getline(cin, ages);

 

You: That’s an odd way to ask for two integers.

Barney: Mmm, yes, it may seem so, but when you go ahead programming you will understand that at most times you don’t get your input the way you want to.

You: He he, okay. So what can we do now?

Barney: Now that we need to extract the two integers from ages we go ahead and use the string stream. Make sure that you need to include the sstream header file to do this.

#include<iostream>

#include<string>

#include<sstream>

 

using namespace std;

 

int main()

{

string ages;

int yourAge, herAge;

cout<<”Enter your age and her age separated by spaces: “;

getline(cin, ages);

stringstream(ages)>>yourAge>>herAge;

 

cout<<”You have an age difference of “<<(yourAge – herAge)<<endl;

system(“pause”);

return 0;

}

You: That’s a bit complex.

Barney: Actually it’s not that complex. String streams make your life easier. You will practice using it on the go. It also can be used to convert integers to strings. We’ll talk about those when we’re dealing with strings.

You: Okay.

Barney: You tired?

You: Oh, not at all!

Barney: Okay, let’s now see how we can get more control over our programmes by deciding what to do. You have to take certain decisions according to the input. Suppose you ask the user to input his age and decide whether he is old or young. For this, you can use the if statements.

if (age>30)

cout<<”You’re too old”;

 

You: That’s pretty straight forward.

Barney: Yes, quite intuitive. You can extend it like this:

 

if (age>30)

cout<<”You’re too old”;

else

cout<<”You’re a young soul!”;

You can easily see what this code does, can’t you?

You: Yes, it seems simple.

Barney: Now if you need to put more than one line of code in an if-else statement, you have to use the curly braces.

 

 

if (age>30) {

cout<<”You’re too old”;

cout<<”But start learning now and you’ll never regret!”;

}

else

cout<<”You’re a young soul!”;

 

And you can use the else if structure to get the most out of the if statements.

 

if (age>30) {

cout<<”You’re too old”;

cout<<”But still too young to start learning!”;

}

else if (age==30)

cout<<”That’s a wonderful age to be!”;

else

cout<<”You’re a young soul!”;

Right?

You: Yeah, cool! But why did you use two equal signs there?

Barney: Yes, that’s because we don’t want to make age 30, but to check whether age is 30. If you use only one equal sign, you’re telling the compiler to equate them. But here you’re just comparing them.

You: Okay.

Barney: Alright. Now let’s use what we have learned and write a simple programme which prompts the user to enter his marks and calculates the grade.

#include<iostream>

 

using namespace std;

 

int main()

{

int marks;

cout<<”Enter your marks: “;

cin>>marks;

 

if (marks>100 || marks<0)

cout<<”Invalid value”;

else if (marks>=50)

cout<<”Congrats! You’re pass!”;

else

cout<<”Sorry, buddy, you’re fail!”;

 

cout<<endl;

system(“pause”);>

return 0;

}

You: Oh, what are these vertical lines for?

Barney: Hmm, I forgot to mention, this is the OR sign. These two vertical lines are called pipes. This will set to true if at least one of the conditions is true. Here, this will be true if marks exceeds 100 or is below 100. Right?

You: Yeah.

Barney: And also you can use && for the AND operation. This is set to true when both the conditions are true. For example, you could have typed:

if (marks<=100 && marks>=50)

cout<<”Congrats! You’re pass!”
with the same effect.

You: I get it.

Barney: Okay, so let’s test the programme.

You: Hmm, works as expected, isn’t it?

Barney: Exactly. You understand it now, don’t you?

You: Completely!

Barney: There is another important method of conditional statements called switch-case structure. We’ll deal with that the next day. Until then you can practice what we learned up to now. Try to make a more serious programme that calculates grade based on marks. You can allocate grades like A, B, C, etc for certain ranges.

You: Yes, will do that today! This is really interesting! Thanks, Barney!

Barney: Oh, no problem, chap!

You: Bye, Barney!

Barney: Bye, Ted!

Comments

comments

Engineer. Loves tic-tac.

NO COMMENTS

Leave a Reply