c++ - String method crashes program -


OK, so I have two identical string methods ...

  string CreateCust () {String Name [] = {"Tom", "Timo", "Sally", "Kelly", "Bob", "Thomas", "Samantha", "Maria"}; Int d = rand ()% (8 - 1 + 1) + 1; String e = name too [D]; Return e; } Create string () {string name Array [] = {"spaghetti", "chicken soup", "menudo"}; Int d = rand ()% (3 - 1 + 1) + 1; String F = nameArray [D]; Return; }  

However no matter how I dare to make it CreateFood, it will always crash. I have created a test chassis for it and it always fails in cMeal = CreateFood ();

  Customer Context; Cout & lt; & Lt; "Test1" & lt; & Lt; Endl; CME = Know Prepare (); Cout & lt; & Lt; "Test1" & lt; & Lt; Endl; CCustomer = Cnow.CreateCust (); Cout & lt; & Lt; "Test1" & lt; & Lt; Endl;  

I have switched CreateCust with CreateFood and it still fails on the CreateFood function ...

Note: if I create an int method It works ...

In addition, if I changed the CreateFood for only one message COUT and nothing else it still crashed ...

Crashing because you are reaching an invalid index. The reason for this is that the array starts with index 0 and is not 1, so you do not want to add 1 in the modulus operator's rivalue.

Here's a clean move that you can use to make your code a bit more satisfying:

  template  

Comments