Thread: writing strings chars from ints

  1. #1
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31

    writing strings chars from ints

    hey all,
    trying to do the following:
    Write a string that has a row of characters, but these characters are supposed to from integers (but must be written as characters), and I get the following error:

    invalid operands `const char *' and `const char[2]' to binary `operator +'

    I also get (on a different line):

    passing `char' to argument 1 of `fputs(const char *, FILE *)' lacks a cast.

    I presume these two errors are related (and I get get the syntax for casting from the faq), but I'm lost as to the first error.

    THX
    Deum solum fidentia est

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Code plz.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  3. #3
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31
    oops, forgot to add it:

    FILE *ci;
    ci=fopen("c:\\vgastrat\\TESTcws.ini", "w");
    int cc=rand()%40;
    char ccq;
    ccq= int (cc);
    int u=rand()%500;
    int e=rand()%4;
    int s=rand()%9;
    char uq= int (u);
    char eq= int (e);
    char sq= int (s);
    fputs(ccq+","+uq+","+eq+","+sq, ci); // error occurs here


    Hope that's not too cryptic
    Deum solum fidentia est

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    You can't add strings like that, not char* strings. Use strcat instead.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Use code tags.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31
    FILE *ci;

    ci=fopen("c:\\vgastrat\\TESTcws.ini", "w");

    int cc=rand()%40;

    char ccq;
    ccq= int (cc);

    int u=rand()%500;
    int e=rand()%4;
    int s=rand()%9;

    char uq= int (u);
    char eq= int (e);
    char sq= int (s);

    fputs(ccq+","+uq+","+eq+","+sq, ci); // error occurs here



    Hope this is more acceptable
    Deum solum fidentia est

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    No, sorry, that's still not doing it for me. Actually READ the code tags link I sent you, then you'll see what I mean.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31
    Code:
         FILE *ci;
    
         ci=fopen("c:\\vgastrat\\TESTcws.ini", "w");
    
         int cc=rand()%40;
    
         char ccq;
         ccq= int (cc);
    
         int u=rand()%500;
         int e=rand()%4;
         int s=rand()%9;
    
        char uq= int (u);
        char eq= int (e);
        char sq= int (s);
    
        fputs(ccq+","+uq+","+eq+","+sq, ci); // error occurs here
    Sorry all (especially bennyandthejets], I'm just not with it tonight.
    Deum solum fidentia est

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    No problem. As for the problem, I don't understand what the function int() is. Is that even a legal function name?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31
    Code:
        char uq= int (u);
        char eq= int (e);
        char sq= int (s);
    Ok, my amateurness is showing through:
    I thought that is how one can type cast from int to char.
    Deum solum fidentia est

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You would do that like this:

    Code:
    char *uq=new char[8];
    itoa(u,uq,10);
    delete [] uq;
    The first line creates a pointer to a char, then makes it point to an 8 byte memory block.
    The second line converts u to a string and stores it in uq, using the base 10 number system (decimal ).
    The third line deallocates the memory block so that you don't get memory leaks. Only delete [] after you finish with the string.

    Those three lines you wrote should flag a compiler error I'm pretty sure.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #12
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    not necessarily dude!


    >Ok, my amateurness is showing >through:
    >I thought that is how one can type >cast from int to char.
    ummm...

    >You would do that like this:

    >code:
    >char *uq=new char[8];
    >itoa(u,uq,10);
    >delete [] uq;
    It is not necessary to carry out C-ish style in C++...unless one is destined to do so.

    Code:
    #include <iostream>
    using namespace std;
    int main(std::string argc , int argv) {
    int u = 90;
    char uq= (int) u; // you were doing fine 
    cout << uq;         //deeluw. prints 'z'
    system("pause");
    return 0;
    }
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    But doesn't deleeuw want to convert the integer to a string? Ie, if int i=50, he/she wants the string to be "50". That's what my code does.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by bennyandthejets
    No problem. As for the problem, I don't understand what the function int() is. Is that even a legal function name?
    i = int(c); is the exact same as i = (int) c;. It's a cast.

    Of course, it's not what he wants to do, but it's legal nonetheless.

  15. #15
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    But doesn't deleeuw want to convert the integer to a string? Ie, if int i=50, he/she wants the string to be "50". That's what my code does.
    The C way

    The C++ way
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Question about strings n chars
    By cszym001 in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2008, 05:09 PM
  4. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM