Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

Quick Question about Iterating

Hi,

Here is some sample code:

string Student::printInventory()
{
      string iSummary;  
      string sCurrency;
      string gSize;
      int i = 1;
   
      vector <Good*>::iterator goodsIterator;
   
      ostringstream outs1;        
      outs1 << currency;             
      sCurrency = outs1.str();      

      iSummary = "\n\nYou are carrying " + sCurrency + " gold." + "\n";
   
      ostringstream outs2;
      outs2 << (unsigned int)goods.size();      
      gSize = outs2.str();      
   
      iSummary = iSummary + "You are carrying " + gSize + " goods:" + "\n";

      for(goodsIterator = goods.begin(); goodsIterator != goods.end(); goodsIterator++)
      {
---------->            iSummary = iSummary + i + " " + (*goodsIterator)->print();
                           (this variant worked: iSummary += (*goodsIterator)->print();)
            i++;
      }

      return iSummary;
}

In the line I pointed out, I can't compile once I add the i. I want to be able to print the contents of each iteration along with the line number, but for some reason I can't concatenate with integers.

Any ideas,
Igor
0
igor92128
Asked:
igor92128
  • 7
  • 5
  • 3
  • +1
2 Solutions
 
nayernaguibCommented:
Looks like you cannot add an integer to a string without conversion, and the statement above seems to cause a type mismatch error.
Convert the integer value stored in i into a string and use it in your statement.

What compiler are you using?

_________________

    Nayer Naguib
0
 
Jaime OlivaresSoftware ArchitectCommented:
You need something like this;:

string Student::printInventory()
{
     string iSummary;  
     string sCurrency;
     string gSize;
     int i = 1;
   
     vector <Good*>::iterator goodsIterator;
   
     ostringstream outs1;      
     outs1 << currency;            
     sCurrency = outs1.str();      

     iSummary = "\n\nYou are carrying " + sCurrency + " gold." + "\n";
   
     ostringstream outs2;
     outs2 << (unsigned int)goods.size();      
     gSize = outs2.str();      
   
     iSummary = iSummary + "You are carrying " + gSize + " goods:" + "\n";

     for(goodsIterator = goods.begin(); goodsIterator != goods.end(); goodsIterator++)
     {
          outs2.clear();  // reuse this stream
          outs2 << i <<  (*goodsIterator)->print();  
          iSummary += outs2.str();
          i++;
     }

But will be cleaner to make iSummary a stringstream and stream all to it
0
 
Jaime OlivaresSoftware ArchitectCommented:
Here is my proposal, it is more C++ style, have not tested but must work:

string Student::printInventory()
{
     ostringstream iSummary;  
   
     iSummary <<  endl << endl;
     iSummary << "You are carrying" << currency << "gold" << endl;
     iSummary << "You are carrying" << goods.size(); << "goods:" << endl;

     int i = 1;
     for(vector <Good*>::iterator goodsIterator = goods.begin(); goodsIterator != goods.end(); goodsIterator++)
          iSummary << i++ <<  (*goodsIterator)->print();  

     return iSummary.str()
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
InteractiveMindCommented:
Hello Igor,

C++ is a bitch when it comes to appending integers to strings. So, you need to cast the int to a string, here's a neat way to do this:


#include <sstream>
...

stringstream temp;
temp << i;
iSummary += temp.str() + " " + (*goodsIterator)->print();



Hope that helps.  :-)
Rob.
0
 
igor92128Author Commented:
I'm using VC++.NET
Jaime, your suggestion helps, but it seems to be printing out the inventory many times but at least it's being numbered now. Your second post gave an error, but this is what I changed the entire function to. Rob, your suggestion worked, but it won't iterate the number. Here is my function now:

string Student::printInventory()
{
      int i = 1;
      string iSummary;  
      string sCurrency;
      string gSize;
      stringstream temp;
      temp << i;
 
      vector <Good*>::iterator goodsIterator;
   
      ostringstream outs1;        
      outs1 << currency;             
      sCurrency = outs1.str();      

      iSummary = "\n\nYou are carrying " + sCurrency + " gold." + "\n";
   
      ostringstream outs2;
      outs2 << (unsigned int)goods.size();      
      gSize = outs2.str();      
   
      iSummary = iSummary + "You are carrying " + gSize + " goods:" + "\n";

      for(goodsIterator = goods.begin(); goodsIterator != goods.end(); goodsIterator++)
      {
            
            iSummary = iSummary + temp.str() + " " + (*goodsIterator)->print();
            i++;
            
      }
      return iSummary;
}

And my output:

You are carrying 1000 gold.
You are carrying 9 goods:
1 Glass bowl, Accient glass bowl Cost: 10
1 Cotton linen, A bolt of white cotton linen Cost: 10
1 Red Silk, A bolt of red silk Cost: 50
1 Palm tree, A Sweet date palm Cost: 25
1 Insense, Jasmine incense Cost: 10
1 Tapestry, Gold-laced Turkish Tapestry Cost: 50
1 Saphire, A Saphire stone Cost: 75
1 Cabbage, Red Kohirabi Cabbage Cost: 10
1 Almonds, Bag of almonds Cost: 5

Even if I set i to be a global, its still producing a 1.

Igor
0
 
Jaime OlivaresSoftware ArchitectCommented:
Sorry, in my first post, clear() will not work as expected, using your last example:

string Student::printInventory()
{
     int i = 1;
     string iSummary;  
     string sCurrency;
     string gSize;
     stringstream temp;
 
     vector <Good*>::iterator goodsIterator;
   
     ostringstream outs1;      
     outs1 << currency;            
     sCurrency = outs1.str();      

     iSummary = "\n\nYou are carrying " + sCurrency + " gold." + "\n";
   
     ostringstream outs2;
     outs2 << (unsigned int)goods.size();      
     gSize = outs2.str();      
   
     iSummary = iSummary + "You are carrying " + gSize + " goods:" + "\n";

     for(goodsIterator = goods.begin(); goodsIterator != goods.end(); goodsIterator++)
     {
         temp.str("");  // clear buffer
           temp << i;
         
          iSummary = iSummary + temp.str() + " " + (*goodsIterator)->print();
          i++;
         
     }
     return iSummary;
}

But I still suggest to use some like my second proposal....
0
 
InteractiveMindCommented:
You're incrementing i, but not recasting it. Try this instead:


string Student::printInventory()
{
     int i = 1;
     string iSummary;  
     string sCurrency;
     string gSize;
     stringstream temp;
     temp << i;
 
     vector <Good*>::iterator goodsIterator;
   
     ostringstream outs1;      
     outs1 << currency;            
     sCurrency = outs1.str();      

     iSummary = "\n\nYou are carrying " + sCurrency + " gold." + "\n";
   
     ostringstream outs2;
     outs2 << (unsigned int)goods.size();      
     gSize = outs2.str();      
   
     iSummary = iSummary + "You are carrying " + gSize + " goods:" + "\n";

     for(goodsIterator = goods.begin(); goodsIterator != goods.end(); goodsIterator++)
     {
         
          iSummary = iSummary + temp.str() + " " + (*goodsIterator)->print();
          i++;
          temp.clear();
          temp << i;
     }
     return iSummary;
}
0
 
InteractiveMindCommented:
..Same thing as Jaime's.
0
 
Jaime OlivaresSoftware ArchitectCommented:
temp.clear(); won't work as I mentioned.
0
 
InteractiveMindCommented:
> temp.clear(); won't work as I mentioned.
Oh. Why not? It worked for me during testing...

Cheers Jaime.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Have tested may times with DevCpp and doesn't clear the buffer, that's why I switched to temp.str("");
0
 
Jaime OlivaresSoftware ArchitectCommented:
About my second proposal with error, it has a typo. Hope there are not more.

string Student::printInventory()
{
     ostringstream iSummary;  
   
     iSummary <<  endl << endl;
     iSummary << "You are carrying" << currency << "gold" << endl;
     iSummary << "You are carrying" << goods.size() << "goods:" << endl;   // had an extra semicolon

     int i = 1;
     for(vector <Good*>::iterator goodsIterator = goods.begin(); goodsIterator != goods.end(); goodsIterator++)
          iSummary << i++ <<  (*goodsIterator)->print();  

     return iSummary.str();   // missed semicolon here
}

0
 
igor92128Author Commented:
Hi InteractiveMind,

Your variant is almost the same, but it produced numbering like this in the output:

1 Glass bowl, Accient glass bowl Cost: 10
12 Cotton linen, A bolt of white cotton linen Cost: 10
123 Red Silk, A bolt of red silk Cost: 50
1234 Palm tree, A Sweet date palm Cost: 25
...

Jaime, you code exactly as I need.

Thanks to everyone for helping.
I'm gonna be working on this project all weekend so expect more questions from me if I get stuck.

Thanks,
Igor
0
 
InteractiveMindCommented:
Fair enough  :-)  Good luck with it.

Rob.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Still I like more my second proposal :(
0
 
igor92128Author Commented:
The answer I accepted was the one that worked.

BTW, this project is for my Design Patterns class.
Singleton and iterator are easy, but I need to implement two more patterns.
That's the whole point of the game (gradewise).

Igor
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now