String to Const Int?

Mar 30, 2014 at 6:01pm
Is there a command/commands to convert a string to a const int? Possibly similar to:

 
std::stoi
Mar 30, 2014 at 6:08pm
What's wrong with std::stoi?

I suppose for C strings (and/or if you have a compiler that doesn't support std::stoi yet), you could use std::atoi...
Last edited on Mar 30, 2014 at 6:10pm
Mar 30, 2014 at 6:14pm
Well, I thought that stoi would only convert to a regular int? Am I incorrect?
Mar 30, 2014 at 6:34pm
I don't think that really makes any difference, especially since it's just int (a fundamental type) we're talking about, and not something like a class or otherwise.

After all, this compiles:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int foo()
{
    return 5;
}
const int bar()
{
    return 10;
}

int main()
{
    int a = foo(); // Works as expected -- a is 5
    const int b = foo(); // Adding 'const'? Whatever

    const int c = bar(); // Okay, c is 10
    int d = bar(); // Still allowed -- after all, we're just making a copy here
    d = 3; // 'd' isn't any different from any other 'int', so no problem with this...
}
Mar 30, 2014 at 6:48pm
I'm using a Const Int, because string arrays require them, throws an error when I use an Int. I am reading a string from a file and need to make it Const Int to use with my string array.

Anyways, that code doesn't seem to use stoi anywhere, it proves nothing.
Mar 30, 2014 at 6:52pm
I'm also running into the problem that Const Int's are not modifiable. Any help here would be great.
Mar 30, 2014 at 7:21pm
There is a set of classes istringstream/ostringstream that allows you to write and read from strings as if they were streams like cin or cout.

This means that you can read an 'int' into a string and extract an 'int' from a string using the << or >> operators, respectively.

I hope this helps.

Joe - C++ tutor
Last edited on Mar 30, 2014 at 7:22pm
Mar 30, 2014 at 7:23pm
Thank you! I will make sure to look into that, and see if it can help me!
Mar 30, 2014 at 8:23pm
You could do something like this:
1
2
3
4
5
6
7
8
9
#include <iostream>

int main ( )
{
    int i;
    std::cin >> i;

    int arr[static_cast<const int>(i)];
}
Mar 30, 2014 at 8:58pm
Unfortunately, I get an error saying that i must have a constant value....
Mar 30, 2014 at 8:59pm
You should use a std::vector for size-varying arrays.
Size variation isn't just "Start with 8, resize to 9".
Just a "Start with N" requires size variation.

1
2
3
4
5
6
7
int main()
{
    int n = (rand()%1024)+1;
    std::vector<float> array;
    array.resize(n);
    array[0] = 0.1f;
}
Mar 30, 2014 at 9:03pm
Vector is undefined, do I need a header?
Mar 30, 2014 at 9:05pm
#include <vector>
Mar 30, 2014 at 9:09pm
Ok, well earlier, I was reading on another similar problem I am having, I believe that vector might fix it, but because I couldn't figure out how to create a vector array, I couldn't try it.

How can I use this to loop through reading a file and assign the value of each line to the vector array.

I remember the article said something about .push_back, unsure if that will help you know what I mean.
Last edited on Mar 30, 2014 at 9:10pm
Mar 30, 2014 at 9:20pm
Yes, you can .push_back on a vector.

1
2
3
4
5
6
7
8

std::vector<std::string> content;
std::string this_line;
while( read_your_line )
{
    content.push_back(this_line);
// adds a this_line at the end of the vector, and increases its size.
}
Mar 30, 2014 at 9:20pm
Ahh, at long last, I have found it, thank you for showing me vector, this is the first time I have seen this! And I believe @Stewbond 's code will help with my other problem! Thanks again!
Topic archived. No new replies allowed.