我的老师一直告诉我,我必须将setw与正在修改的项目放在同一行。我以为我做了,但她一直告诉我不是,她也一直告诉我,我标记我的参数是错误的,所以如果有人能帮助我解决我的setw问题和我的参数问题,那将是伟大的。我在每个函数下面的注释中标注了参数。
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX=14;
void startArray(int beadArray[MAX]);//Creates the array that gets printed out
void printArray(int beadArray[MAX]);//Prints out the array
void board(int beadArray[MAX]); //Creates the board out of stars
void makeSolidLine(int numStars); //makeSolidLine makes a line out of numStars
void line(); // A line of stars with 6 spaces inbetween
void topBinNum(); //Numbers in top of the board
void bottomBinNum(); //Numbers in the bottom bin
void topBinNumValue(); //Values of each slot in the top bin
void bottomBinNumValue(); //Values of each slot in the bottom bin
int gameOver(int beadArray[MAX]); //Declares the winner by adding bins and comparing the totals
int main()
{
int beadArray[MAX];
startArray(beadArray);
board(beadArray);
int winner;
winner=gameOver(beadArray);
cout<<winner;
system("pause");
return 0;
}
/*Calls the functions in proper order so the code will run smoothly
parameter=n/a
return value=n/a
*/
void topBinNum()
{
cout<<"* ";
for(int i=0; i<6; i++)
{
cout<<"*"<<setw(4)<<i<<setw(3);
}
cout<<"*";
cout<<" *n";
}
/*Numbers the slots on the board starting at 0 setting spaces and a * between slots then moving to the next slot adding 1 and so on until at 5 then it stops
parameter=n/a
return value=n/a
*/
void bottomBinNum()
{
cout<<"* ";
for(int i=12; i>6; i--)
{
cout<<"*"<<setw(4)<<i<<setw(3);
}
cout<<"*";
cout<<" *n";
}
/*Numbers the slots on the board starting at 12 setting spaces and a * between slots then moving to the next slot subtracting 1 and so on until at 7 then it stops
parameter=n/a
return value=n/a
*/
void makeSolidLine(int numStars)
{
for (int count=0; count<numStars; count++)
{
cout<<"*";
}
}
/*Prints out a solid line of *
parameter=int numStars
return value=n/a
*/
void line()
{
for (int count=0; count<8; count++)
{
cout<<"*";
for(int count=0; count<6; count++)
{
cout<<" ";
}
}
cout<<"*n";
}
/*Prints out a line of * with six spaces inbetween
parameter=n/a
return value=n/a
*/
void startArray (int beadArray[MAX])
{
for(int i=0; i<MAX; ++i)
{
beadArray[i]=4;
}
beadArray[6]=0;
beadArray[13]=0;
}
/*gives each slot a value
parameter=int beadArray[MAX]) keeps array from going above the MAX
return value=n/a
*/
void printArray (int beadArray[MAX])
{
for(int i=0; i<MAX; i++)
{
cout<<beadArray[i];
cout<<endl<<endl;
}
}
/*Finds data needed to print out the numbers in the correct order
parameter=int beadArray[MAX] keeps array from going above the MAX
return value=n/a
*/
void topBinNumValue(int beadArray[MAX])
{
cout<<"* ";
for(int i=0; i<6; i++)
{
cout<<"*"<<setw(4)<<beadArray[i]<<setw(3);
}
cout<<"*";
cout<<" *n";
}
/*
topBinNumValue calls the parameter int beadArray[MAX] which is the slot scores from 0-4 and outputs five 4's with no return value
parameter=int beadArray[MAX] keeps array from going above the MAX
return value=n/a
*/
void bottomBinNumValue(int beadArray[MAX])
{
for(int i=13; i>5; i--)
{
cout<<"*"<<setw(4)<<beadArray[i] <<setw(3);
}
cout<<" *n";
}
/*
bottomBinNumValue calls the parameter int bead array[max] which is the slot scores from 6-13 and outputs a 0 then five 4's and another 0 with no return value
parameter=int beadArray[max] keeps array from going above the MAX
return value=n/a
*/
void board(int beadArray[MAX])
{
makeSolidLine(57);
cout<<endl;
line();
topBinNum();
line();
topBinNumValue(beadArray);
line();
cout<<"* 13 ";
makeSolidLine(43);
cout<<" 6 *";
cout<<endl;
line();
bottomBinNum();
line();
bottomBinNumValue(beadArray);
line();
makeSolidLine(57);
cout<<endl;
}
/*Creates the board with numbers in proper location by calling all the previously created codes the print out the board.
parameter=int beadArray[MAX] keeps array from going above the MAX
return value=n/a
*/
int gameOver(int beadArray[MAX])
{
int total1=0;
int total2=0;
int winner=0;
for(int i=0; i<6; i++)
{
total1=total1+beadArray[i];
}
for(int i=12; i>6; i--)
{
total2=total2+beadArray[i];
}
if(total1==0||total2==0)
{
if(total1>total2)
{
winner =1;
}
else
{
winner=2;
}
}
else
{
winner= -1;
}
return winner;
}
/*Adds the totals to beadArray[13 & 6] checks to see which slot is higher and displays the winner if there is one.
parameter=int beadArray[MAX] keeps array from going above the MAX
return value=winner, who ever won the game is the return value
*/
以下是std::setw
的一些文档。
从该链接:
当用于表达式
out << setw(n)
…,将输出或输入流的宽度参数精确设置为n。
所以你的行:
cout<<"*"<<setw(4)<<i<<setw(3);
是否按此顺序:
- 打印
"*"
- 设置宽度
cout
现在将使用为4
- 打印宽度为4的
i
- 设置宽度
cout
现在将改为3
之后打印的内容宽度为3。
你的老师似乎不喜欢不清楚将以3的宽度打印什么,因为下一个打印的内容是在另一行。