Hello all, I have a question about using the string::length function to initialize an integer array. I know that the value the function returns is a size_t of how many characters there are in the string, but when I try to use this return value to declare the size of an integer array, the array does not initialize correctly.
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
usingnamespace std;
int main()
{
size_t sizeOfString;
string s;
cin >> s;
sizeOfString = s.length();
int foo[sizeOfString];
//rest of program follows
return 0;
}
And just for reference, this is a console application. The issue is that when foo is initialized, I cannot assign any variables to it. I put a watch on this and ran the GUI debugger just to find out what was going on, and I discovered that the compiler for some reason does not treat foo as an array of integers when it's length is defined by the variable sizeOfString.
In the watch, foo is defined as a hexadecimal number and not an array of integer values. The interesting part is that if I set sizeOfString to a number like 4, and it's still a size_t variable, foo becomes an array with 4 members. This means to me that it isn't the size_t variable type that is the problem, but rather something concerning the string::length function.
My question is: what about the return value from string::length will keep an array from being initialized with the number of elements equal to the number of characters in the string?
You can't declare a dynamically sized array like that. You need to use dynamic memory (new and delete). And your deader files are incorrect. You want <string> <cstdlib> and <cstdio>, not the old C versions you have there. <string.h> is a C header renamed to <cstring>, so I don't know if you would want that as well.
@Zhuge: I have tried using new[], and the same result occurred with that.... Maybe the libraries are obsolete, and I will change them to see what happens.
@Duoas: I want the user to input a number, and I want to be able to separate the individual digits into an integer array. So, if the user inputs "5504", I want that number to be stored in my integer array as {5,5,0,4} So far, string operations and using the atoi() function has worked pretty well, but I don't want to make the integer array a set number that's more than I need. Again, dynamic allocation using new[] and delete[] would seem like it would fix the problem, but it doesn't....
#include <cmath> // For log10()
#include <vector> // for vector
#include <cstdlib> // Here because it looks nice.
#include <iostream> // output/input.
int get_digit_at (int num, int pos);
int pow (int b, int e);
int main()
{
int num;
std::cout << "Enter a number: "; std::cin >> num;
// Expecting non negitive numbers
constint digitsSize = (num == 0) ? 1 : std::log10(num) + 1;
// Container. I used a vector.
std::vector<int> digitsArray(digitsSize, 0);
// Filling in the array. Did it in reverse so you'd get the same order.
for (int i = 0; i < digitsSize; ++i)
{
digitsArray[digitsSize-(i+1)] = get_digit_at(num, i+1);
}
for (int i = 0; i < digitsSize; ++i)
{
std::cout << digitsArray[i] << ", ";
}
return 0;
}
int get_digit_at (int num, int pos)
{
// Found this forumla in the public domain for getting a digit out of a number based on position.
// in 5504, position 2 returns 0.
return ( num / pow(10, pos-1) ) - 10 * ( num / pow(10, pos) );
}
// Wanted a simpler pow. Used my own.
int pow (int b, int e)
{
int ans = 1;
for (int i = 0; i < e; ++i)
ans *= b;
return ans;
}
The reason for hexadecimal value of foo is because foo variable in someway a pointer which is pointing to some memory location.
int foo[sizeOfString]; //writing this instruction does not mean that you are initialising it. it means you are allocating space for integers.
the following code worked for me in g++ compiler
@pushpik: Surprisingly, that is almost the exact same syntax I use to try and store variables into the array, but after the for loop in which the atoi function rests, there are no variables in the array. so by line 17 in the above example, foo[] is still just a hex number....
As for what everyone else is saying: Pretty much you do some sort of mathematical operation involving modulus (or in the other example logs) to see when the operation will yield a 0? An example:
// Standard includes
int main()
{
int num, count, numTemp;
cin >> num;
numTemp = num
while (numTemp != 0)
{
numTemp = numTemp%10
count++;
}
int foo[count];
// Rest of program
return 0;
}
Is this the basic idea everyone is talking about? I would have to obtain the individual number digits from the user input a different way (maybe still with strings or a further implementation of the modulus operation), but it seems sound.
You can't create a dynamically sized array like you are on line 16. You must use dynamic memory. You said using new[] game problems; what were those, exactly?
Um..... Well actually I'm having second thoughts about what the problem was. I basically changed the type to dynamic, tried to do operations with values, they didn't work, so I scrapped it and used a static integer value as the number of elements just to see how the rest of the program flowed. It worked, and then I changed it back to dynamic and all of a sudden the integer array worked. I'm really not sure what happened. I'm reasonably sure that my syntax was the same both times.....
But, for the sake of the forum, the problem I was getting was that basically the members of the dynamic array were undefined. I couldn't do operations on the elements (or if I did they returned a negative integer max value). I even tried to output the elements and they showed the negative max integer value as well. But now it works. I'll mark the problem as solved, but feel free to speculate as to why this might have happened.....