我已经研究了几种解决问题的方法,但似乎没有任何效果。我希望我的输出对齐此代码上的所有名称,所选数字和奖金。目前,它输出以下内容:
["a", "a"]
我相信我需要名称正确对齐才能解决此问题,但我尝试过的任何方法都没有用。这是我的代码:
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
答案 0 :(得分:4)
由于您现在几个小时都无法使用类型为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';
}
}
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
如果要将货币符号附加到值上,可以使用字符串流:
#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';
}
}
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
答案 1 :(得分:-1)
由于您没有提供足够的代码,因此我将无法复制您的代码以获取输出。但是,您可以使用标签进行操作。
cout << student_info[i].id_num;
cout << setw(10) << student_info[i].student_name << setw(25);
for(int j = 0; j < LOTTERYNUMBERS; j++)
cout << student_info[i].lotteryNumbers[j] << " ";
cout << setw(30) << student_info[i].lotteryMatches << setw(10) << setprecision(2)
<< fixed << showpoint << "$" << student_info[i].prizeMoney << endl;