My program compiles but hangs on the first printf

Asked

Viewed 91 times

0

#include<stdio.h>
void main()
{
    int n1,n2,n3,maior,menor;
    printf("Primeiro numero: ");
    scanf("%d", n1);
    printf("Segundo numero: ");
    scanf("%d", n2);
    printf("Terceiro numero: ");
    scanf("%d", n3);
    if (n1>n2&&n1>n3)
    {
        maior=n1;
        if (n2>n3)
            menor=n3;
        else
            menor=n2;
    }
    else if (n2>n1&&n2>n3)
    {
        maior=n2;
        if (n1>n3)
            menor=n3;
        else
            menor=n1;
    }
    else
    {
        maior=n3;
        if (n1>n2)
            menor=n2;
        else
            menor=n1;
    }
    printf("Menor numero: %d\nMaior numero: %d",menor,maior);
}

After asking the first number and I give the answer, the program crashes. Does anyone know why that is? I’m using Codeblocks because it’s recommended by the university.

  • Hi. What input are you putting into the system?

  • 3

    @alwaysNever You forgot the commercial E operator (&) before the variable names in each scanf. Recommended reading -> https://answall.com/questions/125793/qual-significado-operador-e-comercial-na-linguagem-c

  • MY GOD was that, obg. If I knew how to give a little bit I would give. I am compiling in C for another comment

3 answers

3

Missed to put & before variable in scanf. So it is much better to use library and use Cin instead of scanf. You used "Return 0" at the end?

Your code would be (excerpt):

include

using namespace Std; void main() { int n1,N2,N3,major,minor;

cout <<"Primeiro numero: ";
cin >> n1;
cout <<"Segundo numero: ";
cin >> n;
cout <<"Terceiro numero: ";
cin >> n3;
if (n1>n2&&n1>n3)
{

2

Hello!

You should use & in variables when using scanf. Example: scanf("%d", &n1); Because the value you enter will be stored in the address of n1, when you are in the topic of pointers you will understand better. If you still have questions, visit the site http://linguagemc.com.br

1

First, Code::Blocks is a beautiful ******! No joke.

Second, include the <stdlib.h>

Third, give a value of 0 to all variables (always assign a value to each variable you declare!).

I hope it solves your problem! D

  • Codeblocks is the only IDE q has installed on the university PC unfortunately :V I includes <stdlib. h> and put value 0 in td, still hangs :( but thanks for the help

Browser other questions tagged

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