445,804 Members | 1,659 Online
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

# problem with counter

 P: n/a Hello, i got following Job: Write a programm that shows the letters a - z. The lettes should be act in some millisecounds. Start with 100 millisecounds and every loop the millisecounds show be rise of 10 percent (such as the secound loop 110, the third 121...) First Time i thougt this. execute the loop until the actual time is bigger then the pause (100 millisecounds). But it doesent work. I hope you can help me Regards Sebastian === German Version === Hallo, ich habe folgende Aufgabe bekommen. Schreibe ein programm das die Zahlen 1 bis 100 ausgibt. Die Zahlen sollen nacheinander erscheinen aber in unterschiedlichen Zeitabständen. Beginne mit 100 Ms und steigere in jeder runde die Millisekunden um 10 Prozent. Wie soll ich das in der console machen? ich kann mir mit clock die aktuelle uhrzeit holen, "Startzeit" messen- Schleife solange ausfÃ¼hren bis "aktuelle Zeit" - "Startzeit" >= "Pause". Das ist ja soweit klar, aber wie soll ich das programmieren? Ich blicke da nicth so durch. Bitte erklärt es mir Gruß Sebastian Mar 19 '07 #1
4 Replies

 P: n/a Test3456 wrote: Hello, i got following Job: Write a programm that shows the letters a - z. The lettes should be act in some millisecounds. Start with 100 millisecounds and every loop the millisecounds show be rise of 10 percent (such as the secound loop 110, the third 121...) First Time i thougt this. execute the loop until the actual time is bigger then the pause (100 millisecounds). AFAIK, there is no platform-independent way of doing this. Under Windows look up the Sleep (DWORD Milliseconds) function. Regards, Stuart Mar 19 '07 #2

 P: n/a On Mon, 19 Mar 2007 09:01:57 -0700, Test3456 wrote: Hello, i got following Job: Write a programm that shows the letters a - z. The lettes should be act in some millisecounds. Start with 100 millisecounds and every loop the millisecounds show be rise of 10 percent (such as the secound loop 110, the third 121...) First Time i thougt this. execute the loop until the actual time is bigger then the pause (100 millisecounds). But it doesent work. That doesn't really help anyone solve your problem. Post some (preferably compilable) code showing what you have tried so far, and explain what you mean by "doesn't work". [...] -- Lionel B Mar 19 '07 #3

 P: n/a On 19 Mrz., 17:14, Lionel B #include const unsigned short count_numbers = 100; int main() { std::wcout << L"Wellcome to Clock-Counter V0.1" << std::endl; clock_t time_start = clock(); for (unsigned short number = 1; number <= count_numbers; ++number) { while ((clock() - time_start) (number * 10)); // Jetzt wird gewartet, bis die // Differenz von vorher und jetzt // gleich (number * 10) ist ... // TODO: Gewünschte Zeit einsetzen. std::wcout << number << std::endl; // Zahl ausgeben ... } return 0; } This is my code, which i have at the moment, but it doesent seam to work. I hope you dont understand my explanation right. The First Number should be appear 1 secound or 1000 ms. but i dont know why this code doesnt work. I tryed to solve the problem 4 hours but i cannot find this error :-( Mar 19 '07 #4

 P: n/a >>On Mon, 19 Mar 2007 09:01:57 -0700, Test3456 wrote: >>>i got following Job:Write a programm that shows the letters a - z. The lettes should beact in some millisecounds. Start with 100 millisecounds and every loopthe millisecounds show be rise of 10 percent (such as the secound loop110, the third 121...) >>>First Time i thougt this. >>>execute the loop until the actual time is bigger then the pause (100millisecounds). >>>But it doesent work. Test3456 wrote: #include #include const unsigned short count_numbers = 100; int main() { std::wcout << L"Wellcome to Clock-Counter V0.1" << std::endl; clock_t time_start = clock(); You only take the start time once. I think you actually want to take the start time each time you're going to wait for the next number. for (unsigned short number = 1; number <= count_numbers; ++number) { while ((clock() - time_start) (number * 10)); This condition will never be true since the time taken at the call of clock will be very close to time_start, so that clock() - time_start is almost always smaller than number * 10. Thus the while loop will exit at the first iteration (so that it won't "eat away" any time at all). std::wcout << number << std::endl; } return 0; } This is my code, which i have at the moment, but it doesent seam to work. I hope you dont understand my explanation right. The First Number should be appear 1 secound or 1000 ms. but i dont know why this code doesnt work. I tryed to solve the problem 4 hours but i cannot find this error :-( You should inflate the code so that you can run it through a debugger (by inflating I mean that you should expand the while loop so that you can see how many times it runs). Cutting down the code to a point where you have solved the problem with the minimum amount of code will get you to a point where you spend the maximum amount of time debugging. Regards, Stuart Mar 19 '07 #5

### This discussion thread is closed

Replies have been disabled for this discussion.