#My code isnt working and i dont know how to solve it

18 messages · Page 1 of 1 (latest)

torn verge
#

Hello there.
Im trying to create an array from the integer that users input. Tho whenever i try to set a size of an array as the number of digits of the integer, it says expression must have a constant value. Is there a way to solve this?

short forumBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

torn verge
#

Why code isnt working and i dont know how to solve it

#

My code isnt working and i dont know how to solve it

simple pine
#

I think it’s because the array’s size needs to be known at compile time, not during run time. And user input is during run time so

#

Usually ppl just set the array to be big enough to whatever the biggest possible answer ur gonna get would be

#

The size 256 is common for a string I think

frosty fern
#

Use a vector

simple pine
#

Just type array[20] or something

#

Prob big enough

torn verge
frosty fern
#

std::vector<int> my_ints(3) if you want a vector of ints size 3

torn verge
#

what if i want my vector to have the size of the integer that the users input?

frosty fern
#

std::vector<int> my_ints(whatever_var_you_stored_your_input_in)

#
int input;
std::cin >> input;
std::vector<int> my_vec(input);

for example

torn verge
#

ah thank you!

short forumBOT
#

@torn verge Has your question been resolved? If so, run !solved :)

torn verge
#

!solved