Я не могу выровнять вывод своей программы. Я хочу сохранить те же имена и получить правильный интервал. Код приведен ниже. Я также пытался использовать left
, но это все равно не работает.
Ожидаемый результат:
Вывод, который я получаю:
//taking name and votes recieved
for (i = 0; i < 5; i++)
{
cout << "Enter last name of candidate " << (i + 1) << ": ";
cin >> names[i];
cout << "Enter votes recived by " << names[i] << ": ";
cin >> votes[i];
}
//calculating total votes
for ( i = 0; i < 5; i++)
{
total = total + votes[i];
}
//calculating percentage of total votes for each candidate
for ( i = 0; i < 5; i++)
{
percent_of_total[i] = (votes[i] / total) * 100.0;
}
//checking winner
winner = names[0];
int most = 0;
for ( i = 0; i < 5; i++)
{
if (votes[i] > most)
{
most = votes[i];
winner = names[i];
}
}
cout << fixed << setprecision(2);
//dislaying
cout << "Candidte" << setw(20) << "Votes Recieved" << setw(20) << "% of Total Votes";
for (i = 0; i < 5; i++)
{
cout << endl;
cout << names[i] << setw(20) << votes[i] << setw(20) << percent_of_total[i];
}
cout << endl;
cout << "Total" << setw(20) << total;
cout << endl << "The winner of the Election is " << winner << ".";
return 0;
}
std::setw()
к своему первому столбцу, но имена также имеют разную длину. Это причина вашего невыровненного внешнего вида таблицы. (Обратите внимание, что ваши выходные строки лестны именно по длине имен.) - person Scheff's Cat   schedule 15.12.2020std::setw()
применяется к следующему форматированному выводу, но не к предыдущему. (std::setw()) - person Scheff's Cat   schedule 15.12.2020