error C2143: syntax error : missing ')' before 'constant - visual-c++

I keep getting this error on my project and i cant figure it out! please help!
error C2143: syntax error : missing ')' before 'constant'
the line is:
while (selection == ('a','b','c', 'd', 'e', 'f', 'g', 'h', 'i','A','B' 'C', 'D', 'E', 'F', 'G', 'H', 'I');
also i know there is an easier way to write that line out but im not sure how i can do it. im a beginner at this so can any of you pros edit this line for me!

How many open parentheses do you have?
How many closed parentheses do you have?
Are these the same number? If not, where is one missing?
Also, the syntax a == (b,c,d) is not shorthand for a == b || a == c || a == d like you seem to think. It's actually equivalent to a == d due to the way the comma operator works.
To be completely explicit, what you actually want is this:
while (selection == 'a' || selection == 'b' ||
selection == 'c' || selection == 'd' ||
selection == 'e' || selection == 'f' ||
selection == 'g' || selection == 'h' ||
selection == 'i' || selection == 'A' ||
selection == 'B' || selection == 'C' ||
selection == 'D' || selection == 'E' ||
selection == 'F' || selection == 'G' ||
selection == 'H' || selection == 'I')
{
/* Do stuff */
}
Or, to be a lot more consice about it, you can take advantage of the fact that the letters are arranged alphabetically in the ASCII table, and write
while (tolower(selection) >= 'a' && tolower(selection) <= 'i')
{
/* Do stuff */
}
This requires inclusion of <ctype.h> for the tolower function.

Given your comments on Tyler's post, it seems like what you really want is:
while ((selection >= 'a' && selection <= 'i') || (selection >= 'A' && selection <= 'I'))
{
// loop
}
Characters can be compared as if they were numbers (because they are numbers in the CPU), which means that you can check for a range of characters using the < > <= >= operators.

Related

bash - Recursively finding files with different extensions using OR operator [duplicate]

I have a variable v in my program, and it may take any value from the set of values
"a", "b", "c", ..., "z"
And my goal is to execute some statement only when v is not "x", "y", or "z".
I have tried,
for C-like languages (where equality operators compare the actual string values; e.g. c#, javascript, php)
if (v != "x" || v != "y" || v != "z")
{
// the statements I want to be executed
// if v is neither "x", nor "y", nor "z"
}
for Pascal-like languages (e.g. plsql)
IF (v != 'x' OR v != 'y' OR v != 'z') THEN
-- the statements I want to be executed
-- if v is neither "x", nor "y", nor "z"
END IF;
The statements inside the if condition always get executed. Am I doing anything wrong?
Use &&/AND/and, not ||/OR/or:
v != "x" && v != "y" && v != "z"
Problem
If an if block is always executed, the condition for the if block always evaluates to true. The logical expression must be wrong.
Let us consider v != "x" || v != "y" || v != "z" for each value of v.
When v = "x",
v != "x" becomes "x" != "x", which is false.
v != "y" becomes "x" != "y", which is true.
v != "z" becomes "x" != "z", which is true.
The expression evaluates to false || true || true, which is true.
When v = "y", the expression becomes
"y" != "x" || "y" != "y" || "y" != "z"
or true || false || true, which is true.
When v = "z", the expression becomes
"z" != "x" || "z" != "y" || "z" != "z"
or true || true || false, which is true.
For any other value for v, the expression evaluates to true || true || true, which is true.
Alternatively, consider the truth-table:
│ A B C │
v │ v != "x" v != "y" v != "z" │ A || B || C
───────┼──────────────────────────────────┼──────────────
"x" │ false true true │ true
"y" │ true false true │ true
"z" │ true true false │ true
other │ true true true │ true
As you can see, your logical expression always evaluates to true.
Solution
What you want to do is, find a logical expression that evaluates to true when
(v is not "x")and(v is not "y")and(v is not "z").
The correct construction is,
for C-like languages (eg. c#, javascript-(may need the strict equality operator !==), php)
if (v != "x" && v != "y" && v != "z")
{
// the statements I want to be executed
// if v is neither "x", nor "y", nor "z"
}
for Pascal-like languages plsql
IF (v != 'x' AND v != 'y' AND v != 'z') THEN
-- the statements I want to be executed
-- if v is neither "x", nor "y", nor "z"
END IF;
De Morgan's law
By De Morgan's law, the expression can also be rewritten as (using C-like syntax)
!(v == "x" || v == "y" || v == "z")
meaning
not((v is "x")or(v is "y")or(v is "z")).
This makes the logic a bit more obvious.
Specific languages
Some languages have specific constructs for testing membership in sets, or you can use array/list operations.
sql: v NOT IN ('x', 'y', 'z')
javascript: ["x", "y", "z"].indexOf(v) == -1
python: v not in {"x", "y", "z"}
java: !Arrays.asList("x", "y", "z").contains(v)
java-9 (and above): !Set.of("x", "y", "z").contains(v)
I figured I'd contribute an answer for Bourne shell script, since the syntax is somewhat peculiar.
In traditional/POSIX sh the string equality test is a feature of the [ command (yes, that is a distinct command name!) which has some pesky requirements on quoting etc.
#### WRONG
if [ "$v" != 'x' ] || [ "$v" != 'y'] || [ "$v" != 'z' ]; then
: some code which should happen when $v is not 'x' or 'y' or 'z'
fi
Modern shells like Ksh, Bash, Zsh etc also have [[ which is somewhat less pesky.
#### STILL WRONG
if [[ $v != 'x' || $v != 'y' || $v != 'z' ]]; then
: some code which should happen when $v is not 'x' or 'y' or 'z'
fi
We should highlight the requirement to have spaces around each token, which is something many beginners overlook (i.e. you can't say if[[$v or $v!='y' without whitespace around the commands and operators), and the apparent optionality of quoting. Failing to quote a value is often not a syntax error, but it will lead to grave undesired semantical troubles if you fail to quote a value which needs to be quoted. (More on this elsewhere.)
The obvious fix here is to use && instead of || but you should also note that [[ typically sports support for regular expressions, so you can say something like
if [[ ! $v =~ ^(x|y|z)$ ]]; then
: yeah
fi
and don't forget the trusty old case statement which is quite natural for this, and portable back into the late 1970s:
case $v in
x | y | z)
;; # don't actually do anything in this switch
*) # anything else, we fall through to this switch
yeah
some more yeah
in fact, lots of yeah;;
esac
The trailing double semicolons cause aneurysms at first, but you quickly recover, and learn to appreciate, even love them. POSIX lets you put an opening parenthesis before the match expression so you don't have unpaired right parentheses, but this usage is rather uncommon.
(This is obviously not a suitable answer for Unix shells which are not from the Bourne family. The C family of shells -- including the still somewhat popular tcsh -- use a syntax which is supposedly "C-like" but that's like being unable to tell apart Alice Cooper from the girl who went to Wonderland; and the Fish shell has its own peculiarities which I'm not even competent to comment on.)
You could use something like this, for PHP:
if(strpos('xyz',$v[0])===false)//example 1
//strpos returns false when the letter isn't in the string
//returns the position (0 based) of the substring
//we must use a strict comparison to see if it isn't in the substring
if(!in_array($v[0],array('x','y','z')))//example 2
//example 3
$out=array('x'=>1,'y'=>1,'z'=>1); //create an array
if(!$out[$v[0]]) //check if it's not 1
if(!preg_match('/^[xyz]$/',$v))//example 4, using regex
if(str_replace(array('x','y','z'),'',$v[0]))//example 5
if(trim($v[0],'xyz'))//example 6
For Javascript:
if(~'xyz'.search(v[0]))//example 1(.indexOf() works too)
if(!(v[0] in {x:0,y:0,z:0}))//example 2
if(~['x','y','z'].indexOf(v[0]))//example 3, incompatible with older browsers.
if(!/^[xyz]$/.match(v))//example 4
if(v.replace(/^[xyz]$/))//example 5
For MySQL:
Select not locate(#v,'xyz'); -- example 1
select #v not in ('x','y','z'); -- example 2
-- repetition of the same pattern for the others
For C:
if(!strstr("xyz",v))//example 1, untested
There are more ways, I'm just too lazy.
Use your imagination and just write the one that you like more!

i'am trying to make a function that deletes vowels in a string

def disemvowel(word):
new_word = []
list_of_letter = list(word)
for letter in list_of_letter:
if letter == 'a' or 'A' or 'E' or 'e' or 'O' or 'o' or 'U' or 'u':
continue
else:
new_word.append(letter)
return ''.join(new_word)
Your condition is always True
You should change it to:
if letter in "aAeEiIoOuU":
When you write
if letter == 'a' or 'A'
You said "if letter is 'a' or if 'A' is not a empty string", and a is not a empty string.

Indentation under wrapped line

I'm beginning in programming and using Pycharm. I adopted 79 lines as maximous line length. But now I don't know if using an extra tab to indent the next line, since the previous line is already indented under the first. This shows what I mean:
I can use this:
if len(word) % 2 == 1:
cent = len(word) // 2
if (word[cent] == 'a' or word[cent] == 'e' or word[cent] == 'i'
or word[cent] == 'o' or word[cent] == 'u'):
print('The word's center is a lowercase vowel')
Or this:
if len(word) % 2 == 1:
cent = len(word) // 2
if (word[cent] == 'a' or word[cent] == 'e' or word[cent] == 'i'
or word[cent] == 'o' or word[cent] == 'u'):
print('The word's center is a lowercase vowel')
Either ways worked.
So, is there a convention for these situation.
Thanks all in advance!
Have a great day :)
Per PEP8 https://www.python.org/dev/peps/pep-0008/#maximum-line-length:
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
As for indentation on subsequent lines, More indentation included to distinguish this from the rest.:
https://www.python.org/dev/peps/pep-0008/#indentation
Code would look like this:
if len(word) % 2 == 1:
cent = len(word) // 2
if (word[cent] == 'a' or word[cent] == 'e' or word[cent] == 'i'
or word[cent] == 'o' or word[cent] == 'u'):
print("The word's center is a lowercase vowel")
You can use \ as last character in a line to signify "this line continues in the next line" - this helps where "normal" python code can not be broken (not your case).
Your example is better suited by
vowels = set("aeiou") # maybe faster then list-lookup if used several times
if word[cent] in vowels:
or by
if word[cent] in "aeiou":
or by
def isVowel(ch):
return ch in "aeiou"
if isVowel(word[cent]):
PEP-8 maximum line lenght tells about how to "format correctly".

Oracle 11G repeats output_put.put_line twice. How to solve this?

My loop is given me twice the results. As I only want it one time. What should I do?
for i in 1..length(newStudentNumber) loop
character := SUBSTR(newStudentNumber, i, 1);
newStudentNumber := newStudentNumber || case character
when 'A' then '16'
when 'B' then '17'
when 'C' then '18'
when 'D' then '19'
when 'E' then '20'
when 'F' then '21'
when 'G' then '22'
when 'H' then '23'
when 'I' then '24'
when 'J' then '25'
when 'K' then '26'
when 'L' then '27'
when 'M' then '28'
when 'N' then '29'
when 'O' then '30'
when 'P' then '31'
when 'Q' then '32'
when 'R' then '33'
when 'S' then '34'
when 'T' then '35'
when 'U' then '36'
when 'V' then '37'
when 'W' then '38'
when 'X' then '39'
when 'Y' then '40'
when 'Z' then '41'
else character
end;
end loop;
dbms_output.put_line(newStudentNumber);
I hope I am not too vague with this question
Thanks in advance
Inside your loop you are appending the possibly-converted character to the same string variable you are looping over, newStudentNumber. If you don't want both the old and new value then you need a second variable which you populate in the loop.
-- new variable, which has to be declared
convertedStudentNumber := null;
for i in 1..length(newStudentNumber) loop
character := SUBSTR(newStudentNumber, i, 1);
convertedStudentNumber := convertedStudentNumber || case character
...
else character
end;
end loop;
dbms_output.put_line(convertedStudentNumber);
Now the two variables have different values, one starting TUE..., the other starting 353620....

How can I check part of a string for if else statements in C?

I am scanning user input as a string to get a month (user inputs March and month = 3), and I am using if else statements to check which month was input.
if (user_input[0] == 'J' && user_input [1] == 'a' && user_input[2] == 'n'
&& user_input[3] == 'u' && user_input[4] == 'a' && user_input[5] == 'r'
&& user_input[6] == 'y')
Is how i'm checking for January. Is there any way to test this without typing all that out?
like
user_input[0-6] == 'January'
thanks
You can use for loop.Create a switch case state.If you didn't understand I can write the code for you but i prefer you to work yourself with your code except some help

Resources