-6

I am developing a calculator program in c++. But the problem is when I try to convert the character into integer it shows runtime error. The code is given below.

#include<iostream.h>
#include<conio.h>

void main()
{
   clrscr();
   int num,sum=0;
   cout<<"Enter the number"<<endl;
   cin>>num;
   while(num!='=')
   {
      sum=sum+num;
      cin>>num;
   }
   cout<<"The sum is"<<endl;
   getch();
}

The program runs well i.e it takes the input correctly but when I used to press '=' sign then it shows nothing but only the black screen. Please help me. Thankyou.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
indranil
  • 11
  • 1
  • 3

2 Answers2

0

Your issue lies in this loop

while(num!='=')
   {
      sum=sum+num;
      cin>>num;
   }

I will tell you why your code is hanging and why is not asking for more inputs, even if the condition in while loop fails. This happens when you insert a char instead of an int to something, when you input '=' the stream gets corrupted.

You need to use something like this to avoid hanging.

    {
        sum = sum + num;
        cin >> num;
        cin.clear();
        cin.ignore();
    }

And don't insert a char when your code is expecting an int, it sets the error flag and no more reading is possible. And since you can't input char inplace of an int, ask the user at the start how many numbers he wants to add, and then run your loop for that many times to get the input.

like this:

cout<<"Enter the number of items"<<endl;
cin>>items;
cout<<"Enter first item" <<endl;
while(items)
{
cin>>num;
sum=sum+num;
items--;
}
Nishant
  • 1,635
  • 1
  • 11
  • 24
  • Thanx for ur rply.. But i also try to put 0x3D instead of '=' but its showing the same problem. y? – indranil Aug 04 '15 at 10:06
  • i already mentioned, once you try to insert char instead of an int inside int type of variable, the error flag is raised and cin stops taking further more input, so at the moment you enter "=", your num variable is not assigned any new value, instead it is storing the old value only. – Nishant Aug 04 '15 at 10:11
  • Then y Mr R.Sahu is saying to use namespace std ..? and miss ridimha also saying that it will run in turbo C++. ? – indranil Aug 04 '15 at 10:20
  • R.Sahu is right, because is standard header for C++, not Check this http://stackoverflow.com/questions/2976477/difference-between-iostream-and-iostream-h – Nishant Aug 04 '15 at 10:24
  • and cout and cin are global object defined in the std namespace, and endl is a (stream manipulator) function also defined in the std namespace. So you need to use "std" namespace in your code. After including header files, you need to write "using namespace std" in your file... Refer this http://stackoverflow.com/questions/15346944/c-questions-about-using-namespace-std-and-cout – Nishant Aug 04 '15 at 10:26
  • OK so if i use namespace std ,then it will run or what..? – indranil Aug 04 '15 at 10:27
  • R. Sahu said this code should not compile, yes after including these things it will compile, but the issue in your code i have mentioned in my answer, even if you get your code compiled, it won't run as desired.... and i have provided alternate solution as well. – Nishant Aug 04 '15 at 10:29
  • OK thanx a lot for sharing ur precious time and knowledge..It will help me alot.. – indranil Aug 04 '15 at 10:32
  • If it helped you, you can select this answer as accepted – Nishant Aug 04 '15 at 10:37
0

After you type '=', this character be convert to int since you declare num as int. But unfortunately the result isn't as you expected. The conversion failed!When an error occurs when reading from a stream, an error flag gets set and no more reading is possible until you clear the error flags. That's why you get an hang

Please refer to cin for an int inputing a char causes Loop that is supposed to check input to go wild

Community
  • 1
  • 1
Dean Song
  • 341
  • 1
  • 10