Click here to Skip to main content
15,888,322 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello i have a problem with if statements..
What i was asked is to take the 0+4*n and 3+4n elements of the aray but only for the n-5 values of the array.. For the remaining 5 values to take the first and the last one ..
Then in these values only, check if the value in the array is 0 and if it is zero make it 1.
My code is below..It is a little bit altered since in my code the total number is provided through a txt file that i skipped here..
The problem is in the end...
else statement is activated with other else if statements ...
Any suggestions?

What I have tried:

C#
#include <stdio.h>
int main(void)
{
int array[63];
int i,n,total;
total=50;

char selection;

for (i=0; i<total; i++)
array[i] = 0;

do{
printf("0.Exit \n");
printf("1.List array \n");
printf("2.Change value of array: \n");
scanf(" %c",&selection);

if (selection=='1')
{   
    for (i=0; i<total; i++)
    printf("%d \n", array[i]);
}

else if (selection=='2')
    {
        for (i=0; i<(total); i++)
        {
            for (n=0; n<((total)/4); n++)
            {   if (i==(0+(n*4)) && i<(total-5)&&(array[i]==0))
                {
                    array[i]=1;
                    printf("You changed array value in place %d, ",i);
                    i=n=total+1;
                    break;
                }


                else if ((i==(3+(n*4)))&&i<(total-5)&&(array[i]==0))
                {
                    array[i]=1;
                    printf("You changed array value in place %d, ",i);
                    i=n=total+1;
                    break;
                }
                else if (i==(total-5)&&(array[i]==0))
                {
                    array[i]=1;
                    fprintf("You changed array value in place %d, ",i);
                    i=n=total+1;
                    break;
                }
                else if (i==(total-1)&&(array[i]==0))
                {
                    array[i]=1;
                    printf("You changed array value in place %d, ",i);
                    i=n=total+1;
                    break;
                }
                else
                    printf("Nothing to change");

            }
        }
    }
  } while (selection!='0');
}
Posted
Updated 13-Nov-16 22:21pm
v8
Comments
[no name] 13-Nov-16 13:02pm    
"Any suggestions", yes. Learn to use the debugger to debug your code.
baskon 13-Nov-16 13:28pm    
I am in my very first steps of C programming! So thanks toy everyone for helping!
I will also try to learn using the debugger!
Jochen Arndt 13-Nov-16 13:24pm    
"else statement is activated with other else if statements"
How did you know that?

It will be definitely not so. Instead of printing "Something", print the values of i, n, and array[i] to see when the preceeding conditions does not match.

BTW:
- The condition i==(total) will never match
- Instead of using i=n=500 to terminate the loops use i=n=total+1
Richard MacCutchan 14-Nov-16 4:00am    
If selection == '2' your loop will not work because you have not set a value for total. You also seem to be using random numbers (63, 53,50) to access the elements of your array. Use a #define statement to define the size of the array and use that number in all your code.
baskon 14-Nov-16 4:22am    
Thank you so much for the answer!
Yes i made a small mistake because this is a part of a bigger program that reads also info from a txt file..
So when converting it to something that runs without the txt file i made the mistake..i think i fixed it now..but still the problem with the loop exists..

If I understand your question correctly, you're saying that "else" print statement is getting printed when you expect an "else if" to be getting executed. That's impossible, but you do have that inside a nested for loop, so perhaps it's getting to the "else" on the following iteration when it's not expected.

I'm not sure what you're trying to do with that code (it's hard to read, I'd suggest refactoring), but do you mean to have the "break" statements in there? Those will break you out of the for loop.
 
Share this answer
 
Comments
baskon 13-Nov-16 13:23pm    
What i was asked is to take the 0+4*n and 3+4n elements of the aray but only for the n-5 values of the array.. For the remaining 5 values to take the first and the last one ..
Then in these values only, check if the value in the array is 0 and if it is zero make it 1.

That is why i have this big for loop.. and so many if statements..

The problem correctly is in the last else statement where i want to write a message "no zero values find"...
But it gets executed in other statements multiple times...

If you have any better solution for the formatting of course i am all ears! Thank you very much!
Patrice T 13-Nov-16 13:38pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
And complete your code so we can compile and run it.
add the exact statement as well.
Albert Holguin 13-Nov-16 21:38pm    
well, first off... I asked if you were breaking on purpose there... are you? do you understand what that does?
Your code is not complete, we can't devise what it is supposed to do. Please complete it, so that we can understand what you try to do.
The only possible advice:
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

[UpDate]
With the debugger, check that
C++
i=n=total+1;

do what you think it should.
 
Share this answer
 
v2
Comments
baskon 14-Nov-16 1:39am    
Ok i updated my code above...
Will try to check and try to learn the debugger today also!
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900