# C++:Adding two numbers



## spinach-guy (Aug 8, 2007)

We have just started C++ in college


```
#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??


----------



## max_demon (Aug 8, 2007)

^^^Me too .
Help me


----------



## casanova (Aug 8, 2007)

May I know which compiler are you using.

This program will run properly


> #include<iostream.h>
> main ()
> {
> int a,b,c;
> ...


----------



## blueshift (Aug 8, 2007)

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.


----------



## sandeepk (Aug 8, 2007)

spinach-guy said:
			
		

> We have just started C++ in college
> 
> 
> ```
> ...


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.


----------



## max_demon (Aug 8, 2007)

Dev C++?


----------



## xbonez (Aug 8, 2007)

leave out all the complications. just run



> #include<iostream.h>
> #include<conio.h>
> {
> clrscr();
> ...



simple as tht


----------



## whoopy_whale (Aug 9, 2007)

Don.t forget to add the *main()* function too...  


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


----------



## navjotjsingh (Aug 9, 2007)

And what's with your second line: using namespace std;

It also of no use.


----------



## piyush gupta (Aug 9, 2007)

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


----------



## spinach-guy (Aug 9, 2007)

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 (Aug 9, 2007)

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

Here's the link to Turbo C++
*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.*


----------



## casanova (Aug 9, 2007)

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
> ...



What to do?


----------



## sandeepk (Aug 9, 2007)

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 (Aug 10, 2007)

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


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

or


```
#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 (Aug 10, 2007)

Let me post a good ANSI C++ Program ...


```
# 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++


----------



## Ron (Aug 10, 2007)

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.
> ...



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++................


----------



## Yamaraj (Aug 10, 2007)

shirish_nagar said:
			
		

> Let me post a good ANSI C++ Program ...
> 
> 
> ```
> ...


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


----------



## Garbage (Aug 10, 2007)

@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 - *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 !!!



> 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 !!! 



> 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 !!!


----------



## QwertyManiac (Aug 10, 2007)

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.


----------



## utsav (Aug 10, 2007)

i am using turbo c++ ver 3.0 from the last 18 months . I am in 12th now and the computer teacher is very impressed from my programming skills.though i don't know programming very deeply but according to ISC board standards it is sufficient. 
 can any1 tell me from where to download the latest version.I am low on bandwidth that's why i am not downloading the rapidshare link given bu goyal2000. i want 2 download the latest version


----------



## Zeeshan Quireshi (Aug 10, 2007)

g_goyal2000 said:
			
		

> Why not simply use Turbo C++???
> It's much better & easy to use for starters.
> 
> Here's the link to Turbo C++
> ...


No man , coz Turbo C++ is *NOT* standard C++ . It does not support the ISO C++ Specification and thus is usless in the current context , using any FRee IDE(Visual C++ Express , or borland's free compiler) will be much better. 

also , Turbo C++ does NOT run fine under Windows XP(which is what the majority uses)


----------



## casanova (Aug 10, 2007)

Thx Zeeshan for nice info. Can someone sort out the problem I mentioned in Post #13


----------



## utsav (Aug 11, 2007)

turbo c++ consumes 100 % cpu on my p4 3 GHz system .unbelievable


----------



## QwertyManiac (Aug 11, 2007)

I think all 16-bit apps do that?

@casanova - Try reinstalling GCC packages? Looks like an incomplete or limited installation which is thus reporting cc1.exe is missing..


----------



## navjotjsingh (Aug 11, 2007)

Zeeshan Quireshi said:
			
		

> also , Turbo C++ does NOT run fine under Windows XP(which is what the majority uses)



Turbo C++ does run under XP. I run it successfully.


----------



## sandeepk (Aug 12, 2007)

navjotjsingh said:
			
		

> Turbo C++ does run under XP. I run it successfully.


I too have problems running Turbo C++ on my Win XP machine. My sister uses it for her 12th class programs. I don't need it, but whenever she tries to make it fullscreen then the whole computers just freezes. (Using Alt+Enter). What you need to do to work it properly?


----------



## Pathik (Aug 12, 2007)

Use dev c++..


----------



## navjotjsingh (Aug 12, 2007)

sandeepk said:
			
		

> I too have problems running Turbo C++ on my Win XP machine. My sister uses it for her 12th class programs. I don't need it, but whenever she tries to make it fullscreen then the whole computers just freezes. (Using Alt+Enter). What you need to do to work it properly?



Don't run TC using its shortcut. Directly go to bin directory and click on TC.exe I just tested my copy of Turbo C++ and it still works nicely on XP SP2.


----------



## casanova (Aug 12, 2007)

@sandeepk

Don't start TC from windows. Go to command prompt, then navigate to tc and then start it from there. U won't have any problem

@qwerty
I had made a full install. Is it coz I am using Vista?


----------



## sandeepk (Aug 12, 2007)

@navjotjsingh & casanova 
Thanks!!! It is working. Why does this happen with shortcut?


----------



## casanova (Aug 13, 2007)

TC is a 16 bit proggy. When you run it from command, it is being handled more efficiently.


----------



## Zeeshan Quireshi (Aug 13, 2007)

casanova said:
			
		

> TC is a 16 bit proggy. When you run it from command, it is being handled more efficiently.


But still then it consumes 100% on my PC .

and anyways , Turbo C++ *Does Not* support Standard C++ , so it's useless anyways .

Why not use Visual C++ Express or GCC when they're much better ?


----------



## sandeepk (Aug 13, 2007)

Zeeshan Quireshi said:
			
		

> But still then it consumes 100% on my PC .
> 
> and anyways , Turbo C++ *Does Not* support Standard C++ , so it's useless anyways .
> 
> Why not use Visual C++ Express or GCC when they're much better ?


Yes I do use VC++ only. But my sister needs TC as in college they told you to  use TC only. Also for exams it is the one that is available. When will these colleges learn that TC is really old to be any useful these days?


----------



## navjotjsingh (Aug 13, 2007)

Blame it on Old CBSE Course Too. They still teach old format C++.


----------



## casanova (Aug 13, 2007)

Yes, academics spoil things. They ask to use TC which is lacking in features. 

@zeeshan
Yes, it still uses lot of cpu. But it works atleast


----------



## Zeeshan Quireshi (Aug 14, 2007)

navjotjsingh said:
			
		

> Blame it on Old CBSE Course Too. They still teach old format C++.


Well actually there's an alternative to TC that will compile the old format code just fine under windows XP and into a 32 bit executable .

Here's how u can use Borland's FREE C++ Compiler along with the Relo IDE[i use it myself for school program testing  ] :

1. Get Borland's Free C++ Compiler Here
*dn.codegear.com/article/20633

2. Download the Free Relo IDE Here
*www.fifsoft.com/relo/

3. Now Install Borland's Compiler First n then Relo IDE , it will ask u for Compiler Path during setup n just provide the path where u installed Borland Compiler n that's it, u have yor Development Enivronment Set-up and ready to go 

*farm2.static.flickr.com/1073/1111520417_76023e1cc7.jpg


----------



## sandeepk (Aug 14, 2007)

Thanks for this advice. It is better than TC!!!


----------



## Garbage (Aug 14, 2007)

@ sandeepk,

have u ever tested DevC++ ??? It is far better than this! Bcoz it supports OLD as well as NEW (ISO) C++


----------



## Zeeshan Quireshi (Aug 14, 2007)

shirish_nagar said:
			
		

> @ sandeepk,
> 
> have u ever tested DevC++ ??? It is far better than this! Bcoz it supports OLD as well as NEW (ISO) C++


But problem is tat it DOES NOT Support borland specific headers[ conio.h anyone ? ] which are taught in the ICSE n CBSE curriculum


----------



## Zeeshan Quireshi (Aug 14, 2007)

Kenshin said:
			
		

> Use dosbox i used to use dosbox to run TC,...it runs fine under dosbox....but i hate tc hehe.....now we r usin assembler and its turbo assembler and it looks same like tat TC... i hate everythin tat looks like tc...hehe


Biggest prob is that TC does not run fine under my PC in ANY way , that's y use Relo


----------



## Garbage (Aug 14, 2007)

Zeeshan Quireshi said:
			
		

> But problem is tat it DOES NOT Support borland specific headers[ conio.h anyone ? ] which are taught in the ICSE n CBSE curriculum



Who says that ???

Infact take a look @ this code,


```
# include <iostream.h>
[COLOR=Red][B]# include <conio.h>[/B][/COLOR]

int main()
{
    int a,b;
    cout << "Enter 2 numbers : ";
     cin >> a >> b;
    
    cout << "Addition = " << a+b;
    
    getch();
}
```

This code is perfectly valid in Dec C++.
And if u can see, then I've included *conio.h* & used *getch()* from that lib.


----------



## Zeeshan Quireshi (Aug 14, 2007)

well , it frankly wasn't valid the last time i used Dev C++(that was when v4 was launched i suppose)

anyways , use whatever u like , except TC that is


----------



## Yamaraj (Aug 14, 2007)

shirish_nagar said:
			
		

> Who says that ???
> 
> Infact take a look @ this code,
> 
> ...


It does not conform to the ISO C++ standards, regardless of the fact that even GCC can compile this code. The C++ programming language has nothing to do with screen, keyboard input and other I/O hardware interfacing.


----------



## Sykora (Aug 14, 2007)

@shirish_nagar : That is definitely not valid code in dev c++. Dev c++ uses mingw compiler, which is based on gcc, and will choke on that code because it conforms to standards written in about 1995, and since then have become outdated. Even if it does work, you must have copied conio from a TC install to your dev include directory.

EDIT : Slightly late...


----------



## Garbage (Aug 14, 2007)

^^ nah....

I've just installed DevC++ on my machine & compiled this!
Though it gives warnings bcoz I've used "iostream*.h*" instead of "iostream". But it compiled & run.


----------



## g_goyal2000 (Aug 14, 2007)

Zeeshan Quireshi said:
			
		

> Turbo C++ does NOT run fine under Windows XP(which is what the majority uses)


Actually it does. It runs fine on all Windows w/o any service pack(s). I've run it successfully on Win 98, 98SE, 2000, ME, XP, 2003. I've been using it for more than 2 years & still do.


----------



## Garbage (Aug 15, 2007)

^^ yeh, just thing is that u don't have to INSTALL it, copy the TCC ot TC++ folder & navigate thru folder in command prompt.

Then it should work fine!!!


----------



## Zeeshan Quireshi (Aug 15, 2007)

g_goyal2000 said:
			
		

> Actually it does. It runs fine on all Windows w/o any service pack(s). I've run it successfully on Win 98, 98SE, 2000, ME, XP, 2003. I've been using it for more than 2 years & still do.


But it takes up 100% CPU time on my machine(n all other Win XP machines i've used)


----------



## navjotjsingh (Aug 15, 2007)

Seems problem at your end. Here everything is Ok.


----------

