• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

Can't convert string to integer

I'm having trouble converting a string to an integer and I get an error on the last line when I try to print total.

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

      cont = atoi(sCurrency);
      total = cont - pol;

      iSummary = "\n\nYou are carrying " + sCurrency + " gold." + "\n";
      iSummary += "Your profit/loss is " + total + " gold." + "\n";

Thanks,
Igor
0
igor92128
Asked:
igor92128
  • 3
  • 2
  • 2
  • +2
1 Solution
 
jkrCommented:
You should rather use a stringstream for that:

include <sstream>
using namespace std;

string Student::printInventory()
{
    stringstream ssSummary;
    int pol = 1000, cont, total;
    int i = 1;
    string iSummary;  
    string sCurrency;
    string gSize;
    stringstream temp;
    temp << i;

     vector <Good*>::iterator goodsIterator;
 
     ostringstream outs1;      
     outs1 << currency;            
    sCurrency = outs1.str();      

    cont = atoi(sCurrency);
    total = cont - pol;

    ssSummary << "\n\nYou are carrying " << sCurrency << " gold." << "\n";
    ssSummary << "Your profit/loss is " << total << " gold." << "\n";

    return ssSummary.str();
}

A 'string' cannot add integers like 'total'.
0
 
jkrCommented:
Sorry, that should have been

string Student::printInventory()
{
   stringstream ssSummary;
   int pol = 1000, cont, total;
   int i = 1;
   string iSummary;  
   string sCurrency;
   string gSize;
   stringstream temp;
   temp << i;

    vector <Good*>::iterator goodsIterator;

     ostringstream outs1;      
     outs1 << currency;            
   sCurrency = outs1.str();      

   cont = atoi(sCurrency.c_str()); // the '.c_str()' was missing
   total = cont - pol;

   ssSummary << "\n\nYou are carrying " << sCurrency << " gold." << "\n";
   ssSummary << "Your profit/loss is " << total << " gold." << "\n";

   return ssSummary.str();
}
0
 
igor92128Author Commented:
But sCurrency actually is a number. All I need to do is convert it to an integer and subtract 1000 from it to display the profit/loss.
So would I need to convert the integer back to a string after this calculation is done, so I can add it in the string?

Igor
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
igor92128Author Commented:
I tried what was in your second post, but it produced many errors. Then I modified the program some more and I have this now, the whole function this time:
string Student::printInventory()
{
      int pol = 1000, cont, total;
      int i = 1;
      string iSummary;       
      string sCurrency;
      string gSize;
      stringstream temp;
      temp << i;
 
      vector <Good*>::iterator goodsIterator;
   
      ostringstream outs1;
      ostringstream outsx;
      outs1 << currency;             
      sCurrency = outs1.str();      

      cont = atoi(sCurrency.c_str());
      total = cont - pol;
      total = outsx.str(); <-- error here (can't convert)

      iSummary = "\n\nYou are carrying " + sCurrency + " gold." + "\n";
      iSummary += "Your profit/loss is " + total + " gold." + "\n"; <-- error here (cant add pointers)
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("");
                  temp << i;
         
                  iSummary = iSummary + temp.str() + ". " + (*goodsIterator)->print();
                  i++;                   
      }
      return iSummary;
}

Igor
   
0
 
igor92128Author Commented:
I'm using "cout << total" and it works great. Unless a solution with strings can be found, I will just use cout...

Thanks,
Igor
0
 
furqanchandioCommented:
hi

well if really want to use atoi() you have to use C strings ie char currency[].

as atoi() dosn't take string class objects as arguments
0
 
furqanchandioCommented:

well one quick and dirty approach will be


char Ccurrency[12];

for (int i=0; i<=sCurrency.size(); i++)
Ccurrency[i]=sCurrency[i];

Ccurrency[i]=0;  //Null Terminate the Cstring

contl=atoi(Ccurrency);
0
 
PositCommented:
Here is a template function I keep among my general purpose utility functions.  It must reside in a header file to work, but works for all built-in numeric types (integers, floats, doubles, etc.).  A non-template version that works just for integers is given after it; the only advantage of the latter is that it can reside in a .cpp file.  For both, you should #include <sstream> in the .cpp file that calls the function.

template <class T> std::string NumberToString(T num)
{
  stringstream ss;      //declare object of stringstream class
  ss << num;      //put the integer into it
  string str("");      //declare a string
  ss >> str;      //put stringstream integer into it
  return str;
}

std::string IntToString(int num)
{
  stringstream ss;      //declare object of stringstream class
  ss << num;      //put the integer into it
  string str("");      //declare a string
  ss >> str;      //put stringstream integer into it
  return str;
}



0
 
PositCommented:
Of course, since your question was about a conversion the other way, my last reply was rather off point.

In your code, the line

     cont = atoi(sCurrency);

is trying to convert the string object sCurrency to an integer using atoi().  The function atoi() takes a C-style string as an argument, not a string object.  Fortunately, string objects have a function to return a C-style string of themselves.  So rewriting the line as

     cont = atoi(sCurrency.c_str())

should do the trick.
0
 
waysideCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: jkr

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

wayside
EE Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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