简体   繁体   English

即使使用 SetW,Output 也未正确对齐

[英]Output not Aligned Correctly even after using SetW

Hey guys i want my program to be alligned correctly im using the iomanip (setw) libary but im still getting the wrong out嘿伙计们,我希望我的程序在使用 iomanip (setw) 库时正确对齐,但我仍然出错了
The output im getting output 我得到

The output I Want 

1001 Butter         9.45  50  100 74
1002 Milk-1L        12.85 100 150 83
1003 Flour-Bak      13.45 210 500 410

The Output I'm getting 

1001 Butter      9.45  50  100 74
1002 Milk-1L       12.85 100 150 83
1003 Flour-Bak        13.45 210 500 410

Here is my code:这是我的代码:

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

// Function Declaration
void displaymenu();
void Inventory();
void empgrossnet();
void employavgdeduc();

// Size of Array
const int SIZE = 10;
int i;

// Initialize Struct function for Employee Data
struct InventoryData {
  int Itemnum;
  string Name;
  double UnitPrice;
  int Minimumlevel;
  int Optimumlevel;
  int Qtyinstock;

} InventoryItems[SIZE];

// Initialize/Read file into the program
ifstream thefile("i_Data.txt");

int main() {

  while (true) {
    displaymenu();
  }
  return 0;
}

void displaymenu() {
  int option;

  // print menu options and prompt user to enter a menu option
  printf("\n***************** Employee Data *******************\n");
  printf("[1] Press 1 for Inventory  Data Records\n");
  printf("[2] Press 2 for Employee Gross and Net Pay\n");
  printf("[3] Press 3 for Average Hours and Average Deductions\n");
  printf("[4] Exit Program\n");
  printf("\n****************************************************\n");
  printf("\n Enter an option>>\t");
  scanf("%d", &option);

  switch (option) {
  case 1:
    Inventory();
    break;

  case 4:
    printf("\n\n Thank you for using the Program");
    printf("\n Exiting Application....");
    exit(0);
  }
}

void Inventory() {

  // Read from edata.txt File
  ifstream thefile("i_Data.txt");

  // Check to make sure that Program is finding/reading from edata file
  if (!thefile) {
    cerr << "File can't be opened! " << endl;
    system("PAUSE");
    exit(1);
  }

  // Creat loop to store 5 lines of information from edata file
  for (int i = 0; i < SIZE; i++) {
    thefile >> InventoryItems[i].Itemnum >> InventoryItems[i].Name >>
        InventoryItems[i].UnitPrice

        >> InventoryItems[i].Minimumlevel >> InventoryItems[i].Optimumlevel >>
        InventoryItems[i].Qtyinstock;
  }

  // Output/Display Edata file information into prgram
  printf("\n************************************* EMPLOYEE DATA "
         "****************************************\n");
  printf("\n %s %15s %20s %15s %15s ", "EmployeeID", "Employee Name",
         "Hours Worked", "Rate of Pay", "Deductions");
  printf("\n-------------------------------------------------------------------"
         "------------------\n");
  for (int i = 0; i < SIZE; i++) {
    cout << setw(10) << " " << InventoryItems[i].Itemnum;
    cout << setw(10) << " " << InventoryItems[i].Name;
    cout << setw(10) << " " << InventoryItems[i].UnitPrice;
    cout << setw(10) << " " << InventoryItems[i].Minimumlevel;
    cout << setw(10) << " " << InventoryItems[i].Optimumlevel;
    cout << setw(10) << " " << InventoryItems[i].Qtyinstock << endl;
  }
  printf("\n-------------------------------------------------------------------"
         "------------------\n");
}

You seem to want你似乎想要

cout <<" " << setw(10) << InventoryItems[i].Itemnum;
cout <<" " << setw(10) << InventoryItems[i].Name;
cout <<" " << setw(10) << InventoryItems[i].UnitPrice;
cout <<" " << setw(10) << InventoryItems[i].Minimumlevel;
cout <<" " << setw(10) << InventoryItems[i].Optimumlevel;
cout <<" " << setw(10) << InventoryItems[i].Qtyinstock<<endl;

Your original code outputs 10 spaces then values.您的原始代码输出 10 个空格,然后是值。 I believe you want single spaces and values in 10-char placeholders.我相信您想要 10 字符占位符中的单个空格和值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM