我只是很困惑

时间:2013-04-24 17:19:25

标签: c++ xcode arrays 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
*/

1 个答案:

答案 0 :(得分:1)

这是some documentation for std::setw

从该链接:

  

在表达式out << setw(n) ...中使用时,将流的宽度参数设置为out或in为n。

所以你的行:

    cout<<"*"<<setw(4)<<i<<setw(3);

这些内容按此顺序

  • 打印"*"
  • 将宽度cout 设置为4
  • 打印宽度为4
  • i
  • 将宽度cout 设置为3

在此之后打印的内容宽度为3。

您的老师似乎并不喜欢不清楚将以3的宽度打印,因为下一个打印的内容是在不同的行上。