|
Yeah, that might go through a C++ compiler, but it's not really C++. I just didn't want to pile on.
OP - debugger is your friend.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
#include<iostream>
using namespace std;
int main()
{
const int b=14;
const float f=0.20;
cout<<"----------------"<<endl;
cout<<"BOOK LOAN SYSTEM"<<endl;
cout<<"----------------"<<endl;
cout<<"Enter the number of books : ";
int n;
cin>>n;
cout<<"\nEnter the days of the loan : ";
int l;
cin>>l;
cout<<"\n-------------------------------------"<<endl;
cout<<"Days overdue : ";
if(l>b){
cout<<l-b;
cout<<"\nFine : RM "<<(l-b)*(f)*n;
}
}
|
|
|
|
|
Do you mean in this snippet was used some other than C++ language? 
|
|
|
|
|
Did you mean to say "Help me convert this code I found on the internet to C so I can turn it in as my own work?"
|
|
|
|
|
it's already C++.
What are you wanting ? classes ?
I suggest using meaningful variable names instead of b or f or n.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
If you mean to replace std::cout and std::cin with stdio.h functions, then look up the usage of printf() and scanf(), e. g. at C Library - <stdio.h> - Tutorialspoint[^]
If you mean to convert everything to C, you also may need to remove the const qualifiers depending on the exact C compiler (and version) you are using.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
I'm trying to create an array of pointers to a common base class, but each pointer in the array will point to a different derived class instance. This is a 64-bit application.
Here's the critical portion of my code: (Unnecessary detail has been removed.)
Intellisense is warning me that the second pointer assignment (to the second element in the array) will cause a buffer overrun.
It says, "C6386: Buffer overrun while writing to 'this->m_Members': the writable size is 'this->m_MemberCount*8' bytes, but '16' bytes might be written."
I have never come across this warning before, and I wanted to ask the community if there is something obviously wrong with the code that I'm not seeing.
m_MemberCount = 7;
CGsUCharType* m_s_b1 = new CGsUCharType(); CGsUCharType* m_s_b2 = new CGsUCharType();
CGsUCharType* m_s_b3 = new CGsUCharType();
CGsUCharType* m_s_b4 = new CGsUCharType();
CGsUShortType* m_s_w1 = new CGsUShortType();
CGsUShortType* m_s_w2 = new CGsUShortType();
CGsULongType* m_S_addr = new CGsULongType();
CGsTypeBase** m_Members = new CGsTypeBase*[m_MemberCount];
m_Members[0] = m_s_b1;
m_Members[1] = m_s_b2; m_Members[2] = m_s_b3;
m_Members[3] = m_s_b4;
m_Members[4] = m_s_w1;
m_Members[5] = m_s_w2;
m_Members[6] = m_S_addr;
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Your code should work (Intellisense is a little confused here), but may I suggest using std::vector instead:
#include <vector>
...
std::vector<CGsTypeBase*> m_Members(m_MemberCount);
This will ensure that your array is properly constructed and is destructed when it goes out of scope.
You may also wish to look into storing smart pointers (rather than raw pointers) in the vector. This can help ensure that they are destructed at the right time, as well.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Thank you for taking the time to respond. I will consider using vector instead.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
What are the definitions of CGsTypeBase and CGsUCharType ?
[edit]
I built this short program and it compiled and linked successfully:
struct CGsTypeBase
{
char foo[8];
};
struct CGsUCharType : CGsTypeBase
{
wchar_t foo[8];
};
struct CGsUShortType : CGsTypeBase
{
uint16_t foo[8];
};
struct CGsULongType : CGsTypeBase
{
long foo[8];
};
int main(
int argc,
char* argv[]
)
{
int m_MemberCount = 7;
CGsUCharType* m_s_b1 = new CGsUCharType(); CGsUCharType* m_s_b2 = new CGsUCharType();
CGsUCharType* m_s_b3 = new CGsUCharType();
CGsUCharType* m_s_b4 = new CGsUCharType();
CGsUShortType* m_s_w1 = new CGsUShortType();
CGsUShortType* m_s_w2 = new CGsUShortType();
CGsULongType* m_S_addr = new CGsULongType();
CGsTypeBase** m_Members = new CGsTypeBase*[m_MemberCount];
m_Members[0] = m_s_b1;
m_Members[1] = m_s_b2; m_Members[2] = m_s_b3;
m_Members[3] = m_s_b4;
m_Members[4] = m_s_w1;
m_Members[5] = m_s_w2;
m_Members[6] = m_S_addr;
return 0;
}
So how does that differ from your code?
[/edit]
modified 6-Jul-21 3:54am.
|
|
|
|
|
Yes, my code compiles and links successfully too. I guess it's just intellisense that has a problem. Thank you for your response.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
A wild guess: the intellisense does not think (like, instead, you assume) that m_MemberCount is equal to 7 .
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
That's what I think as well. I'm glad to know someone else has the same idea. I did come across a few posts about that warning being incorrect.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Richard Andrew x64 wrote: CGsTypeBase** m_Members = new CGsTypeBase*[m_MemberCount];
As others have pointed out the your solution works because of the way the memory lays down.
In the above you have a declaration of a variable which has a type. And you also give the variable a value. But the declaration of the variable is a pointer. Just a pointer. Not a pointer to an array. So when intellisense sees you use it as an array it goes basically 'pointer + 1' isn't going to work. It does works because of the value and not the declaration.
I believe there is a declaration form that allows you to specify the form that would make intellisense happy but been a long time since I attempted C++ so I will leave it to someone else to come up with the form that makes the array specific.
|
|
|
|
|
Thanks for your input. I appreciate you taking the time to post.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
We can put a "u" after a numeric literal to indicate that it should be compiled as an unsigned int.
Or we can put "ll" to indicate that it should be compiled as a long long.
But what is the correct character to indicate that it should be compiled as a short?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
There isn't one. You'd have to cast it: short(0) .
EDIT: I prefer to use int16_t or uint16_t rather than assuming that's what I'll get. But if you're working with code that already uses short , go ahead and use it.
|
|
|
|
|
Thanks, Greg.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Does anyone know if it's possible to debug a user application from a Windows service, or if there is something that prevents this?
I suppose I could find out by just trying it, but I'd rather not waste time creating and installing a skeleton service just to see that it's not possible.
I'm hoping someone might know one way or the other.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Services run, by default, under the LocalService account, which is an extremely limited account. You can set your service to run as one of the predefined LocalService, NetworkService, or LocalSystem accounts, OR you can create your own account, assign privileges to it, and run your service under that account.
Note that use of LocalSystem is not recommended, as it essentially gives your service access to anything on the platform!
If you are running a debugger under a service, you may wish to allow the service to interact with the user.
Service User Accounts - Win32 apps | Microsoft Docs
LocalService Account - Win32 apps | Microsoft Docs
LocalSystem Account - Win32 apps | Microsoft Docs
NetworkService Account - Win32 apps | Microsoft Docs
Interactive Services - Win32 apps | Microsoft Docs
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
It rather depends on what you mean, and how you think a service could attach itself to a user application, and what information it could collect.
|
|
|
|
|
When writing a service, I always add a way to pass a command line parameter to run the app in debug mode as a console app. Many examples do the same thing.
In other circumstances, you can attach to the service. Without the proper architecture, this can run into timeout issues.
|
|
|
|
|
Thanks for your response. I was asking more about using the service as a debugger and having it attach to a user process.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Hi,
Can some one help me?. I need to make a window with multiple viewports like in 3DS MAX with c++, Each viewport must be rendered with OpenGL with separate mouse actions
|
|
|
|
|
I suggest you read this[^] and then edit your post to provide more detail
|
|
|
|