Thread: simple char array question..

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    simple char array question..

    Hey all,

    I'm trying to use strcat with input as follows:

    Code:
    strcat(top, input[y]); // where y is a loop var
    top is declared as

    char top[256] = ""; // but is later filled

    and input is

    char input[256];


    (these aren't my actual variable names, however).

    Unfortunately my compiler says "Invalid conversion from 'char' to 'const char*'" (gcc in KDevelop)


    What should I do?

    Here is all of the code just in case (a work in progress!):

    Code:
     #include <iostream>
     #include <cstring>
     
     using namespace std;
    
     int main()
     {
     
        char input[256];
        char top[256] = "";
        float ftop;
        char bottom[256] = "";
        float fbottom;
        int input_length;
        bool divide = false;
        float answer;
        char *pointer;
        char *pointer2;
    
        cout << "This program adds, subtracts, multiplies and divides fractions.";
        cout << endl << "(or it will when I'm done)" << endl;
        cout << "Enter a fractional number: (i.e. 5/2)" << endl;
        cin.get(input, 256);
        input_length = strlen(input);
    
        for(int x = 0; x < input_length; x++)
        {
          if(input[x] > 48 && input[x] < 57)
          {
            for(int y = x; input[y] > 48 && input[y] < 57; y++) // take in all numerical digits
            {
              if(divide == false)
              {
                
                pointer2 = &input[y];
                strcat(top, input[y]); // puts it in top of / hasn't been encountered
              }
              if(divide == true)
              {
                
                pointer2 = &input[y];
                strcat(bottom, input[y]); // puts them in bottom if it has
              }
            }
          }
          if(input[x] == '/') divide = true;
          if(input[x] == '\0');
          else cout << "Invalid input.";
        }
    
        ftop = atoi(top);
        fbottom = atoi(bottom);
    
        answer = ftop/fbottom;
    
        cout << answer;
    
        return 0;
     }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    strcat will append a char array to a char array. Try:


    Code:
    char beta[2];
    beta[1] = '\0';
    
    
    do{
    
    beta[0] = input[i];
    strcat(alpha, beta);
    
    }while(1);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Sebastiani's code may be correct. But I didn't read all of your code. I just read the part you said that was giving you the error:

    Code:
            for(int y = x; input[y] > 48 && input[y] < 57; y++) // take in all numerical digits
            {
              if(divide == false)
              {
                
                pointer2 = &input[y];
                strcat(top, input[y]); // puts it in top of / hasn't been encountered
              }
              if(divide == true)
              {
                
                pointer2 = &input[y];
                strcat(bottom, input[y]); // puts them in bottom if it has
              }
            }
    How about changing strcat(top, input[y])--note that you are passing a char in the second parameter which is causing the error--to strcat(top, pointer2)?

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by master5001
    Sebastiani's code may be correct. But I didn't read all of your code. I just read the part you said that was giving you the error:

    Code:
            for(int y = x; input[y] > 48 && input[y] < 57; y++) // take in all numerical digits
            {
              if(divide == false)
              {
                
                pointer2 = &input[y];
                strcat(top, input[y]); // puts it in top of / hasn't been encountered
              }
              if(divide == true)
              {
                
                pointer2 = &input[y];
                strcat(bottom, input[y]); // puts them in bottom if it has
              }
            }
    How about changing strcat(top, input[y])--note that you are passing a char in the second parameter which is causing the error--to strcat(top, pointer2)?
    Hmm I feel quite stupid. I put in the pointer and then didn't even change the strcat... plus I realized that even with that code, it will only append one char at a time. I can fix that though.

    Anyways, now it always says "invalid input"...

    here's the output -

    Code:
    This program adds, subtracts, multiplies and divides fractions.
    (or it will when I'm done)
    Enter a fractional number: (i.e. 5/2)
    5/2
    Invalid input.Invalid input.Invalid input.0
    Press Enter to continue!
    Any tips? Thanks.
    Last edited by Captain Penguin; 10-14-2002 at 05:05 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    in your code you have three if statements and a single else. The else only relates to the last if statement. So if the input[i] isn't a NULL char the else statement will output the invalid input statement. You should use one if, 2 else if's, and a single else.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Hmm makes sense. I always skipped over the sections in my book explaining if's since I thought I knew them already

    Anyways, now the "answer" is always "nan". What the hell does that mean?

  7. #7
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    haven't really followed this thread but I still can contribute:
    NaN stands for 'not a number'..
    urhh..had some nice links for it but they seem to have dissapeared..

    /btq
    ...viewlexx - julie lexx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. simple char array question
    By yahn in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2006, 09:18 PM
  3. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM