By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,990 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,990 IT Pros & Developers. It's quick & easy.

Else statement condition error

P: 80
In the below code.. else statements didnt work correctly... the program is to add the positive and negative numbers... if we enter alphabets it will show the wrong entry ,, but thats not working.....


Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. class loops
  4. {
  5. int i;
  6. int sum;
  7. int sum2;
  8. int num;
  9. public:
  10. void accept()
  11. {
  12. for(sum=num=i=0;i<3;i++)
  13.     {
  14.     cout<<"Enter a number : "<<endl;
  15.     cin>>num;
  16.     if(num>=0)
  17.     sum=sum+num;
  18.     else if(num<=0)
  19.     sum2=sum2+num;
  20.     else
  21.     {
  22.     cout<<"Wrong entry";    //its not working....(dont know wat to do)..
  23.     }
  24.     }
  25. }
  26. void display()
  27.     {
  28.     cout<<endl<<"The sum of positive numbers entered is : "<<sum;
  29.     cout<<endl<<"The sum of negative numbers entered is : "<<sum2;
  30.     }
  31. };
  32. void main()
  33. {
  34. clrscr();
  35. loops l1;
  36. l1.accept();
  37. l1.display();
  38. getch();
  39. }
Feb 2 '07 #1
Share this Question
Share on Google+
7 Replies


RedSon
Expert 5K+
P: 5,000
This is because alphabits are actually represented by numeric values that map to a standard ascii table. So when a user types a letter your program casts that value to an int.
Feb 2 '07 #2

P: 80
ok fine!! is there any method to convert int to char ... i'm a beginner if u know how to convert ....pls give up more details....

This is because alphabits are actually represented by numeric values that map to a standard ascii table. So when a user types a letter your program casts that value to an int.
Feb 2 '07 #3

RedSon
Expert 5K+
P: 5,000
Well, a brute force way is to check if your input variable is equivalent to all of the characters.
Expand|Select|Wrap|Line Numbers
  1. ////////PSEUDOCODE////////
  2. if (input == 'a' || input == 'b' || ... || input == 'z')
  3. {
  4.    //then a letter is input into your variable.
  5. }
  6.  
It might be easier to see if there is a library that will help you to support this type of input checking.
Feb 2 '07 #4

P: 80
ok thanx...
Feb 3 '07 #5

P: 80
Any more ideas except brute force ...
Feb 3 '07 #6

Expert 100+
P: 1,510
you have a couple of errors - the simple one was that you did not initiaialise sum2 to 0 so you negaive additions were wrong (local variables are not initialised by default). the other problem is dealing withfaulty input. if you attempt to read an int and you enter some faulty character the input fails and the character is left in the input stream. you have to clear the error condition and remove the faulty character before attempting to read again. try this
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. class loops
  4. {
  5. int i;
  6. int sum;
  7. int sum2;
  8. int num;
  9. public:
  10. void accept()
  11. {
  12. for(sum=sum2=num=i=0;i<3;i++)  //** zero sum2
  13.     {
  14.     cout<<"Enter a number : "<<endl;
  15.     while(! (cin>>num))  // ** if input fail
  16.        {
  17.        cout<<"Wrong entry, reentry number ";    //its not working....(dont know wat to do)..
  18.        cin.clear(); // ** clear error
  19.          cin.ignore(1000,'\n');  // remove faulty chars from stream
  20.        }
  21.      if(num>=0)
  22.            sum=sum+num;
  23.      else if(num<=0)
  24.          sum2=sum2+num;
  25.       }
  26.  
  27. }
  28. void display()
  29.     {
  30.     cout<<endl<<"The sum of positive numbers entered is : "<<sum;
  31.     cout<<endl<<"The sum of negative numbers entered is : "<<sum2;
  32.     }
  33. };
  34.  
  35. int main()
  36. {
  37. //clrscr();
  38. loops l1;
  39. l1.accept();
  40. l1.display();
  41. getchar();getchar();
  42. }
  43.  
Feb 3 '07 #7

P: 80
Ur guided me as i think....
But one problem is i didnt understand that cin.clear and cin.ignore ...(is that a keyword like cin.getline(var,size)..)
you used while loop but didnt enter continue or break,, thats so confusing to me....
if u dont mind either explain that or post any links or something to refer...
Feb 5 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.