473,809 Members | 2,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decting user input for float or int

2 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<errno.h>
  6. #include<limits.h>
  7.  
  8.  
  9.  
  10. char *FindChr(char *szString, char chr);
  11.  
  12. int main(int argc, char *argv[])
  13.  
  14. {
  15.  
  16. float fValueOneInputed;
  17.  
  18. float fValueTwoInputed;
  19. float fCalculatorStore;      
  20.  
  21.     //Test the input to see if the value entered if Float or Int
  22.     FindChr(argv, '.');
  23.     //Get the first input value in char form and take it to a float          
  24.     sscanf(argv[1],"%f",&fValueOneInputed);
  25.  
  26.     //Get the first input value in char form and take it to a float    
  27.     sscanf(argv[3],"%f",&fValueTwoInputed);
  28.  
  29.     if(*argv[2] == '+')printf("%0.3f", fCalculatorStore = fValueOneInputed + fValueTwoInputed);
  30.  
  31.     if(*argv[2] == '-')printf("%0.3f", fCalculatorStore = fValueOneInputed - fValueTwoInputed);
  32.  
  33.     if(*argv[2] == 'x')printf("%0.3f", fCalculatorStore = fValueOneInputed * fValueTwoInputed);
  34.  
  35.     if(*argv[2] == '/')printf("%0.3f", fCalculatorStore = fValueOneInputed / fValueTwoInputed);
  36. return 0;
  37.  
  38. }
  39.  
  40. char *FindChr(char *szString, char chr)
  41.  
  42. {
  43.  
  44. int base;
  45. int iLoop;
  46. long lTestVal;
  47. char *str;
  48. char *endptr;
  49. str = szString[1];
  50. base = 0;
  51. errno = 0;
  52. lTestVal = strtol(str, &endptr, base);
  53.  
  54. /* Check for Error's */
  55. if((errno == ERANGE && (lTestVal == LONG_MAX || lTestVal == LONG_MIN)) || (errno != 0 && lTestVal == 0)){
  56.     perror("strol");
  57.     exit(EXIT_FAILURE);
  58. }
  59. if(*endptr != '\0'){
  60.     printf("Further character after number: %s\n", endptr);
  61.     exit(EXIT_SUCCESS);
  62. }    
  63. return;
  64. }
  65.  
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
Sep 27 '07 #1
3 2720
sicarie
4,677 Recognized Expert Moderator Specialist
Moving to C++ Forum (Please double check where you are next time. Thanks!)
Sep 27 '07 #2
docmur
2 New Member
????Why the code is C not C++
Sep 27 '07 #3
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).
Sep 27 '07 #4

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

Similar topics

8
8244
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: ")
10
3366
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?
6
7616
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
12
2079
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...
18
25014
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.
116
4647
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
5
2583
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...
14
5555
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?
3
10673
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
0
9721
marktang
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...
1
10387
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,...
0
10120
tracyyun
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...
1
7662
isladogs
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...
0
6881
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();...
0
5550
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...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
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...

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.