Strings and adding integers and restoring

Hi! I've already posted this and didn't get the results I was looking for.

Goals: for this program, I am having problems A) entering integers into a string's element and B)pulling out the integer and adding it to what the user inputs, and restoring it.

Here is the code that I'm having some problems with:

A)
1
2
3
4
5
6
7
8
9
10
11
const int CookieSales = 8;
	string a[CookieSales];
	string b[CookieSales];
	string c[CookieSales];
	string d[CookieSales];
	string e[CookieSales];
	string f[CookieSales];
	string g[CookieSales];
	string h[CookieSales];
	string l[CookieSales];
	string m[CookieSales];

B)
1
2
3
4
5
6
7
8
9
10
11
cout << "\n\n" << CookieNames[0] << " = " << a[0];
Sales();

cout << "Boxes sold: ";
cin >> sales;

//prevsales += sales;

//a[0] = prevsales;

cout << "\nNew records for " << CookieNames[0] << " are " << a[0] << ".\n\n";

Here is a webpage that has all of the code that I have thus far if any one needs to see the whole thing: http://markc81896.hostzi.com/Cookie%20Counter%204.html

Note: I've already been told to do both of these:

Here is how to store a number.
a[0] = 3;

Here is how to store the value of an already existing int.
a[0] = someInt;

Neither way worked, but they didn't see the whole program.


Later on I'll end up cleaning it up. A lot of the commented out code is there so I don't try something that I've already tried and it didn't work.

Thanks in advance!
If you want to store an int into a string you need to store it as "3" and not 3. If the int is in a variable you can try casting to a string.


a[0] = static_cast<string>(someInt);


Ok, so I entered in the code to cast it to the string and it didn't like it.

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "\n\n" << CookieNames[0] << " = " << a[0];
Sales();

cout << "Boxes sold: ";
cin >> sales;

prevsales = a[0];

sales += prevsales;

a[0] = static_cast<string>(prevsales);

cout << "\nNew records for " << CookieNames[0] << " are " << a[0] << ".\n\n";


Reason for red underline: No suitable constructor exists to convert "char" to "std::basic_string<char, std::char_traits<char>,std::allocator<char>>

Not entirely sure what that means. I tried to do as int sales and then char sales. int sales has the exact same problem.

If I was supposed to enter in a number for someInt in your code, how would I do that when it's supposed to do it while the program is running?

Can I use getLine and input getLine to the string? Or maybe a[0] >> sales?

Just wondering. Thanks and sorry!
Gee, that code must be difficult to work with.

As for converting a double into an std::string I think the most simple way for you would be to use something like this:
1
2
3
4
5
6
7
8
9
#include <sstream>

template<class T>
std::string itoa(T n)
{
    std::ostringstream ss;
    ss << n;
    return ss.str();
}


By making it a function template, you can also convert ints or other types.

Also check out: http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/
Ok thanks! I'll give that a shot. And it's not too bad, but I ran into my teacher today before class and she had the same reaction. But later on I'll condense it and use other stuff. 1st, get it to work, then condense it and make it still work. stub programming is being used a lot. :)

Thanks again!
Topic archived. No new replies allowed.