1

I am stuck trying to cout a message with a 3-second delay. I may be trying to do it the hard way by using goto. If anyone has any suggestions they would be greatly appreciated. Thanks!

Code:

#include <iostream>
#include <string>
using namespace std;

int seconds = 0;
int timer = seconds + 1;
int clock = timer;


int main() {
    string namevar;
    cout << "Enter your name: " << flush;
    cin >> namevar;
    std::cout << "Welcome " << namevar << ". This program was created to teach the user how to code using C++" "\n";
clockf1:do {
    seconds = timer;
    if (clock < 3);
        timer = seconds + 1;
        goto clockf1;
        }
    while (clock == 3);
        cout << "When learning any coding language the first step is to understand how to print Hello World. So let us begin!" << flush;
}
Polivicio
  • 71
  • 1
  • 1
  • 12
  • Note that there's nothing in your program that actually keeps time. Read what it actually does, not what you wish it would do. Keep in mind that initializations like `int timer = seconds + 1` are processed only once, at the beginning of the program; they don't establish a permanent relationship between the values. – Nate Eldredge May 04 '20 at 13:27
  • The line `if (clock < 3);` means "if clock is less than 3 then do nothing and continue to next line". In other words, this line is pointless, and despite the way you've indented, doesn't have any effect on whether the following lines are executed. You probably meant `if (clock < 3) { ... }` – Nate Eldredge May 04 '20 at 13:29

2 Answers2

1

Actually you can write in this way to wait 3 seconds:

#include <bits/stdc++.h>

using namespace std;
using namespace this_thread; 
using namespace chrono;

int sec = 0;
int main(){
    string namevar;
    cout << "Enter your name: " << flush;
    cin >> namevar;
    cout << "Welcome " << namevar << ". This program was created to teach the user how to code using C++" "\n";
    do{
        sleep_for(1s);//sleep for 1 sec
        sec++;
        cout << sec <<endl;
    }while (sec<3);


    cout << "When learning any coding language the first step is to understand how to print Hello World. So let us begin!" << flush;

    return 0;
}

You can read more about this_thread and chrono in this previous quetion. I am also reviewing c++ and I can suggest you this book.

Polivicio
  • 71
  • 1
  • 1
  • 12
  • Thanks, the links, and the code both helped quite a bit! I could not get #include to work properly but after looking at the timer link for C++ I figured out I could do #include and #include . It works great now, much appreciated. – Michael Pennington May 04 '20 at 16:01
  • Stdc++ should already include thread and chrono. It is a fast way to include all the std libraries. It is used in coding competition or for testing for example when you don’t want to list all the libraries to not waste time. You can find more info [here](https://stackoverflow.com/questions/25311011/how-does-include-bits-stdc-h-work-in-c) – Polivicio May 04 '20 at 16:53
0

I simplified the code above removing the do and while loops. Figured I might as well post it on here if anyone was interested. Thanks again to all the people who commented or posted code to help!

#include <iostream>
#include <string>
#include <chrono>
#include <thread>

using namespace std;
using namespace this_thread;
using namespace chrono;

const string term = "Hello World!";
int main() {
    string namevar;
    cout << "Enter your name: " << flush;
    cin >> namevar;
    std::cout << "Welcome " << namevar << ". This program was created to teach the user how to code using C++" "\n";
    sleep_for(2s);
    cout << "When learning any coding language the first step is to understand how to print Hello World. So let us begin!" << endl;
}