Its been a while now and im still trying to get a certain code to work. I asked some question about different commands etc. before, but now I hope this is ... |
I understand that strtol and strtof are preferred to atoi/atof, since the former detect errors, and also strtol is much more flexible than atoi when it comes to non-base-10.
But I'm still ... |
Hopefully this is a very simple question. Following is the C pgm (test.c) I have.
#include <stdio.h>
//#include <stdlib.h>
int main (int argc, char *argv[]) {
int intValue = atoi("1");
...
|
4. atof cboard.cprogramming.comThat version works for me, however this is my output for the file im working on. Code: $ ./a.out | A | B | C | D | E | F | G | H | I | --------------------------------------------------------------------------------- 1 | | | | | | | | | | --------------------------------------------------------------------------------- 2 | | | | | | | | | ... |
|
|
7. atof problem cboard.cprogramming.com |
|
|
|
|
|
|
|
14. atof error cboard.cprogramming.com |
int main(int argc, char *argv[]) { float converted[argc]; float result; . . . for ( i = 1; i < argc; i++ ) /* Skip program name */ converted[i - 1] = atof ( argv[i] ); . .// and here some mathematical function which could perform basic operation will be called .//by sending the converted[], &result . . printf("%f",result); |
#include #include #include "psapi.h" #include int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char * lpCmdLine, int nCmdShow) { char szProcessName[MAX_PATH] = ""; char separatori[] = " "; char setipath[MAX_PATH]; double tmp; char *s; int attivo; int inattivo; char *seti; char *att; char *inatt; seti = strtok( lpCmdLine, separatori ); att = strtok( NULL, separatori ); inatt = strtok( NULL, ... |
|
On the surface, I don't see anything that would cause atof's weird behavior. Except that you didn't #include atof's header file, stdlib.h . The effects of that vary from compiler to compiler. Some compilers will go ahead and link the function in correctly, while others won't. Most compilers will complain about having to implicitly declare the function, usually with a warning ... |
Quote: Originally Posted by jwdonahue First of all; atol(s1/100) is never going to work. You can't divide a string by a number like that. It might compile and then take the pointer value of s1 and divide that by 100 which is nothing like what you really want to achieve. atol(s1) / 100 should do the trick. Second, put that logic ... |
|
21. atof error forums.devshed.comI have an error i think it has something to do with my atof function i have attached the file. Code: void addStock(TennisStoreType* ts) { char tmpDescription[DESCRIPTION_MAX + 1]; char tmpunitPricePtr[10]; char tmpstockLevelPtr[PRICE_COLWIDTH]; float tmpPrice; int tmpStock[STOCKLEVEL_MAX]; int finished = FAILURE; StockNodeType* curStock = ts -> headStock; StockNodeType* newStock; StockNodeType* prevStock; do { printf("Description: (1-40 characters)\n"); fgets(tmpDescription, DESCRIPTION_MAX + 2, stdin); ... |
|
With no comma or space AFTER the last token you could have any number of things in there including invisible characters like a soft EOF. Do a strlen on it to give you a clue. You can also walk through it with a char pointer and print the junk out as hex values, if you'd like to get a look at ... |
#include #include #include #include static int top = -1; static double stack[50]; void push(double d) { top++; stack[top]=d; } double pop() { double p; p = stack[top]; top--; return p; } int main(int argc, char *argv[]) { int i; double a, b, r; static double stack[50]; static int top = -1; for(i=1; i |
#include #include #include #include #include #include #include #include #include #include #include static char * string_tok( char *str) { static char *next = NULL; if(str == NULL) { str = next; } else { next = str; } if(str == NULL) { return NULL; } while(next && next[0] != '\n' && ... |
|
|
|
|
A string in C is simply an array of characters, with the final character set to the NUL character (ascii/unicode point 0). This null-terminator is required; a string is ill-formed if it isn't there. The string literal token in C/C++ ("string") guarantees this. const char *str = "foo";is the same as const char *str = {'f', 'o', 'o', 0}; ... |