Having trouble writing code for project

This is a discussion on Having trouble writing code for project within the C++ Programming forums, part of the General Programming Boards category; I'm trying to figure out how to add a random digit and random number to a string I created. I ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    1

    Unhappy Having trouble writing code for project

    I'm trying to figure out how to add a random digit and random number to a string I created. I know how to get both, and can get both, but when it comes to attaching it, I'm screwed. I've tried strcat and strncat and neither are working for me. What am I doing wrong?????

    Code:
    		ran1 = (rand()%q) + s;
    		cout << endl << ran1 << " %%%%%%";
    		cout << endl << num[ran1] << " ^^^^^";
    		ran2 = (rand()%q1) + s;
    		cout << endl << ran2 << " $#$#$#$";
    		cout << endl << alpha[ran2] << " +_+_+_+_+";

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I believe your question is just "How do you insert an integer into a string?"

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <stdio.h>  // for sprintf
    using namespace std;
    
    int main() {
        int n = 12345;
    
        // The C++ way.
        stringstream ss;
        ss << "%%%%%% " << n << " %%%%%%";
        cout << ss.str() << '\n';
    
        // The C way.
        char str[100];    
        sprintf(str, "%%%%%% %d %%%%%%", n);
        cout << str << '\n';
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by oogabooga View Post
    I believe your question is just "How do you insert an integer into a string?"

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <stdio.h>  // for sprintf
    using namespace std;
    
    int main() {
        int n = 12345;
    
        // The C++ way.
        stringstream ss;
        ss << "%%%%%% " << n << " %%%%%%";
        cout << ss.str() << '\n';
    
        // The C way.
        char str[100];    
        sprintf(str, "%%%%%% %d %%%%%%", n);
        cout << str << '\n';
    }
    You should really be using snprintf... Sure, I doubt any integer type on any compiler may ever have enough bits to overflow the array (64-bit integers ought to be enough for everyone!), and I wouldn't find it a bad thing if you would use this in your codebase, but when teaching a newbie you should definitely recommend the "safe" version until he understands exactly what it does.

    So, to the OP:
    Use the C++ way he recommended. Do not use the C way, not yet at least. And if you ever would like to, google for "snprintf", and replace his sprintf function with that function.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Note: snprintf() is a good idea, but it's new in C99, so your C++ compiler is not guaranteed to have it. (Although most major ones will, because they also try to support C99!) In C++, it's definitely a better idea to use stringstreams.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing good code is like writing an artistic expression
    By renzokuken01 in forum C Programming
    Replies: 5
    Last Post: 02-03-2011, 08:48 PM
  2. Having trouble writing a formula
    By Neotriz in forum C Programming
    Replies: 2
    Last Post: 09-05-2009, 09:58 PM
  3. Trouble with writing a plugin
    By BobMcGee123 in forum C++ Programming
    Replies: 0
    Last Post: 09-23-2007, 03:55 PM
  4. I'm having trouble writing a class.
    By SPiRiToFCaT in forum C++ Programming
    Replies: 5
    Last Post: 05-17-2002, 07:34 AM
  5. Trouble writing to file
    By Fyodorox in forum C++ Programming
    Replies: 7
    Last Post: 05-06-2002, 07:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21