473,396 Members | 2,026 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.

Decting user input for float or int

2
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 2691
sicarie
4,677 Expert Mod 4TB
Moving to C++ Forum (Please double check where you are next time. Thanks!)
Sep 27 '07 #2
docmur
2
????Why the code is C not C++
Sep 27 '07 #3
sicarie
4,677 Expert Mod 4TB
????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
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...
10
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...
6
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,...
12
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. ...
18
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,...
116
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,...
5
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...
14
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
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...
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
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: 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
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,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.