View Full Version : C++:Adding two numbers
spinach-guy
08-08-2007, 07:49 PM
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??:confused: :confused: :confused:
max_demon
08-08-2007, 08:24 PM
^^^Me too .
Help me
casanova
08-08-2007, 08:40 PM
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
08-08-2007, 10:16 PM
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
08-08-2007, 11:16 PM
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??: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.
max_demon
08-08-2007, 11:51 PM
Dev C++?
xbonez
08-08-2007, 11:52 PM
leave out all the complications. just run
#include<iostream.h>
#include<conio.h>
{
clrscr();
int a,b;
cin>>a>>b;
cout<<a+b;
getch();
}
simple as tht :)
whoopy_whale
09-08-2007, 09:29 AM
Don.t forget to add the main() function too... :o
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cin>>a>>b;
cout<<a+b;
getch();
}
navjotjsingh
09-08-2007, 10:21 AM
And what's with your second line: using namespace std;
It also of no use.
piyush gupta
09-08-2007, 10:40 AM
^^thats staandard namespace u use for ur program not useful here but itzz very useful in big projects with 1000s of code
spinach-guy
09-08-2007, 01:22 PM
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
09-08-2007, 05:50 PM
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.
casanova
09-08-2007, 06:10 PM
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
09-08-2007, 10:41 PM
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!!! 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.
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
10-08-2007, 04:13 PM
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++
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++................
Yamaraj
10-08-2007, 07:35 PM
Let me post a good ANSI C++ Program ...
...
system ("PAUSE");
...
[snipped...]
Sorry, but this one statement makes your program a nonstandard one.
Read more about it - http://www.gidnetwork.com/b-61.html
Garbage
10-08-2007, 08:11 PM
@Ron ,
From HERE (http://sourceforge.net/project/downloading.php?groupname=dev-cpp&filename=devcpp-4.9.9.2_nomingw_setup.exe&use_mirror=superb-east) u can download Dev C++ (http://www.bloodshed.net/devcpp.html)
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 !!!
QwertyManiac
10-08-2007, 08:17 PM
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
10-08-2007, 09:49 PM
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
10-08-2007, 10:07 PM
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.
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
10-08-2007, 11:11 PM
Thx Zeeshan for nice info. Can someone sort out the problem I mentioned in Post #13
utsav
11-08-2007, 11:46 AM
turbo c++ consumes 100 % cpu on my p4 3 GHz system .unbelievable:shock:
QwertyManiac
11-08-2007, 04:05 PM
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
11-08-2007, 04:36 PM
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
12-08-2007, 03:30 PM
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
12-08-2007, 03:41 PM
Use dev c++..
navjotjsingh
12-08-2007, 05:15 PM
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
12-08-2007, 06:50 PM
@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
12-08-2007, 10:51 PM
@navjotjsingh & casanova
Thanks!!! It is working. Why does this happen with shortcut?
casanova
13-08-2007, 12:56 PM
TC is a 16 bit proggy. When you run it from command, it is being handled more efficiently.
Zeeshan Quireshi
13-08-2007, 08:48 PM
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
13-08-2007, 08:56 PM
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
13-08-2007, 09:11 PM
Blame it on Old CBSE Course Too. They still teach old format C++.
casanova
13-08-2007, 11:02 PM
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
14-08-2007, 11:14 AM
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
http://dn.codegear.com/article/20633
2. Download the Free Relo IDE Here
http://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 :)
http://farm2.static.flickr.com/1073/1111520417_76023e1cc7.jpg (http://farm2.static.flickr.com/1073/1111520417_511a4e87ef_o.jpg)
sandeepk
14-08-2007, 04:32 PM
Thanks for this advice. It is better than TC!!!
Garbage
14-08-2007, 06:26 PM
@ sandeepk,
have u ever tested DevC++ ??? It is far better than this! Bcoz it supports OLD as well as NEW (ISO) C++ :-)
Zeeshan Quireshi
14-08-2007, 08:02 PM
@ 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
14-08-2007, 08:08 PM
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
14-08-2007, 08:45 PM
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>
# include <conio.h>
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.:D
Zeeshan Quireshi
14-08-2007, 08:58 PM
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
14-08-2007, 09:17 PM
Who says that ???
Infact take a look @ this code,
# include <iostream.h>
# include <conio.h>
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.:D
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
14-08-2007, 09:20 PM
@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
14-08-2007, 09:23 PM
^^ 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
14-08-2007, 09:51 PM
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
15-08-2007, 07:01 AM
^^ 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
15-08-2007, 08:13 PM
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
15-08-2007, 08:31 PM
Seems problem at your end. Here everything is Ok.
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.