Thread: Const Char to Char Error

  1. #16
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    hmm, maybe you could use itoa()

    It should take a number like 0 and turn it into "0" i think...

    Then you could add it to the string...

  2. #17
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Lightbulb

    Quote Originally Posted by stickman
    Thanks. Now how do I add int a to blah?

    int a=0;
    blahblah=blahblah+a;

    That would try to add a and blahblah.


    Why not use the += plus/equals operator


    instead of:
    Code:
    blahblah=blahblah+a;

    could be:
    Code:
    blahblah += a;

    The plus/equals operator is basically the string class's version of the cstring "strcat()" function.


    The Brain's tip o' the day.
    Last edited by The Brain; 08-28-2004 at 10:35 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #18
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Sorry, I think my faq link was broken. Anyways check it out (I think the link works now). Here's code taken from the faq:
    Code:
    string IntToString(int num)
    {
      ostringstream myStream; //creates an ostringstream object
      myStream << num << flush;
    
      /*
       * outputs the number into the string stream and then flushes
       * the buffer (makes sure the output is put into the stream)
       */
      
      return(myStream.str()); //returns the string form of the stringstream object
    }
    It's a nice little function you can use like this:
    Code:
    //...
    int main()
    {
    std::string blah("Your number is: ");
    int num=1;
    blah+=IntToString(num);
    }
    Be sure to #include <sstream>
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #19
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    well atoi() would return an integer:
    alphanumeric to integer

    You want itoa() which isn't standard.

  5. #20
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    It said that it had to few arguement and showed me that itoa() should be used like this:

    itoa(int, char*, int)

    Which that makes no sense to me. Where do I put variable a and what should I put for the other two arguements?

  6. #21
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Quote Originally Posted by The Brain
    Why not use the += plus/equals operator


    instead of:
    Code:
    blahblah=blahblah+a;

    could be:
    Code:
    blahblah += a;

    The plus/equals operator is basically the string class's version of the cstring "strcat()" function.


    The Brain's tip o' the day.
    Yeah, that works, but it doesn't return the integer, it returns the character/symbol.

  7. #22
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by stickman
    It said that it had to few arguement and showed me that itoa() should be used like this:

    itoa(int, char*, int)

    Which that makes no sense to me. Where do I put variable a and what should I put for the other two arguements?
    http://www.cplusplus.com/ref/cstdlib/itoa.html

  8. #23
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I was under the assumption that int a was already converted to string... once 'int a' becomes 'string a' then you can concantinate using plus/equals.


    Last edited by The Brain; 08-28-2004 at 10:49 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #24
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>Here is the link to the FAQ for an easy example of how to stream from int to string.

    I guess I wasn't making it obvious enough...Or maybe no one can hear me
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #25
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Let me post here "right after" The Brains post, and not anyone else's, like JaWib, whom to my knowledge hasnt posted on this thread......




    Yes that FAQ article should be what your looking for.

  11. #26

    strings?

    since your using constant type, i believe your data already entered into the program, so why not use

    [code]
    #include <iostream>
    #include <string>
    #include <conio.h>
    using namespace std;

    int main()
    {
    string mystring = "My words here!";
    cout << mystring << endl;
    mystring.insert(13, " \'more words\' ", 0, 14);
    cout << mystring << endl;
    getch();
    }
    [\code]

    Output :

    My words here 'more words' !

    Here is a link for a refrence to insert, PDF Tutorial
    Last edited by JarJarBinks; 08-28-2004 at 11:21 PM.

  12. #27
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    I guess you guys just don't understand. I WANT IT TO RETURN AN INTEGER AND NOT A SYMBOL/CHARACTER. I tried itoa, returns symbol/character. I tried blah+=a, returns symbol/character. I want something that returns AN INTEGER. Let's do a test:

    #include <iostream>

    int main()
    {
    for (int x=0; x<10; x++)
    {
    char test;
    std::cout << "\nThe Number Is: ";
    std::cout << x;
    }
    std::cin.get();
    return 0;
    }

    That returns:

    The Number Is: 1
    The Number Is: 2
    The Number Is: 3
    The Number Is: 4
    The Number Is: 5
    The Number Is: 6
    The Number Is: 7
    The Number Is: 8
    The Number Is: 9

    Now, how do I save "The Number Is: x" to a string so I can use it later?

    P.S. Sorry if I'm going a little over edge, but nothing has been the correct output.

  13. #28
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here is the pseudocode for what ye' desire


    1) covert 'int x' to 'string x' as per the example in the FAQ. I would probably pass int x into a function intTostring( ) for example.

    Hint
    Code:
    string result = "The number is: ";
    
    result += intTostring(x);
    where intTostring( ) will accept an integer argument.. and will have a string type return value.



    2) Your "int to string" function will at least contain this syntax.. which will perform the actual conversion:

    Hint
    Code:
    myStream << x << flush;

    3) The only thing left.. is to pick your choice of data structure that will allow you to readilly store & access your new list of strings...

    Hint
    Code:
    string answer[10];
    
    for( int i = 0; i < 10; i++ )
    
         answer[i] = result;

    Hope this is somewhat close to what you are asking for
    Last edited by The Brain; 08-29-2004 at 12:02 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  14. #29
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    You must be tired, why don't you go to sleep and continue this conversation tomorrow?
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    std::string IntToString(int num)
    {
        std::ostringstream myStream; //creates an ostringstream object
        myStream << num << std::flush;
    
        /*
        * outputs the number into the string stream and then flushes
        * the buffer (makes sure the output is put into the stream)
        */
      
        return(myStream.str()); //returns the string form of the stringstream object
    }
    
    int main()
    {
        std::string blah("Your number is: ");
        std::string arr[10];
        
        for (int x = 0; x <10; x++)
        {
            arr[x] = blah + IntToString( x );
        }
    
        for (int x = 0; x <10; x++)
        {
            std::cout << arr[x] << '\n';
        }
    
        return 0;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  15. #30
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Quote Originally Posted by JaWiB
    Sorry, I think my faq link was broken. Anyways check it out (I think the link works now). Here's code taken from the faq:
    Code:
    string IntToString(int num)
    {
      ostringstream myStream; //creates an ostringstream object
      myStream << num << flush;
    
      /*
       * outputs the number into the string stream and then flushes
       * the buffer (makes sure the output is put into the stream)
       */
      
      return(myStream.str()); //returns the string form of the stringstream object
    }
    It's a nice little function you can use like this:
    Code:
    //...
    int main()
    {
    std::string blah("Your number is: ");
    int num=1;
    blah+=IntToString(num);
    }
    Be sure to #include <sstream>
    I'm sorry, I skipped this post. This is exactly what I wanted. Thanks to everyone who was so kind to help me out. Now I better get some rest. Thanks to all. Your help is much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  5. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM