473,626 Members | 3,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Order of error detection

9 New Member
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 1783
sicarie
4,677 Recognized Expert Moderator Specialist
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
coolsmaster
9 New Member
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
3326
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 : http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96653/create_ps.htm#66206 Still i give the steps i followed. Preparing the Primary Database for Standby Database Creation
2
3008
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 religion. The good in this is I discovered my web site http://www.oawater.com has a slide show on the main page that is broke in Opera 7.11. I get this error message. Can anyone give me some direction? Event thread: onload Error:
3
11465
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 'City' then you select 'Business Category' - now when you select 'Business Category' the next drop-down menu (business sub category) should populate with 'Business Sub-categories' related to the 'Business Category' but once 'Business Category' is...
7
5881
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 down. So i have this statement: document.getElementById("dropdown").options.selected=true; And it gives me an error.
8
2616
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 something entirely different? The docs only mention regular triggers being executed alfabetically.
35
3770
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 gets about 3 or so. It also gets very awkward in nested loops, where you want to check for normal loop processing in the loop condition, not errors. Yes, you could put some generic exit flag in the loop condition, but when you're simply done if...
0
1765
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 cause of error. error: a detected fault. error handling: planned processing (perhaps via formal mechanisms) that occurs upon fault detection. error correction: returning a program (or system) back to an error-free
3
7318
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
1911
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 intrusion detection installation than monitoring alone. Monitoring can help you spot problems in your network, as well as identify performance problems, but watching every second of traffic that passes through your network, manually searching for...
0
8641
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.