Thread: Problem with conversion with stringstream ?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    59

    Problem with conversion with stringstream ?

    I am using rand() to put random numbers between 1 - 10 and then I am counting how many of each of these numbers that appeard.
    After this I am converting them to std::string and finally I try to display for:
    a0 and a1 wich counts the numbers 1 and 2 how many of these that appeard.

    Strange thing here is that when displaying the answer in the MessgeBox I could get number like, 506 or 1321564 or 477610 etc...
    At maximum it should display 20 as this is the count of numbers that I put in vector FirstNumbers ?
    I really wonder what I am missing out ?


    Code:
    //Declare variables
    std::vector<int> FirstNumbers;
    int RandomNumber = 0;
    int Num0,Num1,Num2,Num3,Num4,Num5,Num6,Num7,Num8,Num9,Num10,Num11,Num12,Num13,Num14,Num15,Num16,Num17,Num18,Num118,Num19,Num20;
    std::string a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19;
    			
    //Put Random Numbers beween 0-10 to vector
    for( int i = 0; i < 20; i++)
    {
    	 RandomNumber = rand() &#37; 10; //1 - 10 Random
    	 FirstNumbers.push_back(RandomNumber);
    }
    	
     /*...........................................................................................*/
    
    
    //Count how many appearences of each Number
    for( std::vector<int>::iterator it = FirstNumbers.begin(); it != FirstNumbers.end(); it++)
    {					
    int Dummy5 = *it;
    //Count Numbers;
    if(Dummy5 == 0){Num0 = Num0+1;}if(Dummy5 == 1){Num1 = Num1+1;}if(Dummy5 == 2){Num2 = Num2+1;}if(Dummy5 == 3){Num3 = Num3+1;}if(Dummy5 == 4){Num4 = Num4+1;}
    if(Dummy5 == 5){Num5 = Num5+1;}if(Dummy5 == 6){Num6 = Num6+1;}if(Dummy5 == 7){Num7= Num7+1;}if(Dummy5 == 8){Num8 = Num8+1;}if(Dummy5 == 9){Num9 = Num9+1;}
    if(Dummy5 == 10){Num10 = Num10+1;}if(Dummy5 == 11){Num11 = Num11+1;}if(Dummy5 == 12){Num12 = Num12+1;}if(Dummy5 == 13){Num13 = Num13+1;}if(Dummy5 == 14){Num14 = Num14+1;}
    if(Dummy5 == 15){Num15 = Num15+1;}if(Dummy5 == 16){Num16 = Num16+1;}if(Dummy5 == 17){Num17 = Num17+1;}if(Dummy5 == 18){Num18 = Num18+1;}if(Dummy5 == 19){Num19 = Num19+1;}if(Dummy5 == 20){Num20 = Num20+1;}
    }
    
    //Convert to std::string				     
    stringstream b1; b1 << Num0; a0 = b1.str(); 
    stringstream b2; b2 << Num1; a1 = b2.str(); 
    stringstream b3; b3 << Num2; a2 = b3.str(); 
    stringstream b4; b4 << Num3; a3 = b4.str(); 
    stringstream b5; b5 << Num4; a4 = b5.str(); 
    stringstream b6; b6 << Num5; a5 = b6.str(); 
    stringstream b7; b7 << Num6; a6 = b7.str(); 
    stringstream b8; b8 << Num7; a7 = b8.str(); 
    stringstream b9; b9 << Num8; a8 = b9.str(); 
    stringstream c10; c10 << Num9; a9 = c10.str(); 
    stringstream c11; c11 << Num10; b10 = c11.str(); 
    stringstream c12; c12 << Num11; b11 = c12.str(); 
    stringstream c13; c13 << Num12; b12 = c13.str(); 
    stringstream c14; c14 << Num13; b13 = c14.str(); 
    stringstream c15; c15 << Num14; b14 = c15.str(); 
    stringstream c16; c16 << Num15; b15 = c16.str(); 
    stringstream c17; c17 << Num16; b16 = c17.str(); 
    stringstream c18; c18 << Num17; b17 = c18.str(); 
    stringstream c19; c19 << Num18; b18 = c19.str(); 
    stringstream d20; d20 << Num19; b19 = d20.str(); 
       
    				 
    
    String^ s1 = gcnew String(a0.c_str());
    MessageBox::Show(s1);
    			 
    					
    String^ s2 = gcnew String(a1.c_str());
    MessageBox::Show(s2);
    Last edited by franse; 11-28-2008 at 12:28 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh my goodness. Use arrays, dude. Use arrays!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Looks like .NET as well which even though I have a huge book on...have no interest in.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It looks like the variables are all uninitialized...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    59
    Yes ofcourse, I should have used arrays/vectors. This works fine now.
    Thanks for direction.

    Code:
    stringstream b1;
    std::vector<int> Num(20);
    std::vector<string> a(20);
    for( int i = 0; i < 20; i++ )
    {
    		b1.str("");
    		stringstream b1; b1 << Num[i]; a[i] = b1.str(); 
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That was about, what 100 lines reduced to about... 8? Arrays really do work wonders, do they not?
    And thankfully, vectors initialized its elements to 0. Lucky for you.
    Unlucky for you when you thought ints would, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    59
    They do wonder really, this simplified the code very much.
    I will think about that in the future code I am doing to

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM
  5. Replies: 2
    Last Post: 02-07-2002, 09:39 AM