Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to modify this section of code to hold a long integer. I also need to take out most of the code and add these lines.
#include <chrono>
auto start = chrono::steady_clock::now(); //get start time
auto end = chrono::steady_clock::now(); //get end time
cout << "Elapsed time : " <<
chrono::duration_cast<chrono::milliseconds>(end - start). count()
<< "ms" << endl;
Sorry those aren't in the oder they will probably need to be to put in the program.
The code I posted is the original code that I need to make modifications to.

What I have tried:

//for.cpp -- the for loop
#include <iostream>
#include <chrono>
using namespace std;
int main()
{
	int num;
	cout << "The number generator counts up from 0 to the one you enter! \n";
	cout << "Enter the ending number: ";
	cin >> num;
	for (int i = 0; i <= num; i++)
	{
		cout << i << endl;
	}
	system("pause");
}
Posted
Updated 29-Oct-21 11:12am

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

But to be honest, if you think expanding num to a long - 64 bit - integer is a good idea, I just hope you don't want to use your computer for some significant number of years, as that loop is going to take considerable time to complete ...
What I would suggest you do is scrap the code you have as it isn't at all relevant to the task you have been set, and start again from scratch.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
Mountain town life 29-Oct-21 12:56pm    
Ok thanks I think I was able to figure out most of the error messages that I was receiving, but there are still a couple that I can't figure out.
//for.cpp -- the for loop
#include <iostream>
#include <chrono>
using namespace std;
int main()
{
auto start = chrono::steady_clock::now(); // get the start time
int input, milliseconds,n;
int num;
cout << "The number of generators counts from 0 to the number you enter!";
cout << "Enter the ending number: ";
cin >> num;
for (int i = 0; i <= num; i++)
{
cout << i << endl;
}
{
chrono::duration_cast<chrono::milliseconds> (end - start).count() << "ms" << endl;
auto end = chrono::steady_clock::now(); //get end time
}
{
std::cout << "Elapsed time: " << milliseconds << endl;
system("pause");
}
I keep getting two error messages these are:
Severity Code Description Project File Line Suppression State
Error (active) E0304 no instance of function template "std::chrono::duration_cast" matches the argument list Forloop.cpp C:\Users\burnsk\source\repos\Hello\Forloop.cpp\theforloop.cpp 18
and
Severity Code Description Project File Line Suppression State
Error (active) E0349 no operator "-" matches these operands Forloop.cpp C:\Users\burnsk\source\repos\Hello\Forloop.cpp\theforloop.cpp 18
Thanks for everything!
This is the revised code you posted most recently :
C++
#include <chrono>
using namespace std;

int main()
{
    auto start = chrono::steady_clock::now(); // get the start time
    int input, milliseconds,n;
    int num;
    cout << "The number of generators counts from 0 to the number you enter!";
    cout << "Enter the ending number: ";
    cin >> num;
    for (int i = 0; i <= num; i++)
    {
        cout << i << endl;
    }
    {
        chrono::duration_cast<chrono::milliseconds> (end - start).count() << "ms" << endl;
        auto end = chrono::steady_clock::now(); //get end time
    }
    {
    std::cout << "Elapsed time: " << milliseconds << endl;
    system("pause");
}
There are several problems with this such as unmatched, missing braces and variables referenced before they are declared. Here is one way some of these things can be corrected :
C++
int main()
{
    int num;

    cout << "The number of generators counts from 0 to the number you enter!";
    cout << "Enter the ending number: ";
    cin >> num;

    auto start = chrono::steady_clock::now();  // get the start time

    for( int i = 0; i <= num; i++ )
    {
        cout << i << endl;
    }

    auto end = chrono::steady_clock::now();    // get end time

    auto millisecs = chrono::duration_cast< chrono::milliseconds >( end - start ).count();

    std::cout << "Elapsed time: " << millisecs << "ms" << endl;

    system("pause");
    return 0;
}
 
Share this answer
 
Comments
merano99 30-Oct-21 18:50pm    
Better not to use system("pause").

See:
https://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong

Instead write a function pause() with cin.ignore() and cin.get().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900