C++ anyone?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
maniacal
Posts: 15
Joined: 29 Nov 2010 23:02

C++ anyone?

#1 Post by maniacal » 11 Dec 2010 09:45

Recently I ran into some trouble with batch whilst trying to convert the system time to decimal time (metric time).

The problem occured when trying to convert from the milliseconds passed today to the decimal milliseconds passed today (involves dividing by 0.864, only achievable with set /a %DecimalMillisecondsPassedToday%=(%MillisecondsPassedToday%*1000)/864, that extra *1000 causes the error.)

This is due to the fact that the set /a command stores the value as a signed integer (range from -2147483648 to 2147483647) and 86400000000 (the maximum number of milliseconds in a day possible * 1000) is larger than the range of a signed integer, thus causing the equation to output a wrong value after a certain time of day.

I decided that this was reason enough for me to finally enter the realm of C++ programming.

A quick google search of C++ instantly revealed a 'C++ Language Tutorial' (http://www.cplusplus.com/doc/tutorial/) with absolutely all the information a beginner needs. After familiarising myself with the language style and understanding the basics of c++, I had discovered that all of the commands available in batch programming are really just variations of those of it's parent language (c++).

After a few hours of reading, I started building small programs that performed simple tasks. I found it best if I made them from my memory and understanding of various commands rather than copypasta, thus solidifying their uses in my mind.

What amazed me the most was the versatility of the language, the idea that I am not confined by my console, but by my imagination. Commands are stored in header files rather than built in to cmd.exe, meaning that the program becomes standalone, and that the number of commands avaliable to me are endless, with different ones for any conceivable occasion just a google search away.

Another thing that c++ has over batch scripts are the use of compilers/debuggers.
Ever had a piece of code that just wouldnt work, failed to run, or returned some cryptic erorr? Well with c++ those problems are removed with compilers that check your syntax as you type, underlining any mistakes you have made that will cause errors in the finished code AND give you information regarding why they are errors. Also, a debugger will run your code and give you detailed information about any problems that occur during it's test.

The (minor) drawbacks of c++ are it's compile times and the filesize of the finished product. Although compile times are short with short code (usually a few seconds) I can already tell that as code gets bigger and more complex, those times will only increase. This is especially frustrating when you are compiling the same code over and over as you try to get the output just right. The pro of standalonability (totally a word) may be considered outweighed by it's accompanying con - filesize. A few lines of code in batch may be equal to perhaps a few kilobytes, performing that same task in c++ may however be as large as 50 times that size.

Now to the point of this post.

Do you think that my time spent learning batch could have been better spent learning c++?
Do you have any tips for a c++ noob?
Would you consider learning C++ yourself after reading this?
What is the air-speed velocity of an unladen swallow?
Yes or No on metric time (10 hours/100 minutes/100 seconds)?
Is it possible to perform arithmatic with excessively large numbers in batch?
Am I allowed to post some of my c++ code in these forums to compare it to batch?

tldr: Im learning c++ and it is great.
Last edited by maniacal on 12 Dec 2010 02:38, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: C++ anyone?

#2 Post by aGerman » 11 Dec 2010 15:40

Well, this is a batch forum, so C++ is off topic.
Batch and C++ are completely different concepts.
C++ is a low level language. That means it acts near to the operating system. (The most low level language that I know is Assembly.)
Batch is a high level language. It calls tools which do the job (and a lot of these tools are written in C/C++). E.g. if you use the FIND command, find.exe is called (have a look at C:\Windows\system32).
How ever. To answer some of your questions:
maniacal wrote:Do you think that my time spent learning batch could have been better spent learning c++?

No. The basics of programming are always the same. You can adopt a lot of things you've learnd in Batch.


maniacal wrote:Do you have any tips for a c++ noob?

Read some books about C++. Start with simple console projects. My beginning with C++ was just for the same reason like yours - I found the limits of Batch (and other script languages). Started to write my own command line tools then.


maniacal wrote:Would you consider learning C++ yourself after reading this?

I'm already on the way.


maniacal wrote:What is the air-speed velocity of an unladen swallow?

"African or European :?:" ©Monty Python


maniacal wrote:Yes or No on metric time (10 hours/100 minutes/100 seconds)?

Metric time? What is it good for?
Don't get me wrong, metric length and metric mass are IMO good things and I advocate to standardize things. But 24 hours/60 minutes/60 seconds are used all over the world. No one would come up with the idea to use metric time.

maniacal wrote:Is it possible to perform arithmatic with excessively large numbers in batch?

Yes, but I would never try to do it :wink:
You have to do something like "written calculation" as you learned at school.


maniacal wrote:Am I allowed to post some of my c++ code in these forums to compare it to batch?

I'm not an administrator of this forum. Write a pm to "admin" and ask him.

Regards
aGerman

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: C++ anyone?

#3 Post by jeb » 11 Dec 2010 16:02

Hi maniacal,

The problem occured when trying to calculate the total milliseconds passed today, which happens to be a very large number and thus set /a couldn't handle its size.


A day have 86400 seconds, that are 86.400.000 milli seconds, that is a small number, even for the set /a arithmetic

But it is always a good idea to learn a real programming language.

jeb

maniacal
Posts: 15
Joined: 29 Nov 2010 23:02

Re: C++ anyone?

#4 Post by maniacal » 12 Dec 2010 02:34

jeb wrote:Hi maniacal,

A day have 86400 seconds, that are 86.400.000 milli seconds, that is a small number, even for the set /a arithmetic

jeb


Apoligies.

The problem occured when trying to convert from the milliseconds passed today to the decimal milliseconds passed today (involves dividing by 0.864, only achievable with set /a %DecimalMillisecondsPassedToday%=(%MillisecondsPassedToday%*1000)/864, that extra *1000 causes the error.)

This is due to the fact that the set /a command stores the value as a signed integer (range from -2147483648 to 2147483647) and 86400000000 (the maximum number of milliseconds in a day possible * 1000) is larger than this number, thus causing the equation to output a wrong value after a certain time of day.

What I'm really looking for is the ability to divide by a decimal number reliably

PS: will edit my original post

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: C++ anyone?

#5 Post by aGerman » 12 Dec 2010 07:26

maniacal wrote:86400000000 (the maximum number of milliseconds in a day possible * 1000) is larger than the range of a signed integer, thus causing the equation to output a wrong value after a certain time of day

aGerman wrote:You have to do something like "written calculation" as you learned at school.

Milliseconds * 1000 is easy, simply append 000 and you don't need any calculation for this.
Division:

Code: Select all

@echo off &setlocal
set "Divident=86350000000"
set "Divisor=864"
set "Decimal=2"
set "Separator=."

for /l %%i in (1,1,%Decimal%) do call set "Divident=%%Divident%%0"
set "n=0"
:loop
call set "Part=%%Divident:~%n%,1%%"
if "%Part%"=="" goto :ahead
set "Part=%Reminder%%Part%"
set /a Extension=Part/Divisor, Reminder=Part%%Divisor, n+=1
if %Reminder%==0 set "Reminder="
set "Quotient=%Quotient%%Extension%"
goto :loop

:ahead
if %Decimal% gtr 0 call set "Quotient=%%Quotient:~,-%Decimal%%%%Separator%%%Quotient:~-%Decimal%%%"
for /f "tokens=* delims=0" %%i in ("%Quotient%") do set "Quotient=%%i"
if not defined Quotient set "Quotient=0"
if "%Quotient:~,1%"=="%Separator%" set "Quotient=0%Quotient%"
echo %Quotient%
pause

Regards
aGerman

maniacal
Posts: 15
Joined: 29 Nov 2010 23:02

Re: C++ anyone?

#6 Post by maniacal » 12 Dec 2010 19:03

Thanks for the code aGerman (even if it is a bit much just to perform simple arithmetic)

BTW: I have just completed my first real C++ project, a program which takes the current system time, converts it to decimal time, and then displays and updates this value constantly.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: C++ anyone?

#7 Post by orange_batch » 12 Dec 2010 23:38

It takes a little work, but you can split the 32-bit integer depending on what you want to do with it. I've done so successfully.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: C++ anyone?

#8 Post by ghostmachine4 » 26 Dec 2010 19:25

maniacal wrote:Now to the point of this post.
Do you think that my time spent learning batch could have been better spent learning c++?

you will have wasted your time learning both of them. There are better languages out there such as Python/Perl or Ruby that can also do the job,
without having to use compilers , worry about pointers etc and you can program much faster ? Consider this

Code: Select all

#include <iostream>
using namespace std;
int main(){
  cout << "Hello world" << endl;
  return 0;
}

you can do this in Python (for example) , like this

Code: Select all

print "Hello world";


Simple right?

Is it possible to perform arithmatic with excessively large numbers in batch?

One word. Don't. batch has a limit for large numbers. Sure you can spend your time working around it, but you will be better off with some other language that support large numbers. Simply put, batch is only good for simple stuff, like file copying/moving etc. Other than that, learning batch is a waste of time.

Am I allowed to post some of my c++ code in these forums to compare it to batch?

i don't think why not.

tldr: Im learning c++ and it is great.

you haven't seen anything yet.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: C++ anyone?

#9 Post by ghostmachine4 » 26 Dec 2010 19:32

aGerman wrote:. You can adopt a lot of things you've learnd in Batch.

Wrong!. Batch is not a programming language. It doesn't support data structures such as arrays that are inherent in programming concepts and many other concepts lacking. Batch doesn't have pointers for example, so if you say a person switch from batch to C++, he will not know anything about pointers!.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: C++ anyone?

#10 Post by aGerman » 26 Dec 2010 19:48

I know that batch is no programming language. I meaned that you will find some principle things (like for loops or if statements etc.) in nearly each language.
Of course, each language has its own syntax, its own functions and methodes ...

Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: C++ anyone?

#11 Post by ghostmachine4 » 27 Dec 2010 00:42

aGerman wrote:I know that batch is no programming language. I meaned that you will find some principle things (like for loops or if statements etc.) in nearly each language.

nah, the for loop in batch doesn't "resemble" any of those you seen in languages like C++/Java etc at all. If you look at its features, you can see it can do a bunch of things by passing it options like /L, /F, /R etc... and then it can also be used to call external commands and capture its return value etc.
its nothing like a normal C for loop (for example) where the normal parameters apply: the initializer ( i=0) , the condition (i<10 ), the iterator ( i++,i--) . This is what a traditional for loop is. How about if/else? nah, batch doesn't support multiple if else conditions with logical operators like

Code: Select all

if 1==1 && 2==2 && 3==3  (do something) else ....

for that you have to do this

Code: Select all

if (1==1) 
    if (2==2)
       if (3==3 )

So no, learning batch will not help at all if one wants to proceed on to C/C++ ... period.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: C++ anyone?

#12 Post by aGerman » 27 Dec 2010 05:57

ghostmachine4 wrote:... period.

*LOL* It helped me (slightly). Otherwise I would not write it :wink:

My knowledge in VBS, VBA and my small knowledge in JavaScript, PHP ... helped me even more in learning C++ than Batch could ever do. But this was not the question.

Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: C++ anyone?

#13 Post by ghostmachine4 » 27 Dec 2010 07:13

aGerman wrote:
ghostmachine4 wrote:... period.

*LOL* It helped me (slightly). Otherwise I would not write it :wink:

My knowledge in VBS, VBA and my small knowledge in JavaScript, PHP ... helped me even more in learning C++ than Batch could ever do. But this was not the question.

Regards
aGerman

i was only point to your comment
You can adopt a lot of things you've learnd in Batch.

notice the words "adopt a lot of things". There is nothing in batch you can adopt and use in C++ more than other languages. That's what i am driving at.

Post Reply