0% found this document useful (0 votes)
9 views

2. Writing My First

This document explains the fundamental steps for writing a basic C++ program, emphasizing the three main elements: input, processing, and output. It provides examples, including a flowchart and pseudocode for a program that adds two numbers, followed by the actual C++ code implementation. Additionally, it covers how to read a user's name and display a welcome message, addressing the limitation of reading multi-word names.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

2. Writing My First

This document explains the fundamental steps for writing a basic C++ program, emphasizing the three main elements: input, processing, and output. It provides examples, including a flowchart and pseudocode for a program that adds two numbers, followed by the actual C++ code implementation. Additionally, it covers how to read a user's name and display a welcome message, addressing the limitation of reading multi-word names.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

In the previous video we have already learned the skeleton of a C++ program.

Now here in this video we learn how to write a program.

So we learn the steps for writing basic programs so we will be writing a lot of programs while
following these

steps.

So what are the steps.

Let us learn.

See every program will have three elements.

One is it will take some input.

Then it will process them.

It will give the output. so what is this.

See suppose

I am a program.

I know how to add two numbers and you are a user of a program.

Imagine this right.

Always you have to keep the scenario in your mind.

Now you want to use the program.

You want me to add two numbers.

Then I should know two numbers.

First of all you give two numbers.

Then I will add them then I will give you the output.

That's it.

So from my side if you'll see I'm taking input then processing and giving the output.

Now let us take another example.

Suppose I'm a program for finding the area of a rectangle and you're the user, you want me to find the
area

for that

I should know length and breadth.

So let us see how I should work.

First of all I should ask you what is the length and what is a breadth.
You will give me length and breadth.

I would take and multiply them or

Whatever the formula is, I will process and I will get the result.

Then I will give you the output, area is so and so.

So you are a user.

I am the program so now when you are thinking of writing a program.

Keep yourself at a place of program and other people who are going to use their users.

So start on the side of a program and imagine.

So now let us see how we can follow these steps for writing a simple program for adding two
numbers.

So first of all I will draw a flowchart for this one then I will take our algorithm using pseudo code.

Then I will write down a c++ program for adding two numbers.

So let us draw a flow chart.

So here I will draw a flowchart for adding two number.

So what are the things we have to do.

Let us learn see first of on a starting point of a flow chart that is start.

Then our program should take 2 numbers as input.

So for input and output values we use parallelogram.

So let us draw on a parallelogram.

So the next thing is take input.

So for that is better giving a message.

So printing a message.

I will say print Enter two numbers like when i give the example.

I have told you that first of all I should take two numbers from you.

So enter two numbers.

I should tell you.

Enter two numbers.

Then you will give me two numbers so I should read two number see.

I'm a program, right? so, read, i will read these numbers, two numbers whatever no. you have given.
I will give names to them.

So these are called as variables so I will take them in a and b.

Now first number is in a, second number you have given me is in B.

Now I have two numbers I can add them so I can process so for process rectangle is used.

so the next step is process.

So a rectangle is used.

So I should add a and b and the result I should store it in some other variables.

so result is in 'c' so it mean for any program for taking input you have to write this and process

you have to write this.then,

Now I have the result ready, I should give the result to you say that some of two numbers is so and so
that

is output so for output again the parallelogram, so draw a parallelogram and give a output here.

So here I will see print addition is (in quotes).

This is in quotes because it's a message then comma C so it will be saying "addition is" then the
result

whatever the result is that is there in 'c'. then, next this end of this procedure.

So this is stop.

So for that oval is the shape that we use or ellipses the shape we use so start, and take input, process,
give

output, and end.

This is how the flow chart for adding two numbers. now same thing

I will write it as a algorithm using pseudocode.

So what is pseudocode.

The same thing I write on, this is pseudocode.

It's not a C language code. so the same pseudo code I will write down.

So let us write an algorithm for adding two numbers.

Now here I will say begin the algorithm begin see already I have explained you that you can write.

Begin or you can write on the flower bracket.

That is your choice.

So I am writing begin here.


Now what is the first thing it has to do.

It should print and say enter two numbers

Okay then this is the message I'm giving then writing semicolon or not.

That's a choice because this you pseudo code

It's not strict.

Then I should read two numbers in two variables.

That is a and b.

This is over.

Next step is this one processing.

So C is assigned with a plus b.

So I'm writing this as assignment.

Don't take it as equal to otherwise write arrow like this next step is printing additional of number is so-
and-so

so print "addition is"

c, so this is the result.

And this the end of the algorithm.

So those are steps I have completed them and written them as algorithm.

This is the beginning.

Everything looks simple right as we go on writing more program.

So this may be becoming little Lengthy or complex so slowly we'll move to it.

Now.

Next I have to write a C++ program for adding 2 no.

So here I will write don't c++ program so let as write.

so, First of all I will write down the skeleton of C++ program.

So for that I have to write #include iostream right.

So this is the thing that we have already learned.

So this iostream then I have to write using namespace.

Right.

So whenever I'm using cin or cout this would be useful.


std So using namespace.

No main function, int main and starting off a main function.

Now what I have to do so here I have a flowchart here.

I have a algorithm these two things I have to convert them.

So let us read it.

So what are the things that I have to do.

So from there one of them will convert it into a C++ program.

see begin, inside this first is print.

Enter two numbers how to print anything on the monitor.

cout we know that so c out enter two numbers.

So here I will write down C out enter two numbers, this will print message on the screen.

The next thing read a,b.

So from where? A program will read some numbers or data from the keyboard.

So for that we have object called C in and that is present in this std.

The C out and cin present in this study.

So C in right.

So C in where? see for this I have to use a double greater than symbol for reading anything.

Now when you have to read it in variables A as well as B don't give comma again give this a greater

than symbols and write this variable B so cin a, b.

Now the important thing seeing C++ you cannot directly use the variables unless you have declared
them

so we should declare them.

So declaring means before using them here I should declare.

So total how many variables I have.

ABC three variable are there, a b c three variables and all these variables will have a data type.

Now what does it mean by data type.

We have it the next video right.

So we will be learning it.

So we have to mention the data type means what type of value you are storing in them.
We had a integer type.

So numbers only we are storing.

So that's why this is integer so this is a method of declaring them.

see This declaration was not there in algorithm.

So it's not strict it's not stick because that is not C++.

So you can write as you like.

This is English like, now here,

You have to write everything perfectly.

If you miss anything compiler will not compile your program, so you should write on the things that
are

understandable by compiler right.

So write everything properly.

So this should be terminated by semicolon.

So till here we have each other next step.

This is over.

This is over.

And this one.

C assign a plus b, so a plus b I want to do okay additions will be done then it should be stored.

So it should be stored inC so I write down like this C equal to A plus B but don't read it as equal to

read it as assign.

This means the same thing.

Add this two, result is stored in C. that's it.

Now the last thing remaining we have finished this one.

we Have to print the output.

So say c out what is the message we want to print addition is then c.

I will write down hear addition is in quotes.

And then right again the double less than and then say C..

Right.

So that's all.
And so here inside the main function we must write down return zero.

That is always you have to write because we have written int here.

So that's all.

This is a program in C++ for adding two numbers.

Now i will continue and I will show you how to read a name in C++ and give a message.

Its name means a string, a string, set of characters.

I will show you, it is a simple program.

Let us learn it.

Now let us write a program or take the name of a user and display a welcome message with the name
right.

So let us assume that I am a program for what I should do I should ask you may I know your name.

Then you will tell the name then I should take the name then I should give a message.

I should tell you that Hello Mr or Miss whatever it is underneath.

So let us write on this.

So for this I have a skeleton already available here.

Now let us write on the steps so I'm not showing flowchart all these things.

This is a simple one, so first thing I have to give a message to you.

So what does that

'may I know your name' so for displaying a message on the screen.

I should use cout may i know your name.

So this will appear on the screen.

Now you are the user, I'm the program.

When you see this you trying to write your name from the keyboard.

Right.

So I know you will hit enter so that I should read.

So I should read it by using C in right.

This extraction operator, and here I should read your name.

So what is this.

This is a variable name is a variable which you type it should read a word that is name so it should
be string type.

So in C++ you should declare the variables before using them.

Right.

So I should declare it.

So which type I should declare it as a string string and variable name is name.

So what is it is a string.

Does the data type.

We are going to learn about the data types afterwards.

So this is the name of a variable.

So it will read the name from the keyboard.

Next I should give a message.

Welcome Mr. So-and-so here I should give a message so far that I should write C out.

Welcome Mr. or Miss.

I'm followed by this.

Your name.

So for this again write down. see this is enclosed in double quotes.

now the variable name should not be in double quotes it should be directly given.

So it will display a message welcome Mr. or Miss whatever it is then name.

So this will give a message that all end of the program.

So this is a simple program for taking name of a user and showing off welcome message.

Now one problem in this one.

If your name is having more than one word then it can not read all the words.

It will be just one word.

It will not read second or third word.

Suppose your name is Ravi Kumar so it will just read Ravi right.

So if your name is Anil Kumar.

Then it will just read Anil and then how to make it read everything.

So instead of seeing we have a function called getline inside this I should write cin variable
name.

This name.

This will read all the words until you hit enter.

So this only perfect for taking the names of people that may be off more than one word.

So this is better.

So that's all in the next video.

We will have the demo of these programs.

You might also like