列对齐问题

时间:2017-05-15 15:47:46

标签: c++ alignment

我能够调整我的列,但是我的编码问题出现在我的"%"将不会连接到百分比变量。

这是我对输出/对齐的编码:

//HEADER
    outFile << left << setfill(' ') << setw(15) << "Member Name" 
                << setfill(' ') << setw(20) << "Portion Achieved" 
                << setfill(' ') << setw(12) << "Grade (%)" 
                << setfill(' ') << setw(15) << "Letter Grade" 
                << setfill(' ') << setw(15) << "Comment" << endl;

 while (count <= 6)
    {
    cout << "Enter Name: ";
    cin >> student.name;
    cout << "Points Achieved: ";
    cin >> student.points;
    cout << "\n";

        decimal = (student.points/60);
        percent = decimal*100;

        outFile << left << setfill(' ') << setw(15) << student.name 
                << setfill(' ') << setw(20) << decimal 
                << setfill(' ') << setw(12) << percent << " %"
                << setfill (' ') << setw(15);

这是输出的外观:

Member Name    Portion Achieved    Grade (%)   Letter Grade   Comment        
Min            0.166667            16           %F              Sorry, you did not pass :(
Carmela        0.333333            33           %F              Sorry, you did not pass :(
Jayson         0.5                 50           %F              Sorry, you did not pass :(
Kristin        0.666667            66           %D              Needs Improvement!
Mae            0.833333            83           %B              Well Done!     
JT             1                   100          %A              Excellent!  

VS我希望它看起来如何:

    Member Name    Portion Achieved    Grade (%)   Letter Grade   Comment        
    Min            0.166667            16%         F              Sorry, you did not pass :(
    Carmela        0.333333            33%         F              Sorry, you did not pass :(
    Jayson         0.5                 50%         F              Sorry, you did not pass :(
    Kristin        0.666667            66%         D              Needs Improvement!
    Mae            0.833333            83%         B              Well Done!   
    JT             1                   100%        A              Excellent! 

1 个答案:

答案 0 :(得分:1)

它正在做你告诉它要做的事情。整数<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Browser Tests</title> <link href="https://cdn.rawgit.com/mochajs/mocha/v3.4.1/mocha.css" rel="stylesheet"/> <script src="https://cdn.rawgit.com/mochajs/mocha/v3.4.1/mocha.js"></script> <script src="http://sinonjs.org/releases/sinon-2.2.0.js"></script> <script src="../node_modules/systemjs/dist/system.js"></script> <style> #errors pre { width: 50em; margin: 2em 4em; padding: 1em; border: 1px solid red; } </style> </head> <body> <div id="errors"></div> <div id="mocha"></div> <script> /*global SystemJS */ mocha.setup('bdd'); // This hack switches the system-dependent components so that require() // statements for modules with node.js specific code are exchanged for their // browser versions. const resolveOrig = SystemJS.constructor.prototype.resolve; SystemJS.constructor.prototype.resolve = function (key, parents) { return resolveOrig.call(SystemJS, key.replace('-nodejs.js', '-browser.js'), parents); }; // ====================================================== // RELEVANT SECTION (what I tried) // THIS WORKS: SystemJS.set('sinon', SystemJS.newModule(sinon)); // THIS DOES NOT WORK: SystemJS.set('systemjs', SystemJS.newModule(SystemJS)); // ====================================================== // These are the test scripts in ./test/ that I want to run in the browser const mochaTestScripts = [ // ... ABRIDGED LIST .... 'crypto-helpers', 'map-query', 'object-helpers', 'storage' ]; SystemJS.config({ map: { 'chai': '../node_modules/chai/chai.js', 'chai-as-promised': '../node_modules/chai-as-promised/lib/chai-as-promised.js', // dependency of chai-as-promised 'check-error': '../node_modules/check-error/check-error.js', 'js-sha256': '../node_modules/js-sha256/build/sha256.min.js' } }); Promise.all( mochaTestScripts.map(testScript => SystemJS.import('./' + testScript + '-test.js') .catch(err => { const div = document.getElementById('errors'); const pre = document.createElement('pre'); pre.appendChild(document.createTextNode('Test: ' + testScript + '\n\n' + err)); div.appendChild(pre); }) ) ) .then(() => { mocha.checkLeaks(); mocha.globals([]); mocha.run(); }) .catch(err => { const div = document.getElementById('errors'); const pre = document.createElement('pre'); pre.appendChild(document.createTextNode('GLOBAL ERROR\n' + err)); div.appendChild(pre); }); </script> </body> </html>值的宽度为12个字符。之后发生的任何事情都会发生。这里没有代码可以“连接”任何东西,C ++流中也不存在任何这样的特性。

您可能需要将percent(伪代码)预先构造为字符串,然后使用该单个字符串作为列值。也许:

percent + " %"

否则,请自行删除<< setfill(' ') << setw(12) << (std::to_string(percent) + " %") 并写入 n 空格。要做到这一点,你需要弄清楚你的字符串setw文本的“长”程度,加2(对于“%”)然后从12中减去该批次。将percent写入流。呸!