473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Else statement condition error

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
7 1635
RedSon
5,000 Expert 4TB
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
reon
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
5,000 Expert 4TB
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
reon
80
ok thanx...
Feb 3 '07 #5
reon
80
Any more ideas except brute force ...
Feb 3 '07 #6
horace1
1,510 Expert 1GB
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
reon
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

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

Similar topics

11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
3
by: Patrice | last post by:
Hi, I need to do multi-conditional statements like below, but this error is displayed : Expected 'End' /myFilepath, line x else response.write(arrCorpo(sparam,sdiv)) end if I don't...
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
2
by: juan-manuel.behrendt | last post by:
Hello together, I wrote a script for the engineering software abaqus/CAE. It worked well until I implemented a selection in order to variate the variable "lGwU" through an if elif, else...
1
by: jesmi | last post by:
hi all i have a problem with my code.following is my code: Connection con = null; PreparedStatement stmt = null;
7
by: Ixiaus | last post by:
I have just come across a site that discusses Python's 'for' and 'while' loops as having an (optional) 'else' structure. At first glance I interpreted it as being a bit like the 'default'...
21
by: Steve Swift | last post by:
On page 90 of my O'Reilly "Javascript The definitive guide" 3rd edition there is an example of an If/Else construct: (some text removed) If (username != null) alert("Hello " + username); else {...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.