我有一个函数,如果你插入一个数字,它会计数出来。它只会在程序开始时调用该函数,这意味着它与clock()有关。我添加了clock()给其余的变量,但函数不计数。特别是在if语句中。为什么我的功能不算?
代码:
#include <stdio.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include "math.h"
#include <time.h>
#include <ctime>
#include <cstdlib>
#include <mmsystem.h>
void countbysec(int Seconds);
using namespace std;
int main(){
int secondsinput;
cout<<"Type how many seconds to cout \n";
cin>>secondsinput;
countbysec(secondsinput);
return 0;
}
void countbysec(int Seconds){
clock_t Timer;
Timer = clock() + Seconds * CLOCKS_PER_SEC ;
clock_t counttime = clock() + (Timer/Seconds);
clock_t secondcount = 0;
while(clock() <= Timer){
if(clock() == counttime){
counttime = counttime + CLOCKS_PER_SEC;
secondcount = secondcount + 1;
cout<<secondcount<<endl;
}
}
}
请注意,这是有点不规律的大写你的变量。在传统上保留用于类的C++中。这会让你的代码像“Timer”一样被读取,但事实并非如此。 – tadman
“它将它们计数出来。”不知所云。请用足够的作品来清楚地说出你的意思。 – philipxy
它适合我。 – Neil