Need Help for printing concatenating string ..

Discussion in 'C++' started by ssmile03@gmail.com, Nov 3, 2013.

  1. Guest

    All,

    Need HELP for below wrong output -


    ---
    #include <iostream>
    #include <sstream> // for ostringstream
    #include <string>


    using namespace std;

    int main()
    {
    std::string name = "HELP";
    string age = "01";
    int count[10];
    for (int x = 0; x < 5; x++) {
    int result = 5;
    result += count[x];
    std::eek:stringstream out;
    out << "The value is .." << name << age << result;
    std::cout << out.str() << endl;
    }
    return 0;
    }
    --

    The output is as -

    The value is ..HELP012
    The value is ..HELP011
    The value is ..HELP014198113
    The value is ..HELP011
    The value is ..HELP011

    while the required output should be -

    The value is ..HELP011
    The value is ..HELP012
    The value is ..HELP013
    The value is ..HELP014
    The value is ..HELP015

    Thanks!!
     
    , Nov 3, 2013
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > All,
    >
    > Need HELP for below wrong output -


    Please don't multi-post!

    --
    Ian Collins
     
    Ian Collins, Nov 3, 2013
    #2
    1. Advertising

  3. GGolf Guest

    On 11/3/2013 9:55 AM, wrote:
    > <snip>
    > int main()
    > {
    > std::string name = "HELP";
    > string age = "01";
    > int count[10];
    > for (int x = 0; x < 5; x++) {
    > int result = 5;
    > result += count[x];
    > std::eek:stringstream out;
    > out << "The value is .." << name << age << result;
    > std::cout << out.str() << endl;
    > }
    > return 0;
    > }
    >

    What's wrong with simply using x+1 instead of result? You wouldn't need
    either the count array or result in that case.
     
    GGolf, Nov 6, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. c

    Help me with Concatenating strings

    c, Sep 24, 2006, in forum: C Programming
    Replies:
    21
    Views:
    626
    Chris Torek
    Oct 15, 2006
  2. arnuld
    Replies:
    8
    Views:
    560
    Old Wolf
    Feb 8, 2007
  3. A B
    Replies:
    15
    Views:
    1,639
    Jorgen Grahn
    Nov 11, 2010
  4. Rapier
    Replies:
    11
    Views:
    174
    Anno Siegel
    Oct 14, 2004
  5. dewed

    stupid concatenating string question

    dewed, Nov 24, 2008, in forum: Javascript
    Replies:
    3
    Views:
    109
    Thomas 'PointedEars' Lahn
    Nov 24, 2008
Loading...

Share This Page