c++ - return a char function with random options and local variables -
i have created unit test harness test program. wanted able randomly test each run i'm not sure how go doing this. here think stuck on next, guidance appreciated.
int main (void) { int testnumber = 1; //for testing char carname[] = ""; double carcost = 0; carname = carchosen (testnumber); carcost = assesscost (carname); //assesscost takes in car name , checks cost of car (error checking cars can chosen) return 0; }
"testnumber" seeded time create different number's 1 - 15, in situation it's going "1" testing.
this next bit i'm having trouble with. within function there 15 diffrent car options , return 1 depending on randomly created number.
char carchosen (int randnum) { char carone[] = "honda"; char cartwo[] = "ford"; if (randnum == 1) { return carone; //local variables, not going work... } else if (randnum == 2) { return cartwo; // again, these return's here better represent i'm trying create failing so.. } }
i understand cannot return local variables, can instead?
this
void carchosen (int randnum, char * out) { char carone[] = "honda"; char cartwo[] = "ford"; if (randnum == 1) { strcpy(out, carone); } else if (randnum == 2) { strcpy(out, cartwo); } //.. handle other cases }
call like
char carname[max_len]; carchosen (testnumber, carname);
also maybe better of using switch
instead of nested if..else
if have many conditions test.
i thought c looking @ code, if use c++, can return std::string
objects function without issues.
Comments
Post a Comment