C++ "Too many initializers" Error while making an array of chars - c++

I'm new to C++. I know a lot of python but I'm extremely new to C++. I was creating an array of chars but I got this error- "Too many Initializers" in VSCode.
Please let me know how to fix it.
Here's the code
1 | class Board {
2 | public:
3 | char pos_list[9];
4 |
5 | void reset_pos() {
6 | pos_list[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
7 | };
8 | };
I am getting this error in line 6.
Please help me :(

EDIT: My initial answer was not correct, please find the modified correct way to do what you are looking for:
You will not be able to use {'','',''} in order to assign empty values to all elements in the array in C++, you can only do that while initializing the array when you declare it. Also, it wouldn't be ideal because it would use hardcoding of ' ' across the entire length of the array. The better way to do this is to loop over the array and then set each element to empty like below:
void reset_pos() {
int len = sizeof(pos_list)/sizeof(pos_list[0]);
for(int i=0; i<len; i++){
pos_list[i] = ' ';
}
};

Related

Interactive grid removes spaces

Look at the query and result generated in sqlplus :
SELECT
plan_line_id,
LPAD (a.plan_operation || ' ' || a.plan_options,
LENGTH (a.plan_operation || ' ' || a.plan_options) + a.plan_depth * 3
) OPERATION,
a.plan_cost COST,
output_rows "ROWS"
FROM gV$SQL_PLAN_MONITOR a
WHERE sql_id = '8gan9d0z7bzhm'
Looks pretty cool doesn't it ;)
And the same result in grid report :
Apex 21 removes all spaces generated by lpad function. The same problem is with classic report and classic grid :( any idea why ?
You'll have to use a character which can be correctly interpreted by your browser - instead of a space, use a non-breaking space, the &nbsp character.
Something like this (I'm using the # character in the CTE, which is then replaced by :
WITH
temp
AS
(SELECT plan_line_id,
LPAD (
a.plan_operation || ' ' || a.plan_options,
LENGTH (a.plan_operation || ' ' || a.plan_options)
+ a.plan_depth * 3,
'#') OPERATION, --> here
a.plan_cost COST,
output_rows "ROWS"
FROM a1_test a
WHERE sql_id = '9p4xcx2gd8u49')
SELECT plan_line_id,
REPLACE (operation, '#', ' ') operation, --> here
cost,
"ROWS"
FROM temp
Turn "Operation" column's **Escape special characters" property OFF.
The result is then
Not as pretty as you'd want it to, but it is better than what you currently have.

Invalid Syntax with Print Statement

I have a line of code that is saying that there is invalid syntax in my print statement. Does anyone know about how to fix this? I've tried deleting the parentheses and have tried changing the +'s to ,'s.
print(stockName[random] + ' - $' + Names[0, amod] + ' : You have ' + x + ' of this stock')
If you use ' x ' with + operator it should be a string else you should use coma.

Problems with strip function in generating list of lists

I'm having a slight problem with this function. I'm trying to generate a list of lists. For each item in those lists I need to remove the space to the left and to the right of each item. For some reason it's only working for the first item in each list.
def filereadlistoflists():
master_list=[]
input_file = open('movies.txt','r')
for line in input_file:
add_line = line.strip().split(",")
master_list.append(add_line)
return master_list
Here's the result:
[['Brad Pitt', ' Sleepers', ' Troy', ' Meet Joe Black', ' Oceans Eleven', ' Seven', ' Mr & Mrs Smith'], ['Tom Hanks', ' You have got mail', ' Apollo 13', ' Sleepless in Seattle', ' Catch Me If You Can']

Python- How to change lists order in a list of lists

I have a list of lists that i want to put in order with the times that a person leaves he's job. I already made a code to give me only the time but i need something to check the times and put them in order.
inFile=removeHeader(file_name) # the information is taken from a txt file and this only gives me the part of the services
#print inFile gives me list of lists like [['Peter', ' 06-CB-89', ' Xavier', ' 09:45', ' 10:15', ' downtown', ' 10', ' standby'], ['Robert', ' 13-KI-54', ' Paul', ' 09:30', ' 10:30', ' Castle', ' 45', ' standby']
for time in inFile:
hours=time[4]
print hours #this gives me only the hours they left work
see https://docs.python.org/2/library/functions.html#sorted
you have to sort the main list by the leave time i.e. the 5th value of the sublists, the key argument of sorted alows you to specify a function that will be called to compute keys the list is sorted based on:
import time
x =[['Peter', ' 06-CB-89', ' Xavier', ' 09:45', ' 10:15', ' downtown', ' 10', ' standby'], ['Robert', ' 13-KI-54', ' Paul', ' 09:30', ' 10:30', ' Castle', ' 45', ' standby']]
sorted(x, key = lambda x: x[4])

C macro to transform a SVN revision to an integer

I am looking for a C/C++ macro that can transform a random SVN revision like "$Revision: 9 $" or "$Revision: 9999999 $" into an integer or a string.
I know that simple functions exists to achieve this, but I want this to be made at compile time.
My wish is to write things like:unsigned int rev = SVN_TO_INT("$Revision$");
I agree that you can't work with strings at compile time through macros or templates. So.... don't use strings.
This is an ugly hack, but I think it meets all your requirements. I don't recommend it.
#define $Revision struct REV_STR { unsigned foo
#define $ * 64; };
$Revision: 4521 $
enum { REV = sizeof(REV_STR) / 8 };
#undef $Revision
#undef $
#include <iostream>
int main()
{
std::cout << REV << std::endl;
return 0;
}
// $ g++ -Wall -Wextra revision.cpp && ./a.exe
// revision.cpp:4: warning: width of `REV_STR::foo' exceeds its type
// 4521
I'm relatively sure that this isn't possible with a macro.
It may be possible with template metaprogramming, but I've never gone near it.
It would also be possible with a pre-build script that replaces SVN_TO_INT with your desired text.
I don't understand why you want this, though, since it would be just as easy to hardcode the version number since you know it at compile time.
You can't do the kind of string manipulation you want at compile time with the C preprocessor (macros) or with templates (C++). You'll need to use an external utility or script that you can invoke from the build process.
Some utilities/scripts/code that might help you:
SvnRev: http://www.compuphase.com/svnrev.htm
http://blog.guymahieu.com/2008/06/09/getting-the-svn-head-revision-number-from-a-windows-batch-file/
http://www.codeproject.com/KB/architecture/svn_visual_studio.aspx
My solution is
#define $Revision (false?1
#define $ +0)
int codeRevision() { return $Revision: $; }
#undef $Revision
#undef $
You can't fully work with strings at preprocessing or compile time - but why not use something something like:
int svn_version()
{
static const int v = extract_svn_version(REVISION);
return v;
}
I agree that it is not doable with macros, however, I found a trick using compiler optimizations.
The result is that the expression JL_SvnRevToInt("$Revision: 12345 $") is reduced to a single unsigned integer: 12345
inline unsigned int JL_SvnRevToInt(const char *r) {
if ( r == NULL || r[0] == '\0' || r[10] == '\0' || r[11] == '\0' || r[12] == '\0' || r[13] == '\0' )
return 0;
const unsigned int count =
r[11] == ' ' ? 1
: r[12] == ' ' ? 10
: r[13] == ' ' ? 100
: r[14] == ' ' ? 1000
: r[15] == ' ' ? 10000
: r[16] == ' ' ? 100000
: r[17] == ' ' ? 1000000
: r[18] == ' ' ? 10000000
: r[19] == ' ' ? 100000000
: 0;
return
(r[11] == ' ' ? 0 : (r[11]-'0') * (count/10) +
(r[12] == ' ' ? 0 : (r[12]-'0') * (count/100) +
(r[13] == ' ' ? 0 : (r[13]-'0') * (count/1000) +
(r[14] == ' ' ? 0 : (r[14]-'0') * (count/10000) +
(r[15] == ' ' ? 0 : (r[15]-'0') * (count/100000) +
(r[16] == ' ' ? 0 : (r[16]-'0') * (count/1000000) +
(r[17] == ' ' ? 0 : (r[17]-'0') * (count/10000000) +
(r[18] == ' ' ? 0 : (r[18]-'0') * (count/100000000) +
(r[19] == ' ' ? 0 : (r[19]-'0') * (count/1000000000) +
0)))))))));
}
It supports9 digits revision number, NULL and empty and "$Revision$" strings.
If you have a makefile based build system you can make a special rule that creates a file every maketime.
.PHONY: svn_revision.c
svn_revision.c:
echo -n "int svn_revision = " > svn_revision.c
svn info | grep Revision | cut -f2 -d" " >> svn_revision.c
echo ";" >> svn_revision.c
svn_revision.o: svn_revision.c