【问题标题】:I am just plain confused我只是很困惑
【发布时间】:2013-04-24 17:19:24
【问题描述】:

我的老师一直告诉我,我必须将 setw 与正在修改的项目放在同一行。我以为我做到了,但她一直告诉我不是,她还一直告诉我我的参数标记错误,所以如果有人可以帮助我解决我的 setw 问题和我的参数问题,那就太好了。我在每个函数下面的 cmets 中标注了参数是什么。

#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
*/

【问题讨论】:

  • 你觉得你的setw(3)在做什么?
  • 另外,main 确实有返回值
  • 我很确定它在数组 [i] 之前设置了一组 3 个空格
  • @Geoff:再看一遍,这就是setw(4) 正在做的事情。
  • 你认为setw(4) 在做什么?我只是在问清楚什么被误解了。

标签: c++ xcode arrays setw


【解决方案1】:

这里是some documentation for std::setw

从那个链接:

在表达式out &lt;&lt; setw(n) ...中使用时,将流的宽度参数设置为输出或输入正好为n。

所以你的台词:

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

这些事情按此顺序

  • 打印"*"
  • 将宽度cout 现在使用设置为4
  • 打印宽度为 4 的i
  • 将宽度cout 现在使用设置为3

之后打印的任何内容都将具有 3 的宽度。

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

【讨论】:

    猜你喜欢
    • 2011-04-11
    • 2020-08-17
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    相关资源
    最近更新 更多