Thread: arry/character handling

  1. #1
    In The Light
    Join Date
    Oct 2001

    arry/character handling

    this is what im trying to do:
    given this type of input - 25'-5.5" (how ever this input may contain less 3'-6" or more 125'-5.75" characters)
    i want to
    remove all none digit characters
    multiply the figure before the dash by 12 - in this case 25 * 12
    add it to the figure after the dash - in this case 5.5
    so the answer in this case would be 305.5
    with this code i get as far as this:
    i can strip the non didgit stuff so i get 25 5.5
    i can atof the first figure and multiply it by 12 so i get x=300
    what i cant figure out is how to isolate the 5.5 so i can atof it - so now y always equals zero.

    this is where im at-------
    #include <stdlib.h>
    #include <iostream>
    #include <string.h>
    #include <ctype.h>
    using namespace std;
    int main()
      double x=0;
      double y=0;
      int j=0;
      char input[20];
      cout<<"Enter dimension: ";
      cout<<"Your dimension is: "<<input<<endl;
      for(int i = 0; i<strlen(input); i++)
        if(input[i]=='-' || input[i]=='\''|| input[i]=='"'){
          cout<<"Found it @: "<<i<<" position\n";
          input[i]= ' ';
      cout<<"\nAfter processing input = "<<input<<"\n";
      cout<<"X = "<<x * 12<<" inches"<<endl;
      cout<<"Y = "<<(y)<<endl;
      return 0;
    all i really want is a concept of how to do it - im trying to learn this stuff so someone else writing the code might not be as good of lesson.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    int feetcount=0;//num of digits in num of feet
    while (input[feetcount] != ' ')  feetcount++;
    int numoffeet=0;
    for ( int x=feetcount; x>=1; x--)  numoffeet+=atoi(input[x])*pow(10,x-1);
    Do this for both. If this doesn't work, sorry, I threw this together quickly b/c I have to go to bed, very tired .

  3. #3
    In The Light
    Join Date
    Oct 2001
    thanks for the try golfinguy4 i messed with it a bit and i still get the same error i have been running into
    rdash.cpp:32: cannot convert `char' to `const char *' for argument `1'
    to `atoi (const char *)'
    in reference to this line:
    for ( int x=feetcount; x>=1; x--)  numoffeet+=atoi(input[x])*pow(10,x-1);
    oh well i'll keep trying


  4. #4
    Registered User
    Join Date
    Aug 2001
    rdash.cpp:32: cannot convert `char' to `const char *' for argument `1'
    to `atoi (const char *)'

    atoi takes in a string (char*) as input.. you are passing it a char.. i'm tired too (it seems to be a trend on this post) but just make sure you pass it the whole character array, not just one character

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    atoi takes a char * as its argument, so if you are passing a single char you need to pass the address:
    for ( int x=feetcount; x>=1; x--)  
      numoffeet += atoi(&input[x]) * pow(10,x-1);
    My best code is written with the delete key.

  6. #6
    once you get

    25 5.5

    parse that string into two substrings using strtok() or a homegrown version. then call atoi() or atof() on first string and atof() on second string. Then add the two pieces together into a float (or double).

    char time[20] = "25'-5.5"";
    char sHours[6];
    char sSmin[10];
    float hours;
    float min;
    float add;
    int count = 0;
    int j = 0;

    for(i = 0; i < strlen(time); i++)
    if(count == 0)
    if(time[i] != ' ')
    sHours[j++] = time[i];
    else if(time[i] == ' ')
    sHours[j] = '\0';
    count = 1;
    j = 0;
    else if(count == 1)
    sMin[j++] = time[i];
    sMin[j] = '\0';

    hours = atof(sHours);
    min = atof(sMin);

    add = hours *60;

    min += add;
    cout << min << endl;

    Better yet, do the parsing at the time of the stripping to save yourself a bunch of steps.

  7. #7

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Handling critical errors
    By Memloop in forum C Programming
    Replies: 14
    Last Post: 03-26-2009, 06:14 AM
  3. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  4. event handling is serialized in MS Visual Studio C++ 2005 ??
    By mynickmynick in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2008, 04:47 AM
  5. Error handling
    By mepaco in forum C++ Programming
    Replies: 12
    Last Post: 09-06-2002, 10:45 AM