我目前正在创建一个程序,用于输入作为两组输入数字的分钟和秒的两组输入数字,然后返回专辑的长度,平均歌曲长度,最短的歌曲和最长的歌曲。几乎所有我现在的代码都很顺利,但是我的数字是正确的,我有一些严重的困难。每当我将格式设置为几秒钟,然后回到分钟和秒钟,同时将格式设置为分号并在秒之前设置0时,它们似乎会改变。请参阅附件图片以获取更多解释。数字丢失/更改值
这里是代码:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void displayTime(int);
int main()
{
const int SIZE = 12;
int mins[SIZE], secs[SIZE],minsToSecs[SIZE], totalSecs[SIZE];
int count = 0;
int shortest, longest, totalAlbumLength, averageLengthOfTracks;
cout << "Welcome to my Album Length Calculator" << endl;
cout << "Please enter all track lengths in minutes and seconds" << endl;
cout << "seperated by a space." << endl;
while (count < SIZE)
{
cout << "Track " << count + 1 << ": ";
cin >> mins[count];
cin >> secs[count];
count++;
}
for (int i = 0; i < SIZE; i++)
{
minsToSecs[i] = mins[i] * 60;
}
for (int i = 0; i < SIZE; i++)
{
totalSecs[i] = minsToSecs[i] + secs[i];
}
shortest = totalSecs[0];
for (int i = 0; i < SIZE; i++)
if (shortest > totalSecs[i])
shortest = totalSecs[i];
longest = totalSecs[0];
for (int i = 0; i < SIZE; i++)
if (longest < totalSecs[i])
longest = totalSecs[i];
totalAlbumLength = 0;
for (int i = 0; i < SIZE; i++)
totalAlbumLength += totalSecs[i];
averageLengthOfTracks = totalAlbumLength/SIZE;
cout << "The Shortest track was ";
displayTime(shortest);
cout << "The Longest track was ";
displayTime(longest);
cout << "The Total Album Length was ";
displayTime(totalAlbumLength);
cout << "The Average Track Length was ";
displayTime(averageLengthOfTracks);
system("PAUSE");
return 0;
}
void displayTime(int input)
{
int mins, secs;
mins = input/60;
secs = input % 60;
cout << mins << " : " << setfill('0') << setw(2) << mins << endl;
}
有什么,我很想念这里还是一些简单的,我已经简单地忽略了?
欢迎来到Stack Overflow!这听起来像你可能需要学习如何使用调试器来遍历代码。使用一个好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏离的位置。如果你打算做任何编程,这是一个重要的工具。进一步阅读:** [如何调试小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver
使'SIZE'等于1或2并调试你的程序。你不需要12个项目来查看错误。此外,[您的程序的最小化版本](http://ideone.com/lzevHj)是您可以在此处测试并发布的[mcve]的示例。 – PaulMcKenzie