I have this project for a class and the program is to print out a randomized sequence of the numbers from 0 to 9, Using a really dumb algorithm. I need to find the the problems and not necessarily fix it. The problem I found so far is on line 18 "num_left++;" as a uninitialized local variable, but I don't know what that means.
if (sptr = NULL)
{ // NULL indicates we already processed this;
continue; // skip and continue looping
}
you're doing assignment (=), not testing for equality (==)...
Why are you combining arrays of std::strings with dynamic allocation? Why not just have an array of string objects instead of dynamically-allocated pointers? If it's just for practice, I guess that's fine, but it seems like you're just unnecessarily complicating things.
#include <iostream>
#include <vector>
int main()
{
// Create a vector containing integers
std::vector<std::string> v = {"begin"};
// Add two more integers to vector
v.push_back("hello");
v.push_back("world");
// Iterate and print values of vector
for (const std::string& s : v) {
std::cout << s << '\n';
}
}
On line 27, you are assigning NULL to sptr. I do not believe that this is what you intend.
Your other problem, that I should have noticed before, is that you never set num_left to an initial value before incrementing it.
This is what an "uninitialized local variable" is.
The fix:
Instead of int num_left;, do int num_left = 0;
Are you sure you didnt want to use a single string , also known as an array of characters?
This makes the next randomization part a bit confusing. You also need to be very careful about deleting while still working with that string.
Think about what you're really trying to accomplish here. It appears you're going through 10 times, and I think you're trying to assign a string to NULL -- I believe you can't do this and you could instead try assigning a string to empty string (""). If it was a character, you could assign it to the null terminator ('\0') character. Also, do you need to show the string changing over time (printing it out with each iteration of loop)? Think about the logic before diving into semantics.
As for num_left, I think it doesn't like you atttemping to increment something that doesn't have an initial value. Change line10 to say int num_left=0;