473,396 Members | 1,773 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.

isdigit to work with negative numbers

Ok well i know that isdigit in my code is seeing the negatives as a character and so i made my program give an error message. But i need negative numbers to work how could i get this to work with negative numbers.

The code is supposed to add the 3 numbers input into the command line argument including negative numbers.


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h> 
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6.  
  7. int main(int argc, char *argv[]){
  8.  
  9.    int add=0;
  10.    int i;
  11.    int sum;
  12.  
  13.    if(argc == 4){
  14.       for(i=1; i<argc; i++){
  15.          if(isdigit(*argv[i]) == 0){
  16.             fprintf(stderr, "%s: found non-numerical arg\n",argv[0]);
  17.             return EXIT_FAILURE;
  18.          } else if(isdigit(*argv[i]) !=0){
  19.             sscanf(argv[i],"%d",&sum);
  20.             add = add +sum;
  21.          } 
  22.       }
  23.       printf("%d\n",add);
  24.    } else{
  25.       fprintf(stderr,"incorrect number of args\n");
  26.    }
  27.    return EXIT_SUCCESS;
  28. }
Jul 28 '12 #1
2 11161
Rabbit
12,516 Expert Mod 8TB
Use an or to check for the negative sign when looking at the first character.
Jul 28 '12 #2
Omg lol so simple, thanks heaps. I did however have to swap my if statements around aswell because the or statement for the - sign is true in both cases and tends to just output the first if statement
Jul 28 '12 #3

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

Similar topics

0
by: Danny Winslow | last post by:
I get an unexpected result when I add two negative numbers in PHP on SPARC/Solaris 8. The same program works fine on Intel/Linux. I'm using PHP 4.3.1 on both systems. Here is my program,...
2
by: Mayank | last post by:
X-No-Archive: yes Hi, I have to replace all the negative numbers with 0 in a very large file. How can I do it in Perl/Awk. Negative numbers are of the format (-0.02:0.01:-1.00) (...
3
by: eddie wang | last post by:
formatcurrency function puts ( ) for negative numbers(e.g. (111) ). How can I display the negative numbers with a minus sign infront like -111. Thanks. *** Sent via Developersdex...
7
by: pj | last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter whether they are truly positive or negative ? I am having to cast each column to varchar to find out if there are any...
4
by: Charles A. Lackman | last post by:
Hello, I have some textboxes that are doing addition: Dim AString As Double AString = Convert.ToDouble(TextBox1.Text) - Convert.To(DoubleTextBox2.Text) TextBox3.Text =...
11
by: tlyczko | last post by:
Hello Rob B posted this wonderful code in another thread, http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/c84d8538025980dd/6ead9d5e61be85f0#6ead9d5e61be85f0 I could not...
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
6
by: ariel ledesma | last post by:
hello guys i just ran into this when comparing negative numbers, they start returning False from -6 down, but only when comparing with 'is' True False True i read that 'is' compares if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.