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

4
  • 3
    Does that even compile? You are using <iostream.h> instead of <iostream>. You are using cin, cout, and endl without using the std namespace. Commented Aug 4, 2015 at 6:34
  • It wil compile on turbo c++ . Commented Aug 4, 2015 at 6:52
  • You appear to be learning 1980s C++. This is not going to help you whatsoever in the job market. Commented Aug 4, 2015 at 12:02
  • Then which C++ should I learn...? Commented Aug 14, 2015 at 7:08

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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--;
}

9 Comments

Thanx for ur rply.. But i also try to put 0x3D instead of '=' but its showing the same problem. y?
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.
Then y Mr R.Sahu is saying to use namespace std ..? and miss ridimha also saying that it will run in turbo C++. ?
R.Sahu is right, because <iostream> is standard header for C++, not <iostream.h> Check this stackoverflow.com/questions/2976477/…
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 stackoverflow.com/questions/15346944/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.