Why doesn’t my code print?

Asked

Viewed 197 times

1

I wonder why this code I’m creating doesn’t print anything when I compile it:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void)

{
    int dadoacerto, vidadrizzt, vidaartemis, dadod, dadoa, CAd = 8, CAa = 5, dadoataqdr, dadoataart;

    scanf("%d\n%d\n%d", &dadoacerto, &vidadrizzt, &vidaartemis);

    srand( (unsigned)time(NULL) );

    dadod = rand() % dadoacerto;
    dadoa = rand() % dadoacerto;

    if(dadod>dadoa)
    {
        dadoataqdr = rand() % 8;//Drizzt ataca primeiro

        if( dadoataqdr>CAa)
        {
            vidaartemis = vidaartemis - dadoataqdr;
            printf("Drizzt %d", dadoataqdr);        
        }
    }   
    else if(dadoa>dadod)
    {
        dadoataart = rand() % 5 + rand() % 5;

        if(dadoataart>CAd)
        {
            vidadrizzt = vidadrizzt - dadoataart;
            printf("Artemis %d", dadoataart);
        }
    }
    else if (dadoa==dadod)
    {
        dadoataqdr = rand() % 8;//Drizzt ataca primeiro

    if( dadoataqdr>CAa)
        {
            vidaartemis = vidaartemis - dadoataqdr;
            printf("Drizzt %d", dadoataqdr);        
        }
    }

return 0;
}
  • 2

    When you compile or when you run? You’re probably not entering any if. Put a else to test

  • The first thing the program does is scanf(). Make sure you enter 3 values so the program doesn’t get stuck on this statement.

  • When I click on "compile & run" in C++ DEV, do not print any value, only the option for input of scanf values

  • Yes, scanf() is correct

1 answer

1


Your program has some logic errors. I ran it here and found, I think, the main problem why your program isn’t printing out a output. The following occurs when your program enters an if, for example the first, it will still have to test to enter the internal if the condition is false the code with the printf will not run and the program will not be able to execute nor another command within an Else, so it will not print anything at the end. As I did not understand very well which purpose of this program becomes a little difficult to tidy up, but you just need to take a little but beware of the if-esle logic.

A possible suggestion would be for you to combine the two conditions into one if only using the operator &&.

  • But if the first condition is not satisfied, shouldn’t the program test the later Else if?? that is, the first if, the second and third if Else??

  • Yes, but what is happening is that your program is entering, for example, in the first if, but it is not entering the if inside this. For you can better see the situation include a printf command within the first if ai you will understand what is happening. And also with it has already entered into one if it will not enter into neither another esle. And if it does not enter the first if and enters an Else-if then it may end up occurring the same thing as the case of the first.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.