-3

I am just a student and beginning to learn C programming, so I am not really on an advanced level.

I have this piece of code, and I guess that it is pretty easy to understand what I am trying to do. However I get an error saying an error before if.

I suspect that the problem is my if-else statement in between the if and else. How would you guys solve it?

#include <stdio.h>
#include <iostream>
int main ()
{
int N;
scanf("%i",&N);
if (N > 50)
        (if (N > 75)
            N = N - 25;
            N = N - 10;
        )
else
        N = N + 10;
printf("%i",N);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
zakybaraq
  • 21
  • 1
  • 1
  • 5
  • 1
    use %d instead of %i and also set parenthesis{} of if. e.g if(){ } else{} – Salman Oct 23 '17 at 18:06
  • 6
    Did you confuse `(` with `{`? – user0042 Oct 23 '17 at 18:06
  • 1) Consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), not by trying to code randomly. 2) When asking about compilation errors - copy/paste the error. 3) Voted to close as typo due to obvious reasons. – Algirdas Preidžius Oct 23 '17 at 18:06
  • `(if (N > 75) N = N - 25; N = N - 10; )` --> `{ if (N > 75){ N = N - 25; N = N - 10; } }` or `{ if (N > 75) N = N - 25; N = N - 10; }` ? – BLUEPIXY Oct 23 '17 at 18:07
  • 3
    Why do you use the C++ tag when learning C? Why do you use `#include ` when learning C? Or are you learning C++? Then why do you use printf instead of cout? –  Oct 23 '17 at 18:08
  • Trying to coalesce @manni66 's point, it looks like you are simultaneously learning C and C++. Be warned these are two different languages. C++ can often compile C code, but not always and there are no guarantees this will result in an optimal solution. C can rarely compile C++ code, and when it can, the C++ code that can compile eschews all that is C++. – user4581301 Oct 23 '17 at 18:20

2 Answers2

1

Taking into account the error message it seems that you are compiling the program as a C++ program. Otherwise the compiler at first would issue a message about the wrong header <iostream>.

If it is a C++ program then there is no need to use functions from the header <stdio.h> that moreover in C++ should be included like

#include <cstdio>

So I advice to remove the header and to use the standard C++ native I/O functions.

Also instead of braces you enclosed in parentheses the substatement of the if statement.

if (N > 50)
        (if (N > 75)
            N = N - 25;
            N = N - 10;
        )

And it is obvious if N is greater than 75 then it is evident it is greater than 50. So the first if statement also may be removed.

And it is a bad idea to use a capital letter for naming an ordinary variable.

Taking all this into account the program can look like

#include <iostream>

int main()
{
    int n;

    std::cin >> n;

    if ( n > 75 )
    {
            n = n - 25; // or n -= 25;
            n = n - 10; // or n -= 10;
    }
    else
    {
        n = n + 10; // or n += 10;
    }

    std::cout << n << std::endl;
}

If you want to use standard C I/O functions then the program can look like

#include <cstdio>

int main()
{
    int n;

    std::scanf( "%i", &n );

    if ( n > 75 )
    {
            n = n - 25; // or n -= 25;
            n = n - 10; // or n -= 10;
    }
    else
    {
        n = n + 10; // or n += 10;
    }

    std::printf( "%i\n", n );
}

If it is a C program and the header <iostream> is included by mistake then the program can look like

#include <stdio.h>

int main( void )
{
    int n;

    scanf( "%i", &n );

    if ( n > 75 )
    {
            n = n - 25; // or n -= 25;
            n = n - 10; // or n -= 10;
    }
    else
    {
        n = n + 10; // or n += 10;
    }

    printf( "%i\n", n );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Just change the ( with { after if condition. I have edited your code -

#include <stdio.h>
#include <iostream>
int main ()
{
int N;
scanf("%d",&N);
if (N > 50)
        {
            if (N > 75)
            N = N - 25;
            N = N - 10;
        }
else
        N = N + 10;
printf("%d",N);
}
Naseef Chowdhury
  • 2,357
  • 3
  • 28
  • 52
  • You shouldn't use `%i` for `scanf`. See [here](https://stackoverflow.com/questions/17329647/i-or-d-to-print-integer-in-c-using-printf). Instead, use `%d`. – Nik Oct 23 '17 at 18:19