Solved

# Can't convert string to integer

Posted on 2005-05-11
Medium Priority
188 Views
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
Question by:igor92128
• 3
• 2
• 2
• +2

LVL 86

Accepted Solution

jkr earned 2000 total points
ID: 13983471
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

LVL 86

Expert Comment

ID: 13983493
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

Author Comment

ID: 13983506
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

Author Comment

ID: 13983528
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

Author Comment

ID: 13983589
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

LVL 4

Expert Comment

ID: 13983950
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

LVL 4

Expert Comment

ID: 13983965

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

Expert Comment

ID: 13992483
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

Expert Comment

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

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

LVL 14

Expert Comment

ID: 15623456
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a Gā¦
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a ā¦
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor anā¦
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month14 days, 9 hours left to enroll