0

I am trying to split a string with strtok(). I am getting floating numbers as a string and then by using atof(), i am converting them into floating point and then put them into another array. At least i am trying. My code is like:

int main() {
float new[4];
char inp[16]={"0.2 0.5 0.3 0.7"};

const char s[2] = " ";
char *token;
int i=0,j;

token=strtok(inp,s);
new[i]=atof(token);
i++;
while( token != NULL ) 
{  token = strtok(NULL, s);
  new[i]=atof(token);
  i++;
}
for(j=0;j<4;j++)
    printf("%f\n",new[j]);
} 

But i get errors like:

[Error] expected unqualified-id before 'new'
[Error] expected type-specifier before '[' token
[Error] expected type-specifier before '[' token
[Error] expected type-specifier before '[' token

Why i get these errors? And i couldn't make it work yet but is the logic true? Sorry for poor English and structure mistakes, i am a new user :)

NoWay
  • 11
  • 1
  • 5
  • 3
    You are using the C++ compiler as a C compiler. In c++, `new` can not be used as a variable name because `new` is a reserved word. – BLUEPIXY May 06 '17 at 21:29
  • Your logic change it to `token=strtok(inp,s); while( token != NULL ) { new[i++]=atof(token); token = strtok(NULL, s); }` – BLUEPIXY May 06 '17 at 21:31

3 Answers3

0

Make sure you are using a C compiler instead of a C++ one, otherwise you might have problems naming a varible new, because it is a keyword, like while in C for example.

Despite that, this changes would solve your problems:

while( token != NULL ){
  token = strtok(NULL, s);
  if(token != NULL){
    new[i]=atof(token);
    i++;
  }
}

That's because the while doesn't test if token is NULL at each instruction, only before executing the loop. So the token it's already NULL, but the program tries to do atof(token) which causes the problem

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

the following code:

  1. uses meaningful variable names
  2. performs the correct convertion (atof() returns a double, not a float
  3. does not contain 'magic' numbers
  4. uses the correct logic for using strtok()
  5. uses appropriate horizontal and vertical spacing for readability
  6. follows the axiom: only one statement per line and (at most) one variable declaration per statement.
  7. keeps data declarations local to their usage
  8. uses size_t when the value will never be < 0, rather than int type
  9. allows the compiler to calculate array sizing rather than hardcoding the value
  10. includes the necessary header files
  11. cleanly compiles
  12. Note: does not check for an error while using `strtof()

and now the proposed code

#include <string.h>   // strtok()
#include <stdio.h>    // printf()
#include <stdlib.h>   // strtof()

#define MAX_FLOATS 10

int main( void )
{
    float arrayOfFloat[ MAX_FLOATS ];
    char inp[]={"0.2 0.5 0.3 0.7"};

    const char delimiter[] = " ";
    char *token;

    size_t i=0;
    token=strtok( inp, delimiter );
    while( token )
    {
        arrayOfFloat[i] = strtof( token, NULL );
        i++;
        token = strtok( NULL, delimiter );
    }

    for( size_t j=0; j<i; j++ )
        printf("%f\n", arrayOfFloat[j]);
} // end function: main

the result of running the proposed code is:

0.200000
0.500000
0.300000
0.700000
user3629249
  • 16,402
  • 1
  • 16
  • 17
-1

it's simple , just change the 'new' to any variable name[check sample] . Don't really Know if it's a reserved word buh trust me it's the cause of your problem.

daabbey1
  • 1
  • 1
  • JUST CHANGE THE VARIABLE NAME ie 'new' to any other variable. – daabbey1 Oct 16 '18 at 15:52
  • It's a reserved word in c++, which is probably the kind of compiler they are using. [This answer](https://stackoverflow.com/a/43825667/1092820) already explains this. – Ruzihm Oct 16 '18 at 16:26