Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Determing Input

Posted on 2004-09-10
Medium Priority
188 Views
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
Question by:fluked
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3

LVL 23

Expert Comment

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

0

Accepted Solution

lesniles earned 400 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

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

ID: 12050913
how can i also convert a char* to an integer
0

LVL 23

Expert Comment

ID: 12050976
Check out the atoi() and strtol() library routines.
0

LVL 1

Author Comment

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

LVL 23

Assisted Solution

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

LVL 23

Expert Comment

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

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ouâ€¦
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.
###### Suggested Courses
Course of the Month6 days, 21 hours left to enroll