C++ 赛马程序,忽略函数调用

C++ 赛马程序,忽略函数调用,c++,C++,打印代码时,它会运行,但似乎不会运行coinflip()函数。目前仅尝试打印出第一个马字符串,随机向前移动 #include <iostream> #include <string> #include <time.h> #include <stdlib.h> using namespace std; string h0 = "0................"; string h1 = "1................"; string h

打印代码时,它会运行,但似乎不会运行
coinflip()
函数。目前仅尝试打印出第一个马字符串,随机向前移动

#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
using namespace std;

string h0 = "0................";
string h1 = "1................";
string h2 = "2................";
string h3 = "3................";
string h4 = "4................";
int position0 = 0;
string coinflip0(string h0);

int main(){

   cout << "Press Enter to begin! " <<endl;
   cin.ignore();
   std::cout << h0 << endl; //print string
   cout << h1 << endl;
   cout << h2 << endl;
   cout << h3 << endl;
   cout << h4 << endl;

//      srand(time(NULL));//time goes back to zero for each loop

   while(h0.at(16) != 0) {
        cout << "\n Press Enter to continue " << endl;
        cin.ignore();

        string coinflip0(h0); // call function
        cout << h0 << endl; //print new string
   } //end while
} // end main

string coinflip0(string h0) {

   // find random number(0 or 1)
   int num = rand() % 2;
        cout << num << endl;
   position0 = position0 + num;

   if(num==1){
        std::swap(h0[position0], h0[position0+1]);
   } // end if

   return h0;
}//end coin flip
这实际上不是一个函数调用。这是一个变量声明,类似于:

string coinflip0 = h0;
要调用函数,请省略
字符串
。一个简单的
coinflip0(h0)
就可以了。我认为您希望将结果分配回
h0
,所以也要这样做:

h0 = coinflip0(h0);

不知道如何调用函数应该属于“简单的排版错误”,或者我们需要另一个标志类别。代码中其他令人费解的细节之一是,为什么要在(h0.at(16)!=0)时执行
,以及您认为这样做可以做什么?我试图检查字符串中的最后一个字符是否为零,如果是,循环将停止。这就是我发现的方法,尽管可能有一种更简单的方法我没有找到。
0
'0'
不是一回事,所以我不明白为什么这会起作用,也就是说,为什么你的循环不会永远持续下去。您正在检查具有整数值
0
a.k.a.
'\0'
a.k.a.
NUL
的字符,而不是ASCII数字
'0'
(具有整数值
48
)。这样做没有意义,在我能想到的任何现实情况下都没有用处(因为
'\0'
通常只用作字符串结尾字符)。
string coinflip0 = h0;
h0 = coinflip0(h0);