Solved

Determing Input

Posted on 2004-09-10
10
181 Views
Last Modified: 2010-04-15
I'm trying to make a Roman to Int and vice versa program with C. The main question is here, how can I determine the input so if the user enters a 1 then it would convert it to roman and if he entered a letter then it would call the roman to int function!


0
Comment
Question by:fluked
  • 4
  • 3
10 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12029693
Try the system library macros isalpha() and isdigit(), defined in <ctype.h>

0
 

Accepted Solution

by:
lesniles earned 100 total points
ID: 12029739
The "right" way would be to use something like isdigit():

#include <ctype.h>
#include <stdlib.h>
...

   int i;
   i = 0;
   while ( isblank(s[i]) ) i++; /* Skip leading whitespace */
   if ( isdigit(s[i]) ) {
      to_roman(atoi(s));
   } else {
      to_int(&(s[i]));
   }

A more compact version uses the fact that atoi() returns 0 for
strings that do not represent decimal numbers, and the fact that
there is no roman numeral zero:

#include <stdlib.h>
...
   int n;
   n = atoi(s);
   if ( n != 0 ) {
      to_roman(n);
   } else {
      to_int(s);
   }

0
 
LVL 1

Author Comment

by:fluked
ID: 12050780
ok check this out your above answer is okay but im trying to do this

if ( argument 1 == isaletter ) && ( argument 2 == is a number )
 do

end

how would that work tho ?
0
 
LVL 1

Author Comment

by:fluked
ID: 12050913
how can i also convert a char* to an integer
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12050976
Check out the atoi() and strtol() library routines.
0
 
LVL 1

Author Comment

by:fluked
ID: 12050988
im looking for an example, thats why i increased the points to 200 :)
0
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 100 total points
ID: 12051092
int i = atoi("20");
long l = strtol("123456789", 10);
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12051099
If you actually read the documentation for atoi() or strtol(), you would have
found that the usage is so trivial that you don't really benefit by example.
0

Featured Post

Save on storage to protect fatherhood memories

You're the dad who has everything. This Father's Day, make sure your family memories are protected. My Passport Ultra has automatic backup and password protection to keep your cherished photos and videos safe. With up to 3TB, you have plenty of room to hold the adventures ahead.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now