사용자가 프롬프트 번호 (1-4)를 입력해야하는 간단한 프로그램을 만들고 싶었습니다. 계속되는 조건은 사용자가 선택한 레벨로 정의 된 시간의 숫자를 입력하고 정확한 숫자를 입력하는 것을 기반으로합니다. 문제는 프로그램이 번호가 정확한지 인식하지만, 원하는만큼 오래 걸릴 수 있으며 게임을 멈추지 않습니다. 이 "15 분"프로그램은 C++에 대한 파고가 될 것으로 예상되었지만, 혼란스러운 시계 알고리즘과 혼동의 주말 프로젝트로 바뀌 었습니다. 아래는 전체 소스 코드입니다. Here 표준에서는 당신이 현재 상태의에서 프로그램을 테스트 할 수 있도록 온라인의 C++ 에뮬레이터에 연결 ...C++ 시계가 작동하지 않습니다.
#include <iostream>
#include <time.h>
#include <ctime>
#include <unistd.h>
using namespace std;
int main() {
//input
string numberGuessed;
int intNumberGuessed;
int score = 0;
int actualNumber;
int gameOver = 0;
//Level Select Variables
char level = {0};
string input = " ";
double timeForInput;
//initialize random seed
srand (time(NULL));
//Generates random number
actualNumber = rand() % 4 + 1;
//LEVEL SELECTOUR
cout << "Select A Level: 'e' 'm', or 'h': ";
getline(cin, input);
if (input.length() == 1) {
level = input[0];
}
if (level == 'e') {
cout << "You Have Selected Easy..." << endl;
cout<< "You Have .5 Second to Enter" << endl;
timeForInput = 5;
} else if(level == 'm'){
cout << "You Have Selected Medium..." << endl;
cout<< "You Have .2 Seconds to Enter" << endl;
timeForInput = 2;
} else if(level == 'h'){
cout << "You Have Selected HARD!!!" << endl;
cout<< "You ONLY Have .1 Seconds to Enter" << endl;
timeForInput = 1;
} else {
cout << "You LOSE! GOOD DAY SIR!" << endl;
}
//Instructions and Countdown
cout<<"Press The Number and Hit Enter in The Amount of Time Provided"<<endl;
sleep(1);
cout<<"3"<<endl;
sleep(1);
cout<<"2"<<endl;
sleep(1);
cout<<"1"<<endl;
sleep(1);
cout<<"GO!"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<< "Enter the Numbers As They Appear:"<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
sleep(1);
double duration = 0.0;
do {
//Procedere For Each Round
clock_t start;
clock_t finish;
start = clock();
finish = clock();
double delay = (double)(finish-start);
//Clock
start = clock();
cout<<"The number is: "<< actualNumber<<endl;
getline(cin, numberGuessed);
intNumberGuessed = stoi(numberGuessed);
finish = clock();
double elapsed = (double)(finish-start);
elapsed-=delay;
duration = elapsed/CLOCKS_PER_SEC;
cout<<duration<<endl;
//Test User's input
if((intNumberGuessed == actualNumber) && (duration <= (timeForInput/10))){
score += 1;
gameOver = 0;
} else if ((intNumberGuessed != actualNumber) || (duration >= (timeForInput/10))) {
gameOver = 1;
}
//Reset Number
actualNumber = rand() % 4 + 1;
} while (gameOver != 1);
cout<<"You Failed!"<<endl;
sleep(1);
cout<<"Your Score Was: "<<score<<endl;
return 0;
}
이'수 duration' 안를' (clock() - start)/CLOCKS_PER_SEC'? –
방금 시도했지만 문제가 해결되지 않았지만 코드가 전반적으로 개선되었습니다. 고마워, – Noguiguy
당신은 입력 후 기간을 계산하려는 것 같습니다. 현재 구조가 두 가지 시계 호출이 연속적으로 구성되어 있습니다. –