• CONTEST ALERT - Experience the power of DDR5 memory with Kingston Click for details

C++:Adding two numbers

Status
Not open for further replies.

spinach-guy

Broken In
We have just started C++ in college

Code:
#include <iostream>
using namespace std;
main()
{ int a,b,c;
  cout<<"Enter two nos.:";
  cin>> a >> b;
  c=a+b;
  cin.get();
  cout<<"Sum is"<<c<<"";
  cin.get();
  
}

but on compiling and running it{no errors here}nothing happens when i enter two nos. and press enter.Why is it so??can someone guide me??:confused: :confused: :confused:
 

casanova

The Frozen Nova
May I know which compiler are you using.

This program will run properly
#include<iostream.h>
main ()
{
int a,b,c;
cout<<"Enter two nos:: " ;
cin>>a>>b;
c=a+b;
cout<<"Sum is :: "<<c ;
}
 

blueshift

Wise Old Crow
Is there any need to use instances of cin.get() ?
See in the Output window of the compiler. u might see the result/ o/p their.
Try using getch(); function instead at the end.

Try using getch(); function in the end instead.
 
Last edited:

sandeepk

Journeyman
spinach-guy said:
We have just started C++ in college

Code:
#include <iostream>
using namespace std;
main()
{ int a,b,c;
  cout<<"Enter two nos.:";
  cin>> a >> b;
  c=a+b;
  cin.get();
  cout<<"Sum is"<<c<<"";
  cin.get();
  
}
but on compiling and running it{no errors here}nothing happens when i enter two nos. and press enter.Why is it so??can someone guide me??:confused: :confused: :confused:
No need to use cin.get(). That too for 2 times!!!
When supplying input add a space between the numbers.
Even current program runs fine in MSVC6.
Which compiler you are using?
Also if using Tubro C++ 3 or similar DOS based compiler, then remember they are really old. Instead try using free Microsoft VC++ Express Edition which is based on current standard.
 

whoopy_whale

Journeyman
Don.t forget to add the main() function too... :eek:

Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cin>>a>>b;
cout<<a+b;
getch();
}
 

piyush gupta

Cyborg Agent
^^thats staandard namespace u use for ur program not useful here but itzz very useful in big projects with 1000s of code
 
OP
S

spinach-guy

Broken In
I am using bloodshed Cev C++ which i got from a Digit CD
And what's with your second line: using namespace std;

It also of no use.

Without using this,i get error like cin,cout not defined...something like that

No need to use cin.get(). That too for 2 times!!!

What's the exact use of this function.has it got something to do with user needing to press enter to continue??also what's the use of cin.ignore()??
 

g_goyal2000

Youngling
Why not simply use Turbo C++???
It's much better & easy to use for starters.

Here's the link to Turbo C++
http://rapidshare.com/files/47910015/TURBOC.EXE

Download it, open the file using Winzip or Winrar and extract it to C: drive.

Be sure to check that the option to use path names is checked otherwise the files will overwrite themselves & u'll have a corrupt complier in your hands.

Do Not execute the file directly by double clicking on it.
 
Last edited:

casanova

The Frozen Nova
Dint want to start a new thread as problem with the compiler in question.

When I run a Hello world program in C using Dev-CPP, I get the following error.

Compiler: Default compiler
Executing gcc.exe...
gcc.exe "C:\Users\Casanova\Documents\HelloWorld.c" -o "C:\Users\Casanova\Documents\HelloWorld.exe" -I"F:\Apps\Dev-Cpp\include" -L"F:\Apps\Dev-Cpp\lib"
gcc.exe: installation problem, cannot exec `cc1': No such file or directory

Execution terminated
Compilation successful

What to do?
 

sandeepk

Journeyman
spinach-guy said:
I am using bloodshed Cev C++ which i got from a Digit CD
First of all it is good that you are using Dev C++ which is a good C++ compiler.
using namespace std;
Without using this,i get error like cin,cout not defined...something like that
It is needed. Read more about namespace. It is a new concept introduced in ISO C++. It is needed in most of the new version compilers.


No need to use cin.get(). That too for 2 times!!!
spinach-guy said:
What's the exact use of this function.has it got something to do with user needing to press enter to continue??also what's the use of cin.ignore()??
This is not needed. It was a function used to get a single character from input stream. It is just a way to get character from keyboard like getch() or getche() used in C. For your program it is not needed at all. Read more about 'cin' from whichever book you are using. I would suggest Balaguruswamy as a good book to beginners. There are others also but this does provide a better explaination about all the things related to STL for the beginners.
 

Ron

||uLtiMaTE WinNER||
Hey
I hv also just started C++........May be thsi will help u......

Code:
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a,b;
cin>>a>>b;
cout<<a+b;
getch();
}

or

Code:
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a,b;
cout<<"input 1st no";
cin>>a;
cout<<"input 2nd no";
cin>>b;
cout<<a+b;
getch();
}
 

Garbage

God of Mistakes...
Let me post a good ANSI C++ Program ...

Code:
# include <iostream>
using namespace std;
int main()
{
    int a,b,c;
    cout << "Enter two numbers : ";
     cin >> a >> b;
    
    c = a + b;
    
    cout << "Addition = " << c << endl ;
    
    system ("PAUSE");
    return 0;
}

This is compiling perfectly on Dev C++

@ Ron

Let me tell u bro, the newer versions, there is no need to use ".h" in names of header files. If u use them, they are considered as backward headers.

But, if u r using OLD TC3 compiler, then it should be there.
Also using namespaces std; will NOT work in that compiler.

Better to go for new standards. U my try Dev C++
 
Last edited:

Ron

||uLtiMaTE WinNER||
shirish_nagar said:
Let me tell u bro, the newer versions, there is no need to use ".h" in names of header files. If u use them, they are considered as backward headers.

But, if u r using OLD TC3 compiler, then it should be there.
Also using namespaces std; will NOT work in that compiler.

Better to go for new standards. U my try Dev C++

Thnks Bro......i was not knowing this..as I hv also just started C++...probably 7 Days........
Do u hv some basic tutorial.....as I am unable to get the tutorail...........Our School teacher is mad.....She is teaching us carelessly.without giving any notes............It is out of our Ncert course book...........I am studing in class 10......

.. .......Can i get the link of Dev C++................
 

Garbage

God of Mistakes...
@Ron ,

From HERE u can download Dev C++

Yamaraj said:
Sorry, but this one statement makes your program a nonstandard one.
Read more about it - http://www.gidnetwork.com/b-61.html

Let me answer some of the things...

I've never understood why system("PAUSE") is so popular.

Bcoz it is handy !!!:D

This pause is very useful when your IDE won't wait as you test a program and as soon as the program finished the window closes taking all your data with it.
Thats why I used it !!! :p

t's not portable. This works only on systems that have the PAUSE command at the system level, like DOS or Windows. But not Linux and most others..
Let me tell you, C/C++ programs are NEVER portable. So why to bother for a single statement ???

You must include a header you probably don't need: stdlib.h or cstdlib
I think, he must be talking for C, NOT for C++. Bcoz I haven't included those headers.

Hope this is clear now !!!
 
Last edited:

QwertyManiac

Commander in Chief
C/C++ sources are portable if you write it for portability (w/o OS sensitive stuff). Programs aren't, right.

Why bother with IDEs and all when you've just started learning. Get a good Linux distro and G++/CC your way through the code.
 
Status
Not open for further replies.
Top Bottom