C/C++ :: Setting Constants As Attribute?
Apr 3, 2015
I would like to know how can i set a constant attribute in the constructor. This attribute is an int value that cannot be changed.
For instance:
class Test {
public:
const int x;
public:
Test(const int val);
[code].....
With this code i get compile error!
View 5 Replies
ADVERTISEMENT
Sep 6, 2014
I have tried googling, so, reddit, irc, and various other forums. Anyways, I am writing a todo list app in c. It generates an xml tree that looks something like this: [URL]... . The problem where I am struggling, is in the function todo_complete, I try to make the attribute of an item switch from false to true. it does it without error, but I can't make it stay that way. here is the code: [URL]...
View 3 Replies
View Related
Mar 27, 2013
I want to create a map contaning a regex as key and a char* as an attribute , like:-
map<regex, char*> res;
res["[[:alnum:]]"] = "alphanumeric text";
but the following error is generated:-
vector<pair<regex,string>>res;
res.push_back(make_pair("[[:alnum:]]","alphanumeric"));
but the error generated is:-
no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=std::pair<std::regex, std::string>, _Alloc=std::allocator<std::pair<std::regex, std::string>>]" matches the argument list
argument types are: (std::pair<const char *, const char *>)
object type is: std::vector<std::pair<std::regex, std::string>, std::allocator<std::pair<std::regex, std::string>>>
View 13 Replies
View Related
Dec 9, 2013
I'm having the same problem : [URL] .....
Though, my vector isn't one of int, it is a vector of objects from another class and NetBeans won't compile it.
#include <cstdlib>
#include<vector>
#include <string>
#include<iostream>
using namespace std;
class estoque{
[Code] .....
View 3 Replies
View Related
Aug 2, 2013
I have a input record like
acct|N|Y|N|N|rose@gmail.com
Now I need to create a logic to append a code to the end of the file using the following matrix rules.
00NNNN
01NNNY
02NNYN
03NNYY
04NYNN
05NYNY
06NYYN
07NYYY
[code].....
In the above example these four flags are "N|Y|N|N", so I need to append the matching code at the end of file "04".
desired output :
acct|N|Y|N|N|rose@gmail.com|04|
as it matches code '04':
04NYNN
View 5 Replies
View Related
May 8, 2014
I am trying to create a custom ValidationAttribute in my c# .net code. I have done a lot of searching and have code that should be working but the IsValid method is not firing as far as I can tell.
Model code:
[Required(AllowEmptyStrings = false, ErrorMessage = "required")]
//[Range(2014, 2100, ErrorMessage="Please enter a valid year.")]
//[RegularExpression(@"^d{4}$", ErrorMessage = "Please enter a valid year.")]
[ValidYear(ErrorMessage="Please enter one >= 2014")]
[Display(Name = "Exp. Year")]
public string expYear { get; set; }
And this is the class in that same model:
public class ValidYearAttribute : ValidationAttribute {
protected override ValidationResult IsValid(object value, ValidationContext validationContext){
return new ValidationResult("Something went wrong");
}
}
Why the IsValid is not firing.
View 3 Replies
View Related
Mar 27, 2013
I would like to modify attributes like modification/creation dates.The function is correctly working as if I type in "ls -al", the timestamp is correct. But when using my program to read these attributes, it returns the "real" modification/creation date. Here is the function that shows the timestamp :
Code:
time_t t = sb.st_mtime; struct tm tm = *localtime (&t);
char s[32];
strftime (s, sizeof s, "%d/%m/%Y %H:%M:%S", &tm);
printf ("%-14s %s", lecture->d_name, s); And here is the code for modifying the timestamps : Code: void modifyAttributes(char * file, int mtime, int atime)
}
[code]....
View 9 Replies
View Related
Nov 21, 2013
What's the problem with the following:
Code:
#define K 3;
int max(int a, int b) {
return a>b? a : b;
} int main() {
cout<<max(K, K+3);
return 0;
}
Why is it not allowed, and how is it different from:
Code:
int max(int a, int b) {
return a>b? a : b;
} int main() {
cout<<max(3, 3+3);
return 0;
}
View 3 Replies
View Related
Nov 18, 2013
I am working on a code that is suppose to get vowels and consnants from a string. So far i got up to trying to get the vowels from a string. this is what i have so far:
#include <iostream>
#include <string> // includes go into header
using namespace std;
int getword(string word);
int getvowels(string word);
[Code] .....
View 2 Replies
View Related
Nov 19, 2014
I have a templated class that can either use float or double as type.
My question is now: What do I do with constant numbers in the code?
Let's assume I have a multiplication by 0.5:
In the case of float type, I want: 0.5f
In the case of double type, I want: 0.5
One answer would be to check for every constant the type and doing an if/else, but this is very annoying with lots of constants.
The code using these constants infer their type automatically by the assignment, that's why I have to take care of the constants.
View 1 Replies
View Related
Jun 29, 2014
"A constant, like a variable, is a memory location where a value can be stored. Unlike variables, constants never change in value. You must initialize a constant when it is created. C++ has two types of constants: literal and symbolic.
A literal constant is a value typed directly into your program wherever it is needed. For example, consider the following statement:
long width = 5
This statement assigns the integer variable width the value 5. The 5 in the statement is a literal constant. You can't assign a value to 5, and its value can't be changed.
The values true and false, which are stored in bool variables, also are literal constants.
A symbolic constant is a constant represented by a name, just like a variable. The const keyword precedes the type, name, and initialization. Here's a statement that sets the point reward for killing a zombie:
const int KILL_BONUS = 5000;
Whenever a zombie is dispatched, the player's score is increased by the reward:
playerScore = playerScore + KILL_BONUS;
If you decide later to increase the reward to 10,000 points, you can change the constant KILL_BONUS and it will be reflected throughout the program. If you were to use the literal constant 5000 instead, it would be more difficult to find all the places it is used and change the value. This reduces the potential for error."
what's the difference? Here is a program to demonstrate what I'm having trouble conceptualizing.
#include <iostream>
using namespace std;
int main() {
int width = 10, length = 10;
int area = width * length;
cout << "Width: " << width << endl;
cout << "Length: " << length << endl;
cout << "Area: " << area << endl;
return 0;
}
Now, why would it be harder to go in and changed a regularly defined integer than one defined with the 'const' keyword proceeding it? For example, the width and length variables. My confusion comes from the point that they seem to both simply be variables with a value assigned to them. I feel as if the process of having to change a literal constant's value is synonymous to the process of having to change a symbolic constant's.
View 3 Replies
View Related
Mar 6, 2015
was just curios to know if there is a way to make the preprocessor compare against constants defined like this: Code: uint uint_max = -1;
View 1 Replies
View Related
Oct 13, 2014
I need to write a program in which you do the following:
Define three named constants using the appropriate data types:
DEC_NUM = 65;
HEX_NUM = 0x7a;
LETTER = 'f';
Then display each of these constants in decimal, in hexadecimal, and as a character using cout. Your program will have a total of nine cout statements.
View 1 Replies
View Related
Feb 17, 2014
My assignment is to create a simple stock broker program that ask the user how much they are willing to invest and ask what company they would like to invest in. Finally it outputs how many shares the user will have based on their investment amount. My code is below. My professor said to declare symbolic constants and factor out the if else statements. Ive been struggling trying to understand constant variables. How do I use const variables to factor out the if else statements?
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
//Declare Variables
const double BAC = 16.7;
const double Citigroup = 49.52;
[code]....
View 4 Replies
View Related
Sep 28, 2013
why strcmp() doesn't return true when comparing a string constant with a string that was acquired via a linked list. By the way, the data in the linked list was taken from a text file. Does that imply that there's a new line () character in the string from the linked list?
Code:
struct Node{
char ACNO[15];
struct Node *next;
[Code]....
View 1 Replies
View Related
May 23, 2014
I'm playing around and wrote a tiny program.
Code:
char bracketin[] = "thisgetsbracketed.txt";
char bracketout[] = "bracketed.txt";
char testwalk[10] = "12345678";
[Code]....
I'm incrementing the pointer to buffer 150 bytes beyond its reserved 50. I see testwalk, followed by bracketout, followed by bracketin printed by the overflow on buffer.
The memory locations are ordered descending from their call order. Why is this the case?
One would think that they would be written in ascending order as I call them. I can only assume that they're compiled bottom up - where could I read about this process?
View 3 Replies
View Related
Mar 1, 2013
Lets say i have an array with the values 1, 5, 9, and 3. is there anyway to make this so i can have an int with the value 1593 based on those numbers in the array?
View 8 Replies
View Related
Feb 22, 2013
I want to set limit on cin for example
int i;
cout<<"Please enter 4 digits id: ";
cin>>i
If user enter more then 4 digits it must give an error
View 5 Replies
View Related
Apr 5, 2014
This function below takes a pointer as an argument. What I expect to happen is, since expr++ has higher precedence than *expr, that is, the primary expression operators have higher precedence than the unary operators, pointer arithmetic should occur where we increment to the second address pointed to by dbuf, and then we should dereference the value at that address. Given that logiv, when i print dbuf[3] it should print the value pointed to at the 4th address in dbuf. However, the value it returns is 0x0 not 0x3. Why doesn't it dereference the value 0x3?
Code: void dfill(unsigned char *dbuf)
{
dbuf = (unsigned char*)malloc(4);
memset(dbuf, 0, 4);
*dbuf = 0x0;
*dbuf++ = 0x1;
*dbuf++ = 0x2;
*dbuf++ = 0x3;
printf("dbuf val: 0x%x
", dbuf[3]);
}
View 1 Replies
View Related
Oct 18, 2013
I am trying to create the lparam for the WM_KEYDOWN message. I know this is the C programming forum, but my code is standard enough to apply here. According to Microsoft, the lparam must be formatted like this : lParam The repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown following. Bits Meaning 0-15 The repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent.
However, the repeat count is not cumulative. 16-23 The scan code. The value depends on the OEM. 24 Indicates whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0. 25-28 Reserved; do not use. 29 The context code. The value is always 0 for a WM_KEYDOWN message. 30 The previous key state.
The value is 1 if the key is down before the message is sent, or it is zero if the key is up. 31 The transition state. The value is always 0 for a WM_KEYDOWN message. ( WM_KEYDOWN message (Windows) )^ I'm troubled by setting it into the 32-bit value, and I get confused about what exactly happens when the logical or and the bitshift operators are used. I tried to use them below in my code by looking at Stack Overflow for setting a bit. I also don't know how to test for the endianess of my system, and how to handle it if it's big endian or little endian. Here is my code so far :
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
[Code].....
View 2 Replies
View Related
Oct 27, 2013
I have the following situation:
Code:
void myFun(float *pfMyPtr) {
float Val[] = {0.234, 0,432, 0.322, 0762, 0.984};
pfMyPtr = Val;
}
int main() {
float *pfPtr;
pfPtr = (float*) calloc (5,sizeof(float));
myFun(pfPtr);
}
I would like pfPtr to contain the values of array Val. What am I missing here?
View 5 Replies
View Related
Oct 29, 2014
I need to set a function to a variable of some kind. Then later in the program it needs to run the function that is set to the variable. The variable doesn't need to change after it is set to a function, it just needs to be able to be set to a function. So maybe I don't need a variable? What do I do? :3 Is this even possible? :o
Example:
if (PosRampYes == 0)
{
SomeVariableOrSomething = FirstFunction();
}
else
{
SomeVariableOrSomething = SecondFunction();
}
//later:
SomeVariableOrSomething; //so if PosRampYes is set to 0 then this line would run FirstFunction()
View 2 Replies
View Related
Oct 8, 2013
I am trying to find a quicker way of setting the variables for text. here is my code:
sf::Text set_text_values(sf::Text text, sf::Font font) {
text.setFont(font);
text.setCharacterSize(50);
text.setColor(sf::Color::White);
return text;
[Code] ....
And I have tried it like this
void set_text_values(sf::Text text, sf::Font font) {
text.setFont(font);
text.setCharacterSize(50);
text.setColor(sf::Color::White);
[Code] ....
The code compiles and runs but it won't show the text on the screen like it did when i did it manually like this:
int main() {
Hull.setFont(font);
Hull.setCharacterSize(50);
Hull.setColor(sf::Color::White);
}
What is wrong with it?
View 2 Replies
View Related
Sep 18, 2013
I'm trying to set all the elements of my array to 0 but nothing seems to be working.
.h
#ifndef ServerGroup_h
#define ServerGroup_h
class ServerGroup {
public:
ServerGroup(const int&);
[Code] .....
I want to set each element of the array in servers to 0 based on what is passed into size by numArray.
View 9 Replies
View Related
Feb 20, 2013
I declared a pointer in main with value 0, so I want to change its value so that it points to other variable from a function, I guess the function creates a copy of my pointer that's why whatever I do within function doesn't change the real direction of the pointer in main. I've been trying something like this:
#include <stdio.h>
void redirectionate(char *str, char *ptrCopy);
int main()
{
[Code]....
View 7 Replies
View Related
Jun 23, 2014
I created a WinForm app in C# using VS 2013 Express.
I added code to create a Global Hot Key on the main form. This works fine. My hot key is Ctrl-T. I can press the hot key and make the main form show and hide.
Then I created a second form (ChecklistForm) and now I want to press ctrl-T and make that form show and hide. I do not need the main form to do this any more. I just used the main form to test my Global Hot Key code.
So I'm having trouble getting the second form to respond to the hot key. When I put a break on the WndProc(), there is no break.
namespace ChecklistFSX {
public partial class MainForm : Form {
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(String sClassName, String AppName);
private IntPtr thisWindow;
private GlobalHotKeys hotkey;
public MainForm()
[code]....
View 2 Replies
View Related