9 Replies - 367 Views - Last Post: 28 September 2011 - 06:07 AM Rate Topic: -----

#1 hellsoffun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-September 11

if statement error?

Posted 28 September 2011 - 03:34 AM

I recently posted (ie a few mins ago) on a thread that i hadnt realized was extremely old and as im not sure if yall watch old threads im posting again sorry if this an inconvenience

#include <stdio.h>

int main (void)
{
    int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, num16, row1, row2, row3, row4, col1, col2, col3, col4, diag1, diag2;

    #define sum 34;

    printf("Enter the numbers 1 to 16 in any order.\n");

    scanf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", &num1, &num2, &num3, &num4, &num5, &num6, &num7, &num8, &num9, &num10, &num11, &num12, &num13, &num14, &num15, &num16);

    printf("%d %d %d %d\n%d %d %d %d\n%d %d %d %d\n%d %d %d %d\n%d %d %d %d", num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, num16);

    row1= num1+num2+num3+num4;
    row2= num5+num6+num7+num8;
    row3= num9+num10+num11+num12;
    row4= num13+num14+num15+num16;

    col1= num1+num5+num9+num13;
    col2= num2+num6+num10+num14;
    col3= num3+num7+num11+num15;
    col4= num4+num8+num13+num16;

    diag1= num1+num6+num11+num16;
    diag2= num4+num7+num10+num13;

    printf("Row sums: %d, %d, %d, %d \n", row1, row2, row3, row4);
    printf("Column sums: %d, %d, %d, %d \n", col1, col2, col3, col4);
    printf("Diagonal sums: %d, %d \n", diag1, diag2);

    

    if(row1=sum && row2=sum && row3=sum && row4=sum && col1=sum && col2=sum && col3=sum && col4=sum && diag1=sum && diag2=sum)
        printf("Congratulations, these numbers form a magic square!");
    else printf("Im sorry this is not a magic square.");
    

    
        

    getch (0);
    return (0);
}


im having 2 issues first for some reason when the program runs it places the numbers 0 0 2130567168 -858993460 under where it re states the values given it and in front of the row sums line. i dont under stand why it would do that at all.

also in the if statement, i have tried various means to do it none working, it says i need an lvalue for sum when i set sum= to 34 if i define sum it says im missing a ')'. i later came to the conclusion that instead of setting sum to a number (34) i should set sum to nothing (i used a different variable as sum remained defined to 34 for some reason) so that it could check for multiple answers however i think i did this wrong (my first clue being it still not running lol). to do this i just set tot as an int and left it with no value, but again i got the lvalue error.

Is This A Good Question/Topic? 0
  • +

Replies To: if statement error?

#2 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 164
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: if statement error?

Posted 28 September 2011 - 04:14 AM

if (variable == some value)

You can't use =, you need to use ==.

= is an assignment operator, == is a comparison operator.

This post has been edited by stackoverflow: 28 September 2011 - 04:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 hellsoffun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-September 11

Re: if statement error?

Posted 28 September 2011 - 04:34 AM

well that was kinda stupid of me totally forgot that (derp). however now im getting a run time issue stating that tot is being used without being initialized, and im still having an issue with the random numbers appearing below the restated numbers and before row sums. (it wont let me past a running of the program on here) and it would seem that the if statement still isnt working which im assuming is because tot isnt being initialzed. so how do i set it to check that all answers are the same without setting it to a number (i understand tot not having a value is the reason it isnt being initialized)?

im sorry im just starting college and having alot of issues with this

#include <stdio.h>

int main (void)
{
    int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, num16, row1, row2, row3, row4, col1, col2, col3, col4, diag1, diag2;

    int tot;

    printf("Enter the numbers 1 to 16 in any order.\n");

    scanf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", &num1, &num2, &num3, &num4, &num5, &num6, &num7, &num8, &num9, &num10, &num11, &num12, &num13, &num14, &num15, &num16);

    printf("%d %d %d %d\n%d %d %d %d\n%d %d %d %d\n%d %d %d %d\n%d %d %d %d", num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, num16);

    row1= num1+num2+num3+num4;
    row2= num5+num6+num7+num8;
    row3= num9+num10+num11+num12;
    row4= num13+num14+num15+num16;

    col1= num1+num5+num9+num13;
    col2= num2+num6+num10+num14;
    col3= num3+num7+num11+num15;
    col4= num4+num8+num12+num16;

    diag1= num1+num6+num11+num16;
    diag2= num4+num7+num10+num13;

    printf("Row sums: %d, %d, %d, %d \n", row1, row2, row3, row4);
    printf("Column sums: %d, %d, %d, %d \n", col1, col2, col3, col4);
    printf("Diagonal sums: %d, %d \n", diag1, diag2);

    

    if(row1==tot && row2==tot && row3==tot && row4==tot && col1==tot && col2==tot && col3==tot && col4==tot && diag1==tot && diag2==tot)
        printf("Congratulations, these numbers form a magic square!");
    else printf("Im sorry this is not a magic square.");
    

    
        

    getch (0);
    return (0);
}

This post has been edited by hellsoffun: 28 September 2011 - 04:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: if statement error?

Posted 28 September 2011 - 04:42 AM

You are using an uninitialized variable:
int tot;
.....
if(row1==tot ...)


Was This Post Helpful? 0
  • +
  • -

#5 hellsoffun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-September 11

Re: if statement error?

Posted 28 September 2011 - 04:46 AM

yes i understand im using an uninitialized variable i stated that above. how do i do what im trying to do without giving tot a value, so that it can check for all possible answers rather then just one? is that even possible or do i then have to find all possible answers and set multiple if statements up?
Was This Post Helpful? 0
  • +
  • -

#6 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: if statement error?

Posted 28 September 2011 - 05:14 AM

Why don't you assign row1 value to tot and then compare the other rows(row2 to row4) and columns (col1 to col4) with it:
tot = row1;

if(row2==tot && row3==tot && row4==tot && col1==tot && col2==tot && col3==tot && col4==tot && diag1==tot && diag2==tot)
       printf("Congratulations, these numbers form a magic square!");
else
      printf("Im sorry this is not a magic square.");



You can't use tot without giving a value in it since it is illegal to use uninitialized variable moreover an uninitialized variable have some garbage within it so using it for comparasion is illogical.

This post has been edited by aaa111: 28 September 2011 - 05:20 AM

Was This Post Helpful? 1
  • +
  • -

#7 hellsoffun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-September 11

Re: if statement error?

Posted 28 September 2011 - 05:16 AM

well that's brilliant tyvm, now could you help me with the random numbers appearing earlier on in the program which im sure arnt all that random but i dont see where in the print statement that would cause it.


sorry ima nub ^^
Was This Post Helpful? 0
  • +
  • -

#8 hellsoffun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-September 11

Re: if statement error?

Posted 28 September 2011 - 05:51 AM

the more i look at this i just dont under stand where these numbers are coming from.

this is how the program looks as it runs:

This program is designed to take any order of numbers 1-16 and formulate if it is
or is not a magic square, Please space in between each number.
Enter the numbers 1 to 16 in any order"
16 3 2 13 5 10 11 8 9 6 712 4 15 14 1
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
0 0 2130567168 -858993460Row sums: 34, 34, 34, 34
Column sums: 34, 34, 34, 34
Diagonal sums: 34, 34
Congratulations, these numbers form a magic square!

i dont under stand where 0 0 2130567168 -85899346 are comeing from they shouldnt be there.

#include <stdio.h>

int main (void)
{
    int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, num16, row1, row2, row3, row4, col1, col2, col3, col4, diag1, diag2, tot;

    printf("This program is designed to take any order of numbers 1-16 and formulate if it is or is not a magic square, Please space inbetween each number.\n");
    printf("Enter the numbers 1 to 16 in any order:\n");

    scanf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", &num1, &num2, &num3, &num4, &num5, &num6, &num7, &num8, &num9, &num10, &num11, &num12, &num13, &num14, &num15, &num16);

    printf("%d %d %d %d\n%d %d %d %d\n%d %d %d %d\n%d %d %d %d\n%d %d %d %d", num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, num16);

    row1= num1+num2+num3+num4;
    row2= num5+num6+num7+num8;
    row3= num9+num10+num11+num12;
    row4= num13+num14+num15+num16;

    col1= num1+num5+num9+num13;
    col2= num2+num6+num10+num14;
    col3= num3+num7+num11+num15;
    col4= num4+num8+num12+num16;

    diag1= num1+num6+num11+num16;
    diag2= num4+num7+num10+num13;

    tot=row1;

    printf("Row sums: %d, %d, %d, %d \n", row1, row2, row3, row4);
    printf("Column sums: %d, %d, %d, %d \n", col1, col2, col3, col4);
    printf("Diagonal sums: %d, %d \n", diag1, diag2);


    if(row1==tot && row2==tot && row3==tot && row4==tot && col1==tot && col2==tot && col3==tot && col4==tot && diag1==tot && diag2==tot)
        printf("Congratulations, these numbers form a magic square!");
    else printf("Im sorry this is not a magic square.");
    

    
        

    getch (0);
    return (0);
}

Was This Post Helpful? 0
  • +
  • -

#9 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: if statement error?

Posted 28 September 2011 - 06:01 AM

printf("%d %d %d %d\n%d %d %d %d\n%d %d %d %d\n%d %d %d %d\n%d %d %d %d", num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, num16);
Here you output 20 numbers, but only give it 16 variables.
Was This Post Helpful? 1
  • +
  • -

#10 hellsoffun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-September 11

Re: if statement error?

Posted 28 September 2011 - 06:07 AM

man THANK YOU tbh i probably never would have caught that iv been trying to do this sense 12am its now 8am and i have class at 1 i feel stupid now... this site is now probably my fav than kyou both for all your help i know ima nub but gotta start somewhere right? ;P again thank you both so much its due today and i never would have gotten it done probably. <3
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1