Anthony O'Brien Anthony O'Brien - 1 year ago 73
C++ Question

c++ When I cast a string to an int, the int is just a random number?

So I have a function which edits the values, but I cout the values in main to see that it outputs 1309668848 and changes every time I run the program. (this isn't happening in the preprocessor). I have been struggling with this for a while and decided to come here for advice.

Here's the function.

void GetDahInt() {
std::string NewValueS;
getline(std::cin, NewValueS);
NewValue = (int)NewValueS.c_str();

Answer Source

You can use

std::stoi( str )

Discards any whitespace characters (as identified by calling isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) integer number representation and converts them to an integer value.

Source :Documentation of stoi

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download