Converting time_t to an int - c++

I want to convert the unix timestamp returned by time() as time_t to an integer. I've been searching for a solution for 20 minutes, and decided to ask here.
Every solution I have found has not worked. When trying to cast from time_t to int, I get errors:
long int t = static_cast<long int> time(NULL);
error C2061: syntax error : identifier 'time'
error C2146: syntax error : missing '(' before identifier 'time'
I am very very new to C++. Thanks in advance.

time_t is already an integer, though it's deliberately chosen to be one that stores the system's full range of UNIX time, so I would recommend against this cast.
However, if you insist, you're on the right lines but just got the cast syntax wrong.
In general, statically casting e to T looks like this:
static_cast<T>(e) // <-- parentheses!
Just as the error message told you, you are "missing '(' before identifier 'time'".
So, your expression will be:
long int t = static_cast<long int>(time(NULL));

Just read the errors and insert the 'missing ( before identifier time':
long int t = static_cast<long int>(time(NULL));
static_cast requires the value to be encapsulated in parentheses.

simply add parenthesis around time(NULL):
long int t = static_cast<long int>(time(NULL));

Related

error assigning values to a string literal array after definition

when I try to intialize the string literal array at the point of the definition it works fine :
char* arr[3] = { "ahmed" , "saeed" , "ahmed" };
But when I try to assign these values to it after the point of definition it gives me an error starting from the second value :
char* arr[3] ;
arr[3] = { "ahmed" , "saeed" , "ahmed"};
what's the difference here and whats going on?
error:
Severity Code Description Project File Line Suppression State
Error (active) E0146 too many initializer values structs_consts
Severity Code Description Project File Line Suppression State
Error C2440 '=': cannot convert from 'initializer list' to 'char *' structs_consts
Thank you.
The difference is that one is initialisation, and the other is [attempted] assignment. Those are different constructs.
You cannot assign to an array. You just can't. Even if you could, you [probably] wouldn't be able to do it with initialisation syntax.
It's a rule from C. Why? No idea.
Once the array exists, you'll have to assign to its elements individually (be that directly, or via std::copy, or memcpy or some other such thing).
The specific compiler error is because you tried to do this using the expression a[3], but that is not the name of the array. The name of the array is just a. a[3] is an element that does not exist.
The utterance of a[3] in the declaration means something different. Yes, it's confusing.
How about a nice game of std::array<std::string_view, 3>?

invalid conversion from 'const char*' to 'char*[-fpermissive](might be a multi dimensional array problem, not sure)

I am trying to write a code that deletes a word in a array of strings.
this is my whole code
int cancella(char v[],int nv,char ele,char vt[]){ int i,j;
for(i=0;i<nv;i++){
if (strcmp(v[i],ele))!=0;{
strcpy(vt[j],v[i]);j++
}
Return j;
}
}
int main()
{
char a[DIM][L]={"pane","pizza","pasta","cafe","panino","kebab","patatine"};
char aT[DIM][L];
int naT,na=7;
char elem={"kebab"};
nat= cacella(a,naT,elem,aT);
cout<<nat;
}
How to fix the Error
invalid conversion from 'const char*' to 'char*' [-fpermissive]
(might be a multi dimensional array problem, not sure)
at:
if (strcmp(v[i],ele))!=0;{
There are many problems with this code.
Incorrect indirect level at many places (for ex. char ele instead of char *ele).
Variables used before initialisation (for ex. naT; should probably be na instead).
Improper indentation and formatting.
Some suggestions
Write only a few line at a time and verify that it compile.
Read error message carefully. If you don't understand it, then see your compiler help.
Usually, it is easier to fix error at the top first.
As some errors are dependent on previous errors, compile again after fixing some errors.
Be careful with punctuation. Compiler care about misplaced parenthesis or semi-colon.
Read again your course notes or find a good C++ book.

initialization with ' ... ' expected for aggregate object

I'm trying to write a code in C++ that allows you to enter some text and it will open a website with the variable s_input appended to it. However, I get this error:
'system' : cannot convert parameter 1 from 'std::string' to 'const
char *'
I get that error for the last line you see.
cin >> s_input;
transform(s_input.begin(), s_input.end(), s_input.begin(), tolower);
s_input = "start http://website.com/" + s_input + "/0/7/0";
system(s_input);
I am new to C++ and this is more of a learning program.. So please show as many examples as possible! Thanks!
If s_input is a std::string (I'm betting it is):
system(s_input.c_str());
The function system takes a const char* as parameter, as the error message clearly states.

Having trouble defining a map for stock dividends

I am trying to write some code to calculate dividend yields. I need to calculate the dividend yield (dividend/stock_price) daily. Dividends are constant. Eventually, I will have to tie the stock price to a dynamic feed, but in the meantime the stock prices may be in their own map. I inserted the dividend amounts for each stock in the class constructor because it is less computationally cumbersome than using conditional statements in the members (i.e if stock = Apple, then dividend is X). I am getting the following error message starting at the '[' before the "AAPL":
**error C2679: binary '[' : no operator found which takes a right-hand operand of type 'const
char [5]' (or there is no acceptable conversion) c:\boost/unordered/unordered_map.hpp(415):
could be 'double &boost::unordered_map<K,T>::operator [](const FinModels::Instrument *const &)'
with
[
K=const FinModels::Instrument *,
T=double
]
while trying to match the argument list '(DividendMap, const char [5])'**
Can anyone help me based on my brief code below and description? Is const Symbol* incorrect type for the key?
Also, if it's bad convention to post the fixed map values in the constructor, please let me know what is better.
Header File
public:
typedef boost::unordered_map<const Symbol*, double> Dividend_Map;
typedef Dividend_Map::iterator Dividend_MapIterator;
private:
Dividend_Map p_dividend_map;
.CPP File
p_dividend_map["AAPL"] = 0.01;
p_dividend_map["BAC"] = 0.01;
p_dividend_map["C"] = 0.01;
The key type for your map is a Symbol *, and you're trying to stick a bunch of const char * into the map. This will not work unless Symbol is typedef'd as char.
Create a Symbol object out of each symbol you want to store in the map and then add the address of that to the map.
Symbol appl("AAPL"); // assuming Symbol has a constructor taking a const char *
p_dividend_map[&appl] = 0.01;
In the example above, the lifetime of aapl must match its lifetime as a member of the map. If the lifetime of aapl expires you'll have the map pointing to an invalid memory location.
You may want to change the map to
typedef boost::unordered_map<Symbol, double> Dividend_Map;
Then use it as
Symbol appl("AAPL");
p_dividend_map[appl] = 0.01;

Converting textbox string to float?

I'm basically trying to write a basic converter in visual studio 2008, and I have 2 text boxes, one which gets input from the user, and one which gives output with the result. When I press the button I want the input from the first textbox to multiply by 4.35 then display in the 2nd textbox. This is my code in the button code so far:
String^ i1 = textBox1->Text;
float rez = (i1*4.35)ToString;
textBox2->Text = rez;
However I'm getting these errors:
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(148) : error C2676: binary '*' : 'System::String ^' does not define this operator or a conversion to a type acceptable to the predefined operator
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(148) : error C2227: left of '->ToString' must point to class/struct/union/generic type
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(149) : error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'float' to 'System::String ^'
Please help I'm going insane on how ridiculously difficult it is to get some input from a textbox in C++. I've googled every error I had and nothing useful came up, I've been searching answers for an hour already, please help.
Fixing it for you,
String^ i1 = textBox1->Text;
float rez = (float)(Convert::ToDouble(i1)*4.35);
textBox2->Text = rez.ToString();
Basically, you want to convert your string to an actual number, do the math, and then make it back into a string for displaying purposes.
You're trying to multiply a string by a double and there is no operator that defines how to do that. You need to convert your string to a double first, and then use that in the calculation.
Then, you're trying to assign a string to a float, which again is nonsense.. You need to calculate the float, then convert it to a string when assigning it to the textbox text field.
Something like:
String^ i1 = textBox1->Text;
float rez = (Convert::ToDouble(i1)*4.35);
textBox2->Text = rez.ToString();