我试图解析一个文本文件,并使用setw()将其格式化后的内容输出到控制台。我的问题是只有第一行的格式正确,其余的默认回到左。
while (test)
{
cout << setw(20) << right;
string menu;
price = 0;
getline(test, menu, ',');
test >> price;
cout << setw(20) << right << menu;;
if (price)
cout << right << setw(10) << price;
}
Run Code Online (Sandbox Code Playgroud)
我的目标是使输出与右边的最长单词(长度为20个空格)对齐,但是我的输出最终像这样:
WordThatAlignsRight
notAligning
my longest sentence goal align
notAligning
Run Code Online (Sandbox Code Playgroud)
我希望每个句子在循环中右对齐20个空格。任何帮助表示赞赏,谢谢!
如果有一个数组,
int amounts[26] = { 0, 0, 0, ...};
Run Code Online (Sandbox Code Playgroud)
我想阵列的每个数字代表一个不同的串的量,使得amounts[0] = amount;的'a'的被给定的字符串中发现,反正是有递增的每个值,而无需使用if语句?
Psuedocode示例:
int amounts[26] = { 0, 0, 0, ...};
string word = "blahblah";
loop here to check and increment amounts[0] based on amount of 'a's in string
repeat loop for each letter in word.`
Run Code Online (Sandbox Code Playgroud)
在循环结束时,根据字符串字,数量应如下所示:
amounts[0] = 2 ('a')
amounts[1] = 2 ('b')
amounts[2] = 0 ('c')
// etc
Run Code Online (Sandbox Code Playgroud)