Okay heres my problem I have a problem called Calc which the user enters 2 numbers and a sign to be done with what is entered
EX:
./calc 200 + 200
or
./calc 200.20 + 200
Okay thats easy where I can't seem to get, my prof wants the input checked to see if the char *argv[1] or char *argv[2] contains a float or int. so I have to check for a decimal point.
I know I can search the number like a string because it's in an array of char but my code seems to not work for that. So I tool another approach and I'm use strtol to convert it to a int. If the decimal point is found then I can assume the number is float.
Here is my code for examimation: -
#include<stdio.h>
-
-
#include<stdlib.h>
-
#include<string.h>
-
#include<errno.h>
-
#include<limits.h>
-
-
-
-
char *FindChr(char *szString, char chr);
-
-
int main(int argc, char *argv[])
-
-
{
-
-
float fValueOneInputed;
-
-
float fValueTwoInputed;
-
float fCalculatorStore;
-
-
//Test the input to see if the value entered if Float or Int
-
FindChr(argv, '.');
-
//Get the first input value in char form and take it to a float
-
sscanf(argv[1],"%f",&fValueOneInputed);
-
-
//Get the first input value in char form and take it to a float
-
sscanf(argv[3],"%f",&fValueTwoInputed);
-
-
if(*argv[2] == '+')printf("%0.3f", fCalculatorStore = fValueOneInputed + fValueTwoInputed);
-
-
if(*argv[2] == '-')printf("%0.3f", fCalculatorStore = fValueOneInputed - fValueTwoInputed);
-
-
if(*argv[2] == 'x')printf("%0.3f", fCalculatorStore = fValueOneInputed * fValueTwoInputed);
-
-
if(*argv[2] == '/')printf("%0.3f", fCalculatorStore = fValueOneInputed / fValueTwoInputed);
-
return 0;
-
-
}
-
-
char *FindChr(char *szString, char chr)
-
-
{
-
-
int base;
-
int iLoop;
-
long lTestVal;
-
char *str;
-
char *endptr;
-
str = szString[1];
-
base = 0;
-
errno = 0;
-
lTestVal = strtol(str, &endptr, base);
-
-
/* Check for Error's */
-
if((errno == ERANGE && (lTestVal == LONG_MAX || lTestVal == LONG_MIN)) || (errno != 0 && lTestVal == 0)){
-
perror("strol");
-
exit(EXIT_FAILURE);
-
}
-
if(*endptr != '\0'){
-
printf("Further character after number: %s\n", endptr);
-
exit(EXIT_SUCCESS);
-
}
-
return;
-
}
-
I got the implimentation from man strtol and I've read that man page like 10 times but 2 problems exist.
1)It always segment faults
2)It is implimented correctly
Anyhelp would be appreached
Thanks
DOCMUR
3 2720 sicarie 4,677
Recognized Expert Moderator Specialist
Moving to C++ Forum (Please double check where you are next time. Thanks!)
????Why the code is C not C++
sicarie 4,677
Recognized Expert Moderator Specialist
????Why the code is C not C++
Because you posted in the Articles forum, and the Proper forum name is C/C++ (we do both).
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: hokiegal99 |
last post by:
I don't understand how to use a loop to keep track of user input. Could
someone show me how to do what the program below does with a loop?
Thnaks!
----------------------------
#Write a program that reads 10 numbers from the user and prints out the
sum of those numbers.
num0 = input("Enter a number: ")
|
by: jeff regoord |
last post by:
A user inputs a float value. The scanf() function gets the value.
However, I need to create an error handler with an if else statement
saying invalid input if the input is not a number. Does anybody know
how I could do this?
|
by: karthi |
last post by:
hi,
I need user defined function that converts string to float in c.
since the library function atof and strtod occupies large space in
my processor memory I can't use it in my code.
regards,
Karthi
|
by: sam |
last post by:
hi all,
i'm starting to put together a program to simulate the performance of
an investment portfolio in a monte carlo manner doing x thousand
iterations and extracting data from the results.
i'm still in the early stages, and am trying to code something simple
and interactive to get the percentages of the portfolio in the five
different investment categories. i thought i'd get in with the error
handling early so if someone types in...
|
by: Diogenes |
last post by:
Hi All;
I, like others, have been frustrated with designing
forms that look and flow the same in both IE and Firefox.
They simply did not scale the same.
I have discovered, to my chagrin, that IE7 does not seem
to offer any way to control the font size of a text input
element.
| |
by: dmoran21 |
last post by:
Hi All, I am working on a program to take input from a txt file, do
some calculations, and then output the results to another txt file.
The program that I've written compiles fine for me, however, when I
run it, it stalls and does nothing. I'm wondering if there's something
obvious that I'm missing. My code is below and any help would be
appreciated.
Thanks,
Dave
|
by: no1zson |
last post by:
I have been reading through many of the array questions and cannot find one that addresses my issue. Maybe someone can help me out.
Same story, I am learning Java and have just written a CD Inventory application. It works, does what I want it to and all that, but now I need to put an array in there to store more than one cd at a time. Seems simple enough until I actually start coding. I want to save as much of the code as I can since I worked...
|
by: Jim Langston |
last post by:
The output of the following program is:
1.#INF
1
But:
1.#INF
1.#INF
was expected and desired. How can I read a value of infinity from a stream?
|
by: Arnie |
last post by:
Folks,
We ran into a pretty significant performance penalty when casting floats.
We've identified a code workaround that we wanted to pass along but also was
wondering if others had experience with this and if there is a better
solution.
-jeff
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |