-
December 17th, 2019, 02:55 PM
#1
Round Function
OK guys, take it easy because this is my 1st program. I have jumped some hurdles and resisted the urge to ask for help but the round function has me stumped. In the code below I had to create my own round function. Why won't (51199.9488-round(51199.9488)) return .0512? The debug error states that round is an undeclared identifier.
Code:
// First.cpp: Need I state more?
#include <stdafx.h>
#include <iostream>
using namespace std;
void main()
{
double degrees=360;
double poserr=0;
double pulses=0;
for (degrees=359; degrees<360;)
{
pulses=(((degrees*3600)/2.34375)+poserr); // Throw the previous error back into the grinder.
if ((pulses-int(pulses))>=.5) // Rooky Round Function
{
poserr=(pulses-int(pulses+1));
pulses=int(pulses+1);
}
else if (pulses-int(pulses)<.5)
{
poserr=(pulses-int(pulses));
pulses=int(pulses);
}
std::cout << "Command: " << degrees;
std::cout << " - Output: " << ((pulses/3600)*2.34375);
std::cout << " - Pulses: " << pulses;
std::cout << " - Error: " << ((poserr/3600)*2.34375);
std::cout << "\n";
std::cout << "\n";
degrees=(degrees+.01);
}
std::cin.get();
}
Last edited by OldRon; December 17th, 2019 at 04:22 PM.
-
December 18th, 2019, 04:24 AM
#2
Re: Round Function
To use round(), you need to insert
at the top of the program as this include has the definition for the round() function.
Also
Code:
degrees = (degrees + .01);
can be removed and incorporated into the for statement as:
Code:
for (degrees = 359.0; degrees < 360.0; degrees += 0.01)
when dealing with non-integer constants, it is better to explicitly specify this by using a . in the number. So 350 becomes 359.0 and 360 becomes 360.0 etc.
Also, if (pulses - int(pulses)) is not >= .5 then it must be < .5, so the second if statement is not required.
Code:
if ((pulses - int(pulses)) >= .5) // Rooky Round Function
{
poserr = (pulses - int(pulses + 1));
pulses = int(pulses + 1);
}
else
{
poserr = (pulses - int(pulses));
pulses = int(pulses);
}
As you have using namespace std; after the #include statements, your std::cout statements do not need the std:: part and can be just cout
Code:
cout << "Command: " << degrees;
2.34375 must 'mean something' in the context of this program, so it would be good practice to specify this value as a const variable at the top of the program and use this const variable instead of the number in the code.
Code:
const double special = 2.34375;
int main()
....
pulses = (((degrees * 3600) / special) + poserr);
Also note that the correct format for main() is
although the main() function doesn't have to explicitly return a value as 0 is used if none is specified.
Last edited by 2kaud; December 18th, 2019 at 04:27 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 18th, 2019, 10:47 AM
#3
Re: Round Function
OK, I included <cmath> and the error still exists. error C2065: 'round' : undeclared identifier
I created an APP in vb6 that performs a recursive folder/subfolder search, reads the folder files, and searches for a string. Starting in the VC98 folder there is no 'round'. Google has a lot of hits on that error and no solutions other than creating your own function. If I had a better understanding of the libraries then I could probably add my rounding function to math.h but I won't live long enough to reach that skill level so I'll be content with my rounding code. I have a lot to learn but I was surprised by how quickly I went from std::cout << "Hello World!" outputting the results that I need. That stupid lesson was where I threw the MS book in the trash when I was learning vb6. I appreciate your advice on the for statement and the unnecessary conditional statement.
Ron
-
December 18th, 2019, 11:02 AM
#4
Re: Round Function
Starting in the VC98 folder
Ah. You are using C++98. That explains the issue. In C++, round() was added in the later C++11 release. C++98 is now 22 years old and there has been a lot of changes to the C++ language since then. The latest version is C++17 - with C++20 due for release shortly. Why not upgrade to the latest free VC2019 Community edition - which includes C++17? See https://visualstudio.microsoft.com/downloads/
PS For learning C++ on line, have a look at https://www.learncpp.com/
Last edited by 2kaud; December 18th, 2019 at 11:07 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 18th, 2019, 11:24 AM
#5
Re: Round Function
Originally Posted by 2kaud
Ah. You are using C++98. That explains the issue. In C++, round() was added in the later C++11 release. C++98 is now 22 years old and there has been a lot of changes to the C++ language since then. The latest version is C++17 - with C++20 due for release shortly. Why not upgrade to the latest free VC2019 Community edition - which includes C++17? See https://visualstudio.microsoft.com/downloads/
PS For learning C++ on line, have a look at https://www.learncpp.com/
My use is for entertainment and my wife would say that doesn't warrant the expense of buying a new programming suite... and she's always right.
-
December 18th, 2019, 11:29 AM
#6
Re: Round Function
Originally Posted by OldRon
My use is for entertainment and my wife would say that doesn't warrant the expense of buying a new programming suite... and she's always right.
But VS2019 Community is free as I said in post #4
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 19th, 2019, 11:37 AM
#7
Re: Round Function
Although round() is a C++11 feature, there are 2 similar ones in C++98 which might be helpful:
ceil() - which rounds upwards, returning the smallest integral value that is not less than
floor() - which rounds downwards, returning the largest integral value that is not greater than.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 21st, 2019, 11:04 PM
#8
Re: Round Function
Originally Posted by 2kaud
Although round() is a C++11 feature, there are 2 similar ones in C++98 which might be helpful:
ceil() - which rounds upwards, returning the smallest integral value that is not less than
floor() - which rounds downwards, returning the largest integral value that is not greater than.
I had tried those functions but it was simpler to use (value - int(value)) + 1 if the fractional part was .5 or greater. I had Visual Studio 2010 Express on my drive so I installed it thinking that it would have round but that wasn't the case. It is the no license version but it required a serial key and MS let that page expire. After three chat sessions with MS it looked hopeless but I eventually found it without having to risk a download. I can run it in Windows 10 whereas the old software required a virtual XP machine. It would not not compile the simple program that it loaded when starting a new project. I don't know what it is but incremental compiling was the problem. I'm currently trying to learn about strings and it's sure a mess compared to vb6. My current project requires C++ so I'm eager to learn the programming language.
Thanks for your help,
Ron
Last edited by OldRon; December 21st, 2019 at 11:06 PM.
-
December 22nd, 2019, 01:22 AM
#9
Re: Round Function
Originally Posted by OldRon
I had tried those functions but it was simpler to use (value - int(value)) + 1 if the fractional part was .5 or greater. I had Visual Studio 2010 Express on my drive so I installed it thinking that it would have round but that wasn't the case. It is the no license version but it required a serial key and MS let that page expire. After three chat sessions with MS it looked hopeless but I eventually found it without having to risk a download. I can run it in Windows 10 whereas the old software required a virtual XP machine. It would not not compile the simple program that it loaded when starting a new project. I don't know what it is but incremental compiling was the problem. I'm currently trying to learn about strings and it's sure a mess compared to vb6. My current project requires C++ so I'm eager to learn the programming language.
Thanks for your help,
Ron
If the old link to the 9 year old VC 2010 is no longer available it just might be a hint from MS to go to something newer - like the FREE version of VC 2019.
-
December 22nd, 2019, 04:41 AM
#10
Re: Round Function
I had Visual Studio 2010 Express on my drive so I installed it thinking that it would have round but that wasn't the case
VS 2010 is based upon C++98 - as C++11, which is needed for the round function, didn't exist at that point.
As you're using Windows 10, installing the free VS 2019 Community should be a no-brainer!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 22nd, 2019, 09:51 AM
#11
Re: Round Function
Originally Posted by Arjay
If the old link to the 9 year old VC 2010 is no longer available it just might be a hint from MS to go to something newer - like the FREE version of VC 2019.
Of course you are right. If I'm going to learn C++ then I just as well be current.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|