在打印代码时,它可以运行,但似乎没有运行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
输出:
Press Enter to begin!
0................
1................
2................
3................
4................
Press Enter to continue
0................
Press Enter to continue
0................
Press Enter to continue
0................
Press Enter to continue
答案 0 :(得分:6)
string coinflip0(h0); // call function
这实际上不是函数调用。这是一个变量声明,类似于:
string coinflip0 = h0;
要调用该函数,请省略string
。一个简单的coinflip0(h0)
就可以解决问题。我认为您想将结果分配回h0
,所以也可以这样做:
h0 = coinflip0(h0);