像格式表格中的输出没有排列

时间:2011-11-21 23:31:08

标签: c++

以下是我的输出结果: enter image description here

我希望将输出放在一个漂亮的表中,例如带有标题的格式,以及为该变量分配的setw区域中间的数据。我不知道该怎么做。

代码:

/***************************************************/
/* Author:     Sam LaManna                         */
/* Course:     CSC 135 Lisa Frye                   */
/* Assignment: Program 4 Grade Average             */
/* Due Date:   10/10/11                            */
/* Filename:   program4.cpp                        */
/* Purpose:    Write a program that will process   */
/*             students are their grades. It will  */
/*             also read in 10 test scores and     */
/*             compute their average               */
/***************************************************/

#include <iostream>     //Basic input/output
#include <iomanip>      //Manipulators
#include <string>       //String stuff 
#include <fstream>

using namespace std;

void instruct ();       //Function declaration for printing instructionstring studname ();
void input (ifstream &infile, float& test1, float& test2, float& test3, float& test4, float& test5, float& test6, float& test7, float& test8, float& test9, float& test10, string& studentname);      //Function declaration for input
float aver (float test1, float test2, float test3, float test4, float test5, float test6, float test7, float test8, float test9, float test10);      //Function declaration for calculating average
void output (string studentname, float average);      //Function declaration for output



int main()
{
  float test1 = 0;              //Vars (test1 - test10) for test scores
  float test2 = 0;
  float test3 = 0;
  float test4 = 0;
  float test5 = 0;
  float test6 = 0;
  float test7 = 0;
  float test8 = 0;
  float test9 = 0;
  float test10 = 0;
  string studentname = "a";     //Define Var for storing students name
  float average = 0;            //Define var for storing average


  instruct();     //Function call to print instructions


  ifstream infile("grades.dat");

  input (infile, test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, studentname);     //Function call for scores

  cout << left;
  cout << setw (30) << "Student Name" << setw (5)<< "Average" << setw (12) << "Letter Grade" << "\n";     //headers for output  

  while (!infile.eof())
    {
      average = aver (test1, test2, test3, test4, test5, test6, test7, test8, test9, test10);    //Function call for average

      output (studentname, average);     //Function call for output


      input (infile, test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, studentname); //Get new input
    }     //end eof

  return 0;
}

/***************************************************/
/* Name: instruct                                  */
/* Description: Print instructions to user.        */
/* Paramerters: N/A                                */
/* Return Value: N/A                               */
/***************************************************/

void instruct()
{
  cout << "\n" << "This program will calculate the average of 10 test scores that are read from a file. " << "\n" << "\n";
  //Prints instructions

  return;
}

/***************************************************/
/* Name: input                                     */
/* Description: Get input                          */
/* Paramerters: N/A                                */
/* Return Value: N/A                               */
/***************************************************/

void input (ifstream& infile, float& test1, float& test2, float& test3, float& test4, float& test5, float& test6, float& test7, float& test8, float& test9, float& test10, string& studentname)

{
  getline(infile, studentname);
  infile >> test1 >> test2 >> test3 >> test4 >> test5 >> test6 >> test7 >> test8 >> test9 >> test10;
  infile.ignore(10, '\n');

  return;
}



/***************************************************/
/* Name: aver                                      */
/* Description: Calculate Average                  */
/* Paramerters: N/A                                */
/* Return Value: aver                              */
/***************************************************/


float aver (float test1, float test2, float test3, float test4, float test5, float test6, float test7, float test8, float test9, float test10)

{
  float aver = 0;
  aver = test1 + test2 + test3 + test4 + test5 + test6 + test7 + test8 + test9 + test10;
  aver = aver / 10;
  return aver;
}


/***************************************************/
/* Name: output                                    */
/* Description: Calculate Average                  */
/* Paramerters: N/A                                */
/* Return Value: aver                              */
/***************************************************/

void output (string studentname, float average)      //Function declaration for output
{
  char letter = 'Z';

  if (( average < 101) && (average > 89.9))
    {letter = 'A';
    }
  else if (( average < 90) && (average > 79.9))
      {letter = 'B';
      }
  else if (( average < 80) && (average > 69.9))
    {letter = 'C';
    }
  else if (( average < 70) && (average > 59.9))
      {letter = 'D';
      }
  else if ((average < 60) && (average > -1))
        {letter = 'F';
        }
  else if (average >100)
    {
      letter = 'A';
    }
  else 
    {cout << "Error, Program shutting down.";
      exit (0);
    }

  cout << left;

  cout << setw (30) << studentname << setw (5) << setprecision (2)<< fixed << average << setw (12) << letter << "\n";

  return;
}

2 个答案:

答案 0 :(得分:1)

cout&lt;&lt; setw(30)&lt;&lt; “学生姓名”&lt;&lt; setw(5)&lt;&lt; “平均”&lt;&lt; setw(12)&lt;&lt; “字母等级”&lt;&lt; “\ n” 个; //输出标题

cout&lt;&lt; setw(30)&lt;&lt; studentname&lt;&lt; setw(5)&lt;&lt; setprecision(2)&lt;&lt;固定&lt;&lt;平均&lt;&lt; setw(12)&lt;&lt;字母&lt;&lt; “\ n” 个;

我对c ++知之甚少,但这似乎正在发生。 setw函数是某种宽度,在你的标题中,你只有5个空格用于单词平均值,考虑到单词本身的字母数量,它已经混淆了输出。尝试在上面两种情况下将setw(5)更改为setw(15),看看它是什么样的。

祝你好运。

答案 1 :(得分:1)

我发现此页面上的信息(http://www.cprogramming.com/tutorial/iomanip.html)是setw()用法的绝佳提示。

除此之外,Mike_K是正确的。您为“平均”列指定了5个字符的宽度(7个字符)。

为了更容易看到值如何影响输出,您可以考虑执行以下操作:

// these three values are globals
const unsigned int NAME_LEN(30);
const unsigned int AVG_LEN(10);
const unsigned int GRADE_LEN(12);
...
...
  cout << setw (NAME_LEN) << "Student Name" << setw (AVG_LEN)<< "Average"
       << setw (GRADE_LEN) << "Letter Grade" << "\n";     //headers for output  

您在每个cout语句中使用全局值的位置。这样,如果你想看看如果你将平均列的长度从10改为20会是什么样子,你可以改变一次值,重新编译并重新运行。我发现在一个位置而不是在多个位置更改值更容易

当然,其他人可能会争论使用全局变量。