我已经研究了几种解决方案来解决我的问题,但似乎没有什么对我有用。我希望我的输出能够对齐此代码上的所有姓名、所选号码和中奖金额。目前它输出:
3454 Atkins, Joe 7 6 5 4 3 2 1 7 $20.00
4321 Barber, John 11 22 7 8 45 12 10 1 $0.00
8976 Dollar, Kim 44 33 22 11 10 9 4 1 $0.00
Run Code Online (Sandbox Code Playgroud)
我相信我需要将名称右对齐才能解决此问题,但我尝试过的任何方法都不起作用。这是我的代码:
cout << student_info[i].id_num;
cout << setw(10) << student_info[i].student_name << setw(10);
for(int j = 0; j < LOTTERYNUMBERS; j++)
cout << student_info[i].lotteryNumbers[j] << " ";
cout << setw(10) << student_info[i].lotteryMatches << setw(10) << setprecision(2)
<< fixed << showpoint << "$" << student_info[i].prizeMoney << endl;
Run Code Online (Sandbox Code Playgroud)
由于您在几个小时内未能使用 now 类型的定义更新您的问题student_info,我假设它是类似的
#include <iostream>
#include <iomanip>
#include <string>
#include <array>
// that:
struct student_info_t {
int id;
std::string name;
std::array<int, 7> numbers;
int matches;
long money;
};
int main()
{
student_info_t student_info[] {
{ 3454, "Atkins, Joe", { 7, 6, 5, 4, 3, 2, 1 }, 7, 2000 },
{ 4321, "Barber, John", { 11, 22, 7, 8, 45, 12, 10 }, 1, 0 },
{ 8976, "Dollar, Kim", { 44, 33, 22, 11, 10, 9, 4 }, 1, 0 }
};
for (auto const &s : student_info) {
std::cout << std::setw(4) << s.id << ' ' << std::left << std::setw(15)
<< s.name << std::right;
for (auto const &n : s.numbers)
std::cout << std::setw(4) << n;
std::cout << std::setw(4) << s.matches << " $" << std::setw(8) << std::fixed
<< std::setprecision(2) << s.money / 100. << '\n';
}
}
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
#include <iomanip>
#include <string>
#include <array>
// that:
struct student_info_t {
int id;
std::string name;
std::array<int, 7> numbers;
int matches;
long money;
};
int main()
{
student_info_t student_info[] {
{ 3454, "Atkins, Joe", { 7, 6, 5, 4, 3, 2, 1 }, 7, 2000 },
{ 4321, "Barber, John", { 11, 22, 7, 8, 45, 12, 10 }, 1, 0 },
{ 8976, "Dollar, Kim", { 44, 33, 22, 11, 10, 9, 4 }, 1, 0 }
};
for (auto const &s : student_info) {
std::cout << std::setw(4) << s.id << ' ' << std::left << std::setw(15)
<< s.name << std::right;
for (auto const &n : s.numbers)
std::cout << std::setw(4) << n;
std::cout << std::setw(4) << s.matches << " $" << std::setw(8) << std::fixed
<< std::setprecision(2) << s.money / 100. << '\n';
}
}
Run Code Online (Sandbox Code Playgroud)
如果您希望将货币符号附加到值上,可以使用字符串流:
3454 Atkins, Joe 7 6 5 4 3 2 1 7 $ 20.00
4321 Barber, John 11 22 7 8 45 12 10 1 $ 0.00
8976 Dollar, Kim 44 33 22 11 10 9 4 1 $ 0.00
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
#include <iomanip>
#include <string>
#include <array>
#include <sstream>
struct student_info_t {
int id;
std::string name;
std::array<int, 7> numbers;
int matches;
long money;
};
int main()
{
student_info_t student_info[]{
{ 3454, "Atkins, Joe", { 7, 6, 5, 4, 3, 2, 1 }, 7, 2000 },
{ 4321, "Barber, John", { 11, 22, 7, 8, 45, 12, 10 }, 1, 0 },
{ 8976, "Dollar, Kim", { 44, 33, 22, 11, 10, 9, 4 }, 1, 0 }
};
for (auto const &s : student_info) {
std::cout << std::setw(4) << s.id << ' ' << std::left << std::setw(15)
<< s.name << std::right;
for (auto const &n : s.numbers)
std::cout << std::setw(4) << n;
std::stringstream ss;
ss << '$' << std::fixed << std::setprecision(2) << s.money / 100.;
std::cout << std::setw(4) << s.matches << std::setw(10) << ss.str() << '\n';
}
}
Run Code Online (Sandbox Code Playgroud)