473,396 Members | 2,070 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,396 software developers and data experts.

Order of error detection

When I put my code through different inputs, one form of input results in a problem. when I enter:

updatename 123456789 10 f9

the expected output is

"Error: 10 is out of the range 0-4"

whereas I got

"Error: argument is not a number"

i tried moving the error detection around and it wouldn't work. Seem like a simple problem but I can find the solution for it :S

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char a[maxLength];         //command
  4.     char l[maxLength];         //last name
  5.     char f[maxLength];         //first name
  6.     int b;
  7.     int idx, mark;
  8.  
  9. //    cin >> a >> b;
  10.  
  11.     while (! cin.eof()) {
  12.         cin >> a;
  13.         if(cin.fail())                                                   // checks for non-integer inputs
  14.         {
  15.             cout << "Error: Argument is not a number." << endl;
  16.             cin.clear();                                               // lowers error flag
  17.             cin.ignore(50, '\n');                                     // ignores rest of string
  18.             }
  19.  
  20.         else {
  21.         for (char * c = a; *c != '\0'; c++)
  22.             *c = tolower(*c);        // Scan through the entire string; change the input
  23.                                     // into lower case.
  24.  
  25.         // See which of the commands was entered
  26.  
  27.  
  28.         if (!strcmp (a, "new")) {                             //checks for the command "new" in the string
  29.  
  30.             if(cin >> b){        
  31.  
  32.             if(b <= 999999999){
  33.             cout<< "New: ";
  34.             cout.fill('0');
  35.             cout<< setw(9) << dec << b <<endl;
  36.             }
  37.             else{
  38.                 cout<<"Error: "<< b <<" is too large.\n";}
  39.             }
  40.         }else if (!strcmp (a, "locate")) {                     //checks for the command "locate" in the string
  41.             if(cin >> b){
  42.  
  43.  
  44.             if(b <= 999999999){
  45.             cout<< "Locate: ";
  46.             cout.fill('0');
  47.             cout<< setw(9) << dec << b <<endl;
  48.             }
  49.             else{
  50.                 cout<<"Error: "<< b <<" is too large.\n";
  51.             }
  52.             }
  53.         }else if (!strcmp (a, "updatename"))                    //checks for the command "updatename" in the string
  54.         {
  55.             if(cin >> b >> l >> f){        
  56.             if(b <= 999999999){
  57.             cout<< "Updatename ";
  58.             cout.fill('0');
  59.             cout<< setw(9) << dec << b << "";
  60.             cout<<" "<< f << " "<< l <<endl;
  61.             }
  62.             else{
  63.                 cout<<"Error: "<< b <<" is too large.\n";    
  64.             }
  65.             }
  66.         }else if (!strcmp (a, "updatemark")) {                   //checks for the command "updatemark" in the string
  67.  
  68.             if(cin >> b >> idx >> mark){    
  69.             if(idx < 0 || idx > 4){
  70.  
  71.                 cout<<"Error:"<< idx <<" is out of the range 0-4.\n";
  72.                 cin.clear();
  73.                 cin.ignore(50, '\n'); 
  74.             }else if(mark < 0 || mark > 100){
  75.                 cout<<"Error:"<< mark <<" is out of the range 0-100.\n";
  76.  
  77.             }                
  78.             if(b <= 999999999){
  79.             cout<< "Updatemark ";
  80.             cout.fill('0');
  81.             cout<< setw(9) << dec << b << "";
  82.             cout<<" "<< idx << " "<< mark <<endl;
  83.             }
  84.             else{
  85.                 cout<<"Error: "<< b <<" is too large.\n";    
  86.             }
  87.  
  88.             }
  89.  
  90.  
  91.  
  92.         }else if (!strcmp (a, "delete")) {                       //checks for the command "delete" in the string
  93.             if(cin >> b){
  94.  
  95.  
  96.             if(b <= 999999999){
  97.             cout<< "Delete: ";
  98.             cout.fill('0');
  99.             cout<< setw(9) << dec << b <<endl;
  100.             }
  101.             else{
  102.                 cout<<"Error: "<< b <<" is too large.\n";
  103.             }
  104.             }
  105.         }else if (!strcmp (a, "printall")) {                         //checks for the command "printall" in the string
  106.  
  107.             cout<< "Printall\n";
  108.  
  109.         }else if (!strcmp (a, "deleteall")) {                                //checks for the command "delete" in the string
  110.             cout<< "Deleteall\n";
  111.         }else {
  112.             cin.ignore(1000,'\n');
  113.             cout<< "Error: Unknown Command\n";
  114.         }
  115.  
  116.         }        
  117.  
  118.     }
  119.  
  120.     return (0);}
Mar 28 '07 #1
2 1774
sicarie
4,677 Expert Mod 4TB
When I put my code through different inputs, one form of input results in a problem. when I enter:

updatename 123456789 10 f9

the expected output is

"Error: 10 is out of the range 0-4"

whereas I got

"Error: argument is not a number"

i tried moving the error detection around and it wouldn't work. Seem like a simple problem but I can find the solution for it :S
Did you try putting cout statements after lines of execution, and find out where it will and won't get to in the program?
Mar 28 '07 #2
I used Visual Studio's debug feature to walk through the code line by line.

when I input

"updatemark 123456789 10 f9"

after cin >> a; it will strcmp to updatemark and then cin >> b >> idx >> mark;
then it will jump back to cin >> a; and produce "the arguement is not a number" because the value of a was alter to a unknown digit.

I can't seem to get this work no matter how i tried rearranging the code.
Mar 29 '07 #3

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

Similar topics

0
by: Cherrish Vaidiyan | last post by:
sir, The following are the steps that i followed in setting up standby database on Red hat Linux 9. i am using Oracle 9i. i have followed the steps in this site : ...
2
by: andy johnson | last post by:
I made the mistake of "upgrading" to IE6.0 on my windoze 98 laptop. I really wish bill gates would die in a horribly painful accident. Anyway, after much grief, I downloaded Opera and saw true...
3
by: news.onetel.net.uk | last post by:
I and my friend Karl have spent literally all day trying to find out what is causing my error but we are zapped of any further functionality :) I have a form that adds news records. You select...
7
by: debugger | last post by:
hello, Question, on page load, I populate an existing drop down with createElement and appendChild. It works fine so far. BUT I want to automatically select some option from this populated drop...
8
by: Frank van Vugt | last post by:
Hi, If during a transaction a number of deferred triggers are fired, what will be their execution order upon the commit? Will they be executed in order of firing or alfabetically or...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
0
by: JohnQ | last post by:
(The thread "Error Handling Idioms" prompted this post. I meant to post it at the top level, but it got posted as a reply. So here it is again!) An attempt at common defintions: fault: the...
3
by: harshadanarvekar | last post by:
Hi Everyone, Here is a part of javascript code that works well in FF2 but shows above error in IE6 and error "missing name after . operator" in NS8 function PopUp(idf,stepX,stepY,speed){
0
by: origami.takarana | last post by:
Intrusion Detection Strategies ----------------------------------- Until now, we’ve primarily discussed monitoring in how it relates to intrusion detection, but there’s more to an overall...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.