Can't find my mistake! error: expected identifier before '(' token - c++

This is my main code, I did search related mistakes before asking but it just doesn't seem wrong...The IDE says the error is in line 11.
#include<stdio.h>
int main()
{
float sal;
printf("Digite o salário bruto: ");
scanf("%f",&sal);
if(sal<=2246.75){
printf("Salário líquido : ",sal);
}
else{
if(sal>2246.75)&&(sal<2995.70){
printf("Salário Líquido: ",sal * 0.925);
}
else{
if(sal>2995.70)&&(sal<=3743.19){
printf("Salário Líquido: ",sal * 0.845);
}
else{
printf("Salário Líquido: ", sal * 0.775);
return 0;
}
}
}
}

if(sal>2246.75)&&(sal<2995.70){
The problem is that the entire condition must be placed within a set of parentheses.
It's fine if you want to further enclose the sub-conditions, but you must surround the entire lot, too:
if ((sal > 2246.75) && (sal < 2995.70)) {

Your if statement has to correct as follows, here you were missing bracket() for if.
if( (sal>2246.75)&& (sal<2995.70)){
You have to specify the formatter for printf correctly as follows; here you are missing type formatter.
printf("Salário Líquido: %f", sal * 0.775);
Both these errors are there in multiple occasions in your code.

there are actually two major kinds of problems with the posted code.
printf("Salário líquido : ",sal);
is missing a format specifier for the 'sal' variable
it should be:
printf("Salario liquido : %f", sal);
Note: each of the printf() statements have this same problem
if(sal>2246.75)&&(sal<2995.70){
is missing the outside parens
it should be:
if( (sal>2246.75) && (sal<2995.70) ) {
Note: I added some horizontal spacing for clarity only
the last two 'if' statements have this same problem
Suggest compiling with all warnings enabled.
For gcc, at a minimum, use '-Wall -Wextra -pedantic'
main always returns an 'int'
To avoid that return code being a random value, always end the function with:
return(0);

I think that if(sal>2246.75)&&(sal<2995.70) is supposed to be
if(sal>2246.75 && sal<2995.70).

Related

How to adjust the indentation of code in text file using c++?

I am copying code of file 1 to file 2 , but i want the code in file 2 to look adjusted with indentation like this: at the beginning indentation=0, every curly bracket opened increases the depth of indentation, every curly bracket closed reduces the indentation 4 spaces for example. I need help in fixing this to work
char preCh;
int depth=0;
int tab = 3;
int d = 0;
int pos = 0;
file1.get(ch);
while(!file1.eof())
{
if(ch=='{')
{
d++;
}
if(ch=='}'){
d--;
}
depth = tab * d;
if(preCh == '{' && ch=='\n'){
file2.put(ch);
for (int i = 0; i <= depth; i++)
{
file2.put(' ');
}
}
else
file2.put(ch);
preCh = ch;
ch = file1.get();
}
}
result must be indented like in code editors:
int main(){
if(a>0)
{
something();
}
}
Maybe, unexpectedly for you, there is no easy answer to your question.
And because of that, your code will never work.
First and most important, you need to understand and define indentation styles. Please see here in Wikipedia. Even in your given mini example, you are mixing Allman and K&R. So, first you must be clear, what to use.
Then, you must be aware that brackets may appear in quotes, double quotes, C-Comments, C++ comments and even worse, multi line comments (and #if or #idefs). This will make life really hard.
And, for the closing brackets, and for example Allman style, you will know the needed indentation only after you printed already the "indentation spaces". So you need to work line oriented or use a line buffers, before you print a complete line.
Example:
}
In this one simple line, you will read the '}' character, after you have already printed the spaces. This will always lead to wrong (too far right) indentation.
The logic for only this case would be complicated. Ant then assume statements like
if (x ==5) { y = 3; } } } }
So, unfortunately I cannot give you an easy solution.
A parser would be needed, or I simply recommend any kinde of beautifier or pretty printer

c++ for loop: 'i' was not declared in this scope

bool linear_search(const string A[], int n, string colour, int &count)
{
for (int i = 0; i < n; i++);
{
if (colour == A[i])
{
return true;
}
}
return false;
}
Compiling the above code results in the error 'i' was not declared in this scope for the if statement if (colour == A[i]).
This is really similar to many other for loops I have written, and I don't understand why it is not declared in the scope. Wasn't it declared in the previous line? How do I fix this?
You have a semi colon after your for loop declaration, remove it and you will be fine.
As others have pointed out, the problem is an extra semicolon that prevents your intended loop body from actually being part of the loop. But I want to provide more information on how to catch and avoid this kind of error.
First of all, when I compile the code with the formatting you show, my compiler produces a warning:
main.cpp:130:32: warning: for loop has empty body [-Wempty-body]
for (int i = 0; i < n; i++);
^
You should check to see if you're already getting this or some similar warning, and if so you should make sure to pay attention to warnings in the future. If you're not getting this warning, see if you can increase your compiler's warning level in some way to cause it to produce a warning like this. Enabling and paying attention to compiler warnings can save you a lot of trouble.
Next, I notice that your code is poorly formatted. Poor formatting can hide this sort of error. When I auto-format the code it becomes:
bool linear_search(const string A[], int n, string colour, int &count) {
for (int i = 0; i < n; i++)
;
{
if (colour == A[i]) {
return true;
}
}
return false;
}
This formatting makes the extraneous semicolon much more obvious. (It also suppresses my compiler's warning about the empty body, since the compiler assumes that if you put the empty body on a separate line then you really mean for it to be empty.) Using automatic formatting avoids the problems of inconsistent formatting and ensures that the formatting is consistent with the actual meaning of the code. See if your editor provides formatting support or see if you can integrate an external formatter like clang-format.
Are you sure you need ; in this line? for (int i = 0; i < n; i++);
you ended for loops block by adding a ; after for loop
for (int i = 0; i < n; i++);
remove this semicolon.
I had a similar problem but there was an "if" statement before my variable declaration in front of a "for" loop, the error was the same. Just in case that somebody googled it and didn't mention something like that:
if (someVar!=1) // an "if" statement that reduces the scope of "var"
//some comment line
//some other comment line
double var = 0.0;
for (size_t i = 0; i < length; i++) {
var *= 0.5; // Error appeared here
}

if and else without braces

I expect the following code to compile. Clang and VC++ both give me an error on the line with else.
void MyFunction(std::int32_t& error)
{
std::int32_t variable = 0;
if(GetSomething())
error = EOK;
else
error = ERROR;
}
If I put curly braces around error = EOK;, then it compiles. Why does VC++ say:
illegal else without matching if
?
My full code is below, replacing std::uint32_t with a typedef. It still gives the same error in VC++.
using sint32 = int;
#define ERROR 5;
#define EOK 0;
bool GetSomething();
void MyFunction(sint32& error)
{
sint32 variable = 0;
if (GetSomething())
error = EOK;
else
error = ERROR;
}
If your definition of EOK is as follows:
#define EOK 0;
then it would cause this type of error, because it forcibly terminates the if-statement before the else is reached, making it an else without a matching if. The compiler sees this code after macro replacement:
if(GetSomething())
error = 0;;
else
Here is a possible fix:
enum
{
EOK = 0,
ERROR = 5
};
Note that all identifiers starting with E followed by either another uppercase letter or a number are reserved for use as macro names by <cerrno>, so to avoid name clashes consider using a different naming convention for your errors.
To be implest and more efficient, you can do :
error = (GetSomething()) ? 0 : 5 ;
And if you want to with enum as Matt say , it become :
error = (GetSomething()) ? enum.EOK : enum.ERROR ;

Error C3493: residual' cannot be implicitly captured because no default capture mode has been specified

I'm writing a function to solve a stream value problem as part of my Numerical methods studies. This is the 'meat' of the program yet it comes up with a number of bizarre errors, which is strange because I've used the same piece of code in other programs without any errors.
void solve_stream(void)
{
double residual, residual_total;
int resid_count, count=0;
do
{
residual_total=0.0;
resid_count=0;
for(int i=0;i<maxi;i++)
for(int j=0;j<maxj;j++)
if((i+j)%2==count%2)
{
residual= P[i][j]*e[i][j]-f[i][j];
if(i<maxi-1) residual+= P*[i+1][j]*a[i][j];
if(i>0) residual+= P[i-1][j]*b[i][j];
if(j<maxj-1) residual+= P[i][j+1]*c[i][j];
if(j>0) residual+= P[i][j-1]*d[i][j];
residual_total+= fabs(residual);
resid_count++;
}
residual_total = residual_total/resid_count; //average residual
count++;
if(count%100==0) cout<<"-";
}
while(residual_total>1.0);
}
On line 155, the one beginning if(i'less than' maxi-1), the compiler returns 'error C2143: syntax error : missing ']' before '+' ' even though residual is defined as a double!
For all the variables defined it returns 'error C3493: 'residual' cannot be implicitly captured because no default capture mode has been specified'; even for i and j!
P*[i+1][j]
What is P? It seems to be an array but here you're multiplying it.
This looks suspect:
P*[i+1][j]
You probably mean
P[i+1][j]

Parse error in code: expected ' ; ' before ' { ' token -- what is causing this?

The error I'm getting is error: expected ' ; ' before ' { ' token
I tried fixing the code by adding ; after if (thisisanumber==5) as well as after else (thisisanumber!=5). While this solves the first error it creates another error that says error: ' else ' without a previous ' if '. I'd really love to know what error I've made in writing the code, thanks.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int thisisanumber;
cout<<"Whats the Password?: ";
cin>> thisisanumber;
cin.ignore();
if (thisisanumber==5) {
cout<<"You've discovered the password! Wow, you're a genious you should be proud./n";
}
else (thisisanumber!=5) {
cout<<"You've failed in knowing the password and therefore cannot enter, leave and do not come back. Goodbye!/n";
}
cin.get();
}
You're missing a keyword if:
else if (thisisanumber!=5) {
^^
Alternately, since the opposite condition to thisisanumber == 5 is that thisisanumber is not 5, you don't need the condition:
else {
You don't need another condition as there are only two cases - just use else { ... } and it will catch all cases in which thisisanumber==5 is false.
The structure of an if statement is:
if (condition) { ... }
else if (another condition) { ... }
// ... more conditions
else { ... all cases in which no previous condition matched end up here ... }
... but the else if and else parts are always optional.
What happens is the compiler looks at the following:
else (thisisanumber!=5) {
and thinks to itself:
"OK, here's else. Is the next token if? No. Ok, so this is an else clause, and the next thing is what to do in the else-case. Is the next token {? No. Ok, so in the else-case, we execute a single statement, instead of a block. Is the next token (? Yes. Ok, so our statement is wrapped in parentheses... [insert here: the rest of the thought process for interpreting an expression that's wrapped in parentheses] Ok, there's the matching ). Whew. Now let's just match up the ; for this statement... wait, what's this? A {! That's not right."
The compiler is reading the code one token at a time, left to right. It does not report an error at the point where, in a logical sense that humans understand, the error actually is. It reports an error at the point where, by reading the code one token at a time, left to right, it is first able to detect that something is wrong.
It would be legal to write else (thisisanumber!=5);. That would mean "if the number is not equal to 5 (because the if test failed), then check if the number is not equal to 5, and do nothing with the result of that comparison". Meaningless, but legal. It would also be legal to write else if (thisisanumber!=5) {...}, which is presumably what you meant. That would mean "if the number is not equal to 5 (because the if test failed), and the number is not equal to 5, then do this stuff inside the {}". But this is redundant: given that something is not equal to 5, it is guaranteed to be not equal to 5, so there is no point in specifying the test twice. So we should just write else {...}.
"else" is really a shorter word for "otherwise", and has that purpose in C++ as well.