c++ - std::left/right 的 Iomanip 错误

标签 c++ format iomanip

我试图将一条线的一侧左对齐并右对齐该线的另一侧,但我无法让右侧右对齐。谁能发现我的错误?

//The following program will declare
#include <iostream>
#include <iomanip>

using namespace std;

const int HOUSING = 3500;                                       //housing cost
const int MEALS = 2000;                                         //meal cost
const int N_CRED_PER = 285;                                     //out of state credit cost
const int CRED_PER = 130;                                       //amount of money per credit(cost)
const int TAB = 30;                                             //precision
const int ADD_FEES = 6200;                                      //out of state fees for non-resident

int main()
{
  cout << "Grant Mercer section 1002 Assignment 6\n\n";
  int size, studentID, credits, i, valid,total;
  //size of loop, studentID to hold ID of student, credits to hold num
  //of credits the student will take, i to start loop
  char first, last, res, housing, meal;                         //first and last to hold initials of
                                                                //student, res to hold code
                                                                //if student is resident, meal to hold
                                                                //code if student has meals
  cin >> size;                                                  //grab length of loop in file
  cout << left;                                                 //manipulate stream
  cout << fixed << showpoint << setprecision(2);
  valid = size;                                                 //initially all entries are valid, but de
                                                                //cremented each time something goes wrong
  while  (i < size)
    {
      cin >> studentID >> first >> last >> credits >> res;      //read in mandatory data to vars
      cout << setw(TAB) << left <<  "Student #: " << right << studentID << endl;  //display id
      cout << setw(TAB) << left << "Initials: " << right << first << last << endl;//display initials
      if(credits > 0 && credits < 21)                           //if credits are fine
        {
          switch(res)                                           //find char in res
            {
            case 'R':                                           //if RESIDENT
            case 'r':
              cout << setw(TAB) << left << "Residency status: " << right << "Resident" << endl;
              cout << setw(TAB) << left << "Credits: " << right << credits << endl;
              cout << setw(TAB-10) << left << "Tuition: " << setw(10) << "$" << right << (double)CRED_PER * credits << endl;
              cout << setw(TAB-10) << left << "Total due: " << setw(10) << "$" << right << (double)CRED_PER * credits << endl;
              total += CRED_PER * credits;                      //add to total cost
            break;
            case 'N':                                           //if NON-RESIDENT
            case 'n':
              cout << setw(TAB) << left  << "Residency status: " << right <<  "Non-resident" << endl;
              cin >> housing >> meal;                            //since the student is non-resident, they
                                                                 //have a mean and house code
              cout << setw(TAB) << left << "Credits: " << right << credits << endl;
              cout << setw(TAB - 10) << left  << "Tuition: " << setw(10) << "$" << right << (double)credits * N_CRED_PER + ADD_FEES << endl;
              if(housing == 'H' || housing == 'h')
              {
                cout << setw(TAB - 10) << left << "Housing: " << setw(10) << "$" << right << (double)HOUSING << endl;
                if(meal == 'M' || meal == 'm')                  //if MEALS
                  {
                    cout << setw(TAB-10) << left << "Meal Plan: " << setw(10) << "$" << right << (double) MEALS << endl;
                    cout << setw(TAB-10) << left << "Total due: " << setw(10) << "$" << right << (double) MEALS + HOUSING + (credits * N_CRED_PER) + ADD_FEES << endl;
                    total += MEALS + HOUSING + (credits * N_CRED_PER) + ADD_FEES;
                                                                //add to total
                  }
                else                                             //if NO MEALS
                  {
                    cout << setw(TAB-10) << left << "Total due: " << setw(10) << "$" << right << (double)\
HOUSING + (credits *N_CRED_PER) + ADD_FEES << endl;
                    total += HOUSING + (credits * N_CRED_PER) + ADD_FEES;
                                                                //add to total
                    total += HOUSING + (credits * N_CRED_PER) + ADD_FEES;
                                                                //add to total
                  }
              }
              else                                              //if NO HOUSING NO MEALS
              {
                cout << setw(TAB-10) << left  << "Total due: " << setw(10) << "$" << right << (double)(cr\
edits * CRED_PER) + ADD_FEES
                     << endl;
                total += (credits * CRED_PER) + ADD_FEES;
              }
            break;
            }

        }
      else
      {
        cout << "INVLIAD CREDIT AMOUNT...\n";
        valid--;
      }
      i++;
      cout << endl << endl;
    }
  cout << "\nFINAL TOTALS\n";                                   //display final totals from students
  cout << right << setw(TAB) << left << "Valid request count: " << right << valid << endl;
  cout << setw(TAB) << left  << "Invalid requrest count: " << right << size - valid << endl;
  cout << setw(TAB - 10) << left  << "Total feed due: " << setw(10) << "$" << right << (double)total;
  cout << endl;
  return 0;
}

输出都是左对齐的,我不明白我需要做什么才能使这一切都对齐。谢谢!

最佳答案

在使用 cout 打印每个右对齐字符串之前,您忘记使用 setw()。例如,在您的代码中使用以下行:

cout << setw(TAB) << left << "Residency status: " << right << "Resident" << endl;

在打印每个向左或向右对齐的字符串之前,您必须设置一个新的宽度:

cout << setw(20) << left << "Residency status: " << setw(20) << right << "Resident" << endl;

如果没有 setw(),它会假定宽度为零,这基本上会忽略右对齐。

关于c++ - std::left/right 的 Iomanip 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12922744/

相关文章:

c++ - 使用带有 adjacency_list 的通用类型

c++ - Linux 将文档打印到标准输出或文件

sql - Oracle SQL 时间戳格式

c++ - 如何正确使用 setprecision() 将 ".00"添加到金额的末尾?

c++ - iomanip 错误或 ‘setw’ 未在此范围内声明

c++ - 删除这些指针时出错

arrays - 将字符串值添加到 Numpy 数组 Python 中每个值的末尾

java - 如何用前导零写入 csv?

c++ - cout中 `fixed`的反义词是什么?

c++ - 是否可以将参数包扩展为不同的成员函数?