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 - int main()
-
{
-
char a[maxLength]; //command
-
char l[maxLength]; //last name
-
char f[maxLength]; //first name
-
int b;
-
int idx, mark;
-
-
// cin >> a >> b;
-
-
while (! cin.eof()) {
-
cin >> a;
-
if(cin.fail()) // checks for non-integer inputs
-
{
-
cout << "Error: Argument is not a number." << endl;
-
cin.clear(); // lowers error flag
-
cin.ignore(50, '\n'); // ignores rest of string
-
}
-
-
else {
-
for (char * c = a; *c != '\0'; c++)
-
*c = tolower(*c); // Scan through the entire string; change the input
-
// into lower case.
-
-
// See which of the commands was entered
-
-
-
if (!strcmp (a, "new")) { //checks for the command "new" in the string
-
-
if(cin >> b){
-
-
if(b <= 999999999){
-
cout<< "New: ";
-
cout.fill('0');
-
cout<< setw(9) << dec << b <<endl;
-
}
-
else{
-
cout<<"Error: "<< b <<" is too large.\n";}
-
}
-
}else if (!strcmp (a, "locate")) { //checks for the command "locate" in the string
-
if(cin >> b){
-
-
-
if(b <= 999999999){
-
cout<< "Locate: ";
-
cout.fill('0');
-
cout<< setw(9) << dec << b <<endl;
-
}
-
else{
-
cout<<"Error: "<< b <<" is too large.\n";
-
}
-
}
-
}else if (!strcmp (a, "updatename")) //checks for the command "updatename" in the string
-
{
-
if(cin >> b >> l >> f){
-
if(b <= 999999999){
-
cout<< "Updatename ";
-
cout.fill('0');
-
cout<< setw(9) << dec << b << "";
-
cout<<" "<< f << " "<< l <<endl;
-
}
-
else{
-
cout<<"Error: "<< b <<" is too large.\n";
-
}
-
}
-
}else if (!strcmp (a, "updatemark")) { //checks for the command "updatemark" in the string
-
-
if(cin >> b >> idx >> mark){
-
if(idx < 0 || idx > 4){
-
-
cout<<"Error:"<< idx <<" is out of the range 0-4.\n";
-
cin.clear();
-
cin.ignore(50, '\n');
-
}else if(mark < 0 || mark > 100){
-
cout<<"Error:"<< mark <<" is out of the range 0-100.\n";
-
-
}
-
if(b <= 999999999){
-
cout<< "Updatemark ";
-
cout.fill('0');
-
cout<< setw(9) << dec << b << "";
-
cout<<" "<< idx << " "<< mark <<endl;
-
}
-
else{
-
cout<<"Error: "<< b <<" is too large.\n";
-
}
-
-
}
-
-
-
-
}else if (!strcmp (a, "delete")) { //checks for the command "delete" in the string
-
if(cin >> b){
-
-
-
if(b <= 999999999){
-
cout<< "Delete: ";
-
cout.fill('0');
-
cout<< setw(9) << dec << b <<endl;
-
}
-
else{
-
cout<<"Error: "<< b <<" is too large.\n";
-
}
-
}
-
}else if (!strcmp (a, "printall")) { //checks for the command "printall" in the string
-
-
cout<< "Printall\n";
-
-
}else if (!strcmp (a, "deleteall")) { //checks for the command "delete" in the string
-
cout<< "Deleteall\n";
-
}else {
-
cin.ignore(1000,'\n');
-
cout<< "Error: Unknown Command\n";
-
}
-
-
}
-
-
}
-
-
return (0);}
2 1774
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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 :
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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){
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |