mardi 28 février 2017

How do i use IF in C?

i am new :) I'm trying to make a program that calculates the sum of two integers, and i don't know how to use If. My intent was to print a message if one (or both) number is negative.

An error appears if i try to compile this, it says me: 'else without a previous if'

Here is the code.

#include <stdio.h>
main()
{
    int num1, num2, numf;
    printf("type a number ");
    scanf("%d", &num1);
    if (num1<0);
    printf("you must type only positive numbers");
    else
    printf("type a number ");
    scanf("%d", &num2);
    if (num2<0);
    printf("you must type only positive numbers");
    else
    numf=num1+num2;
    printf("%d + %d = %d", num1, num2, numf);
    system("PAUSE");
}

i know, it's kinda stupid, but i'm doing this to understand how to use properly if. What am i doing wrong? Is this a complete disaster?

Please help me ;(

Aucun commentaire:

Enregistrer un commentaire