GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Nov-2005, 20:35
xoBeBExo xoBeBExo is offline
New Member
 
Join Date: Nov 2005
Posts: 1
xoBeBExo is on a distinguished road

Help needed on visual C++ basic math program


Hi Guys,

I am using Visual Basic C++ 6.0 edition.

I need help on creating a basic program that can:

1. Add

2. Subtract

3. Multiply

4. Division



It should take no longer than 10 minutes if you are C++ savy.



--------------------

Use float

The screen should prompt user to enter two numbers:





Enter a number:

Enter a number:



Result of Addition =

Result of Subtraction =

Result of Multiplying =

Result of Division =



xoBeBExo



where you guys from?
  #2  
Old 17-Nov-2005, 21:29
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Arrow

Re: Help needed on visual C++ basic math program


Hi xoBeBExo,

Welcome to the GIDForums™.
Did you read the Guidelines?

There are many people from many countries in this forum. You can look forward to meet them in this forum and have a enjoyable stay.
I am from India.
Where are you from?

Since you mentioned that you use Visual C++, i assume that you use C++ programming language.

Ok. Lets start thinking how to do this program.

Quote:
I need help on creating a basic program that can:

1. Add

2. Subtract

3. Multiply

4. Division

Lets start from a simple program.
I hope you know:
1. Every executable program should have a main function:
CPP / C++ / C Code:
int main()
{

return 0;
}
at the end to indicate successful execution.
2. We should include the iostream header file using :
CPP / C++ / C Code:
#include<iostream>
3. We should include the line:
CPP / C++ / C Code:
using namespace std;
to indicate that we are going to use the statements from the namespace std;(you'll know what are namespaces later..)
4. We can add comments using //. Comments help in understanding the program better.

So, here is the prototype of a typical C++ program:
CPP / C++ / C Code:
#include<iostream>     //include the iostream standard library
using namespace std;  //use the namespace std.

int main()                  //the main function. should return int.
{                             //open the braces 

    //statements go here...

    return 0;               //return 0 indicates successful execution.
}                            //close the braces 

Now, we have created a prototype.

We shall see next, how to use variables to store numbers and print them.
CPP / C++ / C Code:
#include<iostream>
using namespace std;

int main()
{
    int i;          //int represents integer. create a variable i.
    i = 5;         //store the value of 5 in i.
   
    cout << "The number is " << i ;   //print the value of i

    return 0;
}

As you see the comments, we create an integer say i and then store some value in the integer.
The we are printing by using the cout statement.
So, cout<<"The number is " will print "The number is " and "<< i;" prints the number i.
Try this program in your visual C++.

Next, we'll see how to manipulate variables. Say addition first.
CPP / C++ / C Code:
#include<iostream>
using namespace std;

int main()
{
    float num1, num2;           //two variables to store numbers
    float sum;                      //this variable stores the sum
    
    num1 = 5;                      //store some value in the numbers.
    num2 = 10;
    sum = num1 + num2;       //calculate the sum and store it in sum.
    
    cout << "The sum is " << sum ;    //print sum

    return 0;
}
Since you have to use floats, we declare three variables say num1, num2 and sum.
the store some value in the numbers and add them using the addition symbol "+" and store the sum in the variable sum.
so,
CPP / C++ / C Code:
sum = num1 + num2; 
represents we are adding num1 and num2 and then store the result in sum..

Now, how to get the user input?
We can use the cin statement..
Here is the sample program.
CPP / C++ / C Code:
#include<iostream>
using namespace std;

int main()
{
    float num1, num2;           //two variables to store numbers
    float sum;                      //this variable stores the sum
    
    cout<<"Enter the number 1 ";
    cin >> num1;                  //get the user input for num1

    cout<<"Enter the number 2 ";
    cin >> num2;                  //get the user input for num2


    sum = num1 + num2;       //calculate the sum and store it in sum.
    
    cout << "The sum is " << sum ;    //print sum

    return 0;
}
So, cin >> num1 waits for the user to enter a number and then stores the number in num1. We do similarly for num2 and then we add them and print the sum.

Here is a sample output from the program:
Code:
Enter the number 1 10 Enter the number 2 20 The sum is 30

So, we have successfully created a program that adds two numbers and print the sum.

Now, here is what you should do for subtraction, multiplication and division:
1. Create separate variables to store the result of subtraction, multiplication and division.
2. Get the input for the two numbers.
3. Do the corresponding action for subtraction, multiplication and division and then store it in variables. i.e num1 - num2 for subtraction etc..
4. Print the result.

Best Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 18-Nov-2005, 00:09
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road

Re: Help needed on visual C++ basic math program


nicely explained paramesh
  #4  
Old 18-Nov-2005, 00:42
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Help needed on visual C++ basic math program


Quote:
Originally Posted by alcoholic
nicely explained paramesh
Thank You alcoholic(are you?)

and Welcome to the GIDForums™.

Best Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #5  
Old 18-Nov-2005, 01:48
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Help needed on visual C++ basic math program


Quote:
Originally Posted by Paramesh
I am from India.

Not just India ! I've read you are from Madras (or Fort St. George) !
__________________
It's actually a one time thing (it just happens alot).
  #6  
Old 18-Nov-2005, 01:52
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Help needed on visual C++ basic math program


Quote:
Originally Posted by Kobi
Not just India ! I've read you are from Madras (or Fort St. George) !
Not Madras. The name has changed to Chennai(tamil word).

Fort St. George is in Chennai.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
GIDForums enables New [VB] Visual Basic bbcode. admin .NET Forum 0 01-Oct-2005 13:08
Enter key in basic c program hopesfall C Programming Language 2 26-Mar-2005 16:09
Help with simple math table program (was a SIMPLE program) Bubba C Programming Language 3 09-Mar-2005 12:40
What is "Ambigious symbol" ??*( a compilation error) small_ticket C++ Forum 2 07-Jan-2005 21:10
C Currency Conversion program help needed mutt C Programming Language 1 13-Jun-2004 15:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:41.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.