I’m new to c++ and still in highschool so i joined a computer science club and they give out puzzles weekly, i finished one of them and was messing around with having the answer come out one number at a time slowly (like one number every second until the end of the value) but when i researched a command to wait and i got ‘this_thread::sleep_for(seconds(1));’ it pauses the whole code before anything runs and not after i’ve already calculated everything when i need it to run. i’ve made sure to include the libaries and to make sure im using the right namespace (#include , #include , using namespace std::this_thread;, using namespace std::chrono;) but nothing i’ve tried works
I tried moving it into another part of the code to see if it was an error with the part of the code i had it in but it doesn’t change anything it still paused before doing anything and then immediately ran and calculated everything.
I might just be missing something fairly obvious and if so that’s fine but it wouldn’t hurt to have someone tell me what i’m missing.
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
using namespace std;
//this is a puzzle to add all the even numbers in the fibonacci sequence under four million
int main()
{
int holdnumber = 2; //holdnumber and holdvalue have pretty much the same purpose but i did need different names
int holdvalue = 1;
int totaleven = 2; //this shows the sums the total of all even numbers in the Fibonacci sequence under four million
int whichset = 0; //this will change which value will be set to which variable (holdnumber or holdvalue)
int newnumber; //used to check if the number is even and to set the new number that will be added
while( holdnumber < 4000000 && holdvalue < 4000000) {
newnumber = holdnumber + holdvalue;
if (newnumber % 2 == 0) {
totaleven = totaleven + newnumber;
}
if (whichset == 0) {
holdnumber = newnumber;
whichset = 1;
}
else if (whichset == 1) {
holdvalue = newnumber;
whichset = 0;
}
else {
cout << "error 'whichset' not 1 or 0";
}
}
cout << "every even number in the Fibonacci sequence added is..." << totaleven;
//this is for reference so i know the number and i can write the correct value
this_thread::sleep_for(chrono::seconds(10));
//plan is to change this later to 1 second
cout << " ";
this_thread::sleep_for(chrono::seconds(10));
cout << "4";
}
//this is where i did my first test and i've been trying to figure out how to fix
//the problem before writing more, i want the numbers to come out one second at a
//time ex: 4, wait a second, 6, wait a second, etc..
ive been using cpp.sh if that could change anything
DuckTapedDog is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
6