# Thread: What is wrong with this???

1. ## What is wrong with this???

So here's my program, I'm fairly sure the problem is in the beginning, as I've tried removing entirely the first for loop, and it said that i was 205837 or something, it changed every time.
As it is right now, when I run the program, it asks for the first questions (How many divisions do you want to make?) and then closes instantly whatever I enter.
Now what I don't understand is, EVEN IF my for loop wouldn't work at all, why wouldn't the scanf end at the end prevent it from just closing?

I am COMPLETELY new to C programming btw, I just started studying Computer Science in University. Also I'm from Quebec, hence the french in the program.

The point is basically to manually divide a number of numbers simply by using substractions and ...well shifts? I don't really know how to call them in English, but moving moving bits in a variable.

Code:
```#include "stdafx.h"
#include <stdio.h>

int main ()
{
//division

unsigned short a, b, diff, reste, end;
int valida, validb, i, u, nb;

printf("Combien de divisions voulez-vous effectuer?");
scanf("&d", &nb);

nb = nb + 1; //Pour que la boucle for fonctionne et que le i affiche le bon numéro de dividende/diviseur.

for ( i = 1 ; i < nb ; i++ )
{
do
{
printf("Entrez le dividende %d (valeur entre 0 et 255) : ", &i);
scanf("%d", &valida);
}
while (valida < 0 || valida > 255);

a = valida;

do
{
printf("Entrez le diviseur %d (valeur entre 0 et 255) : ", &i);
scanf("%d", &validb);
}
while (validb < 0 || validb > 255);

b = validb;

b = b << 4;

for ( u = 1 ; u <= 3 ; u++ )
{
a = a << 1;
if (a >= b)
{
a = a - b;
a = a + 1;
}
}
}

printf ("Résultat: %d", &a);
scanf ("%hd", &end);   //Inutile, seulement pour ne pas que le programme se ferme

return 0;
}```

2. Change this
Code:
`scanf("%d", &valida);`
to this
Code:
`scanf(" %d", &valida);`
(adding a space before d ) in 2nd,3rd and 4th scanf.
and report behavior of code.

3. Originally Posted by std10093
Code:
`scanf(" %d", &valida);`
(adding a space before d )
This is redundant: the "%d" conversion specification ignores leading whitespace on its own.

4. Enable your compiler warnings, and fix those warnings first.

For example, GCC (using gcc -W -Wall -std=c99 -pedantic -O3 carl_fillion.c -o carl_fillion) reports:
Code:
```carl_fillion.c: In function ‘main’:
carl_fillion.c:16:5: warning: too many arguments for format [-Wformat-extra-args]
carl_fillion.c:26:17: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
carl_fillion.c:37:17: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
carl_fillion.c:61:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘short unsigned int *’ [-Wformat]
carl_fillion.c:62:5: warning: format ‘%hd’ expects argument of type ‘short int *’, but argument 2 has type ‘short unsigned int *’ [-Wformat]
carl_fillion.c:11:32: warning: unused variable ‘reste’ [-Wunused-variable]
carl_fillion.c:11:26: warning: unused variable ‘diff’ [-Wunused-variable]
carl_fillion.c:16:10: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
carl_fillion.c:27:22: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
carl_fillion.c:38:22: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
carl_fillion.c:62:11: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]```

5. ## Ahhh

Oh! Well, I don't know if the spaces did anything, but I just noticed in my first scanf the & should be a %.
Totally missed that the last 800 times I read it.

It RUNS now at least, though the i value is still around 410328 for some reason, even when I enter 1 to the first scanf.

Thanks though, I guess just looking at the program while looking for something else helped me to find the problem =P

6. How do I enable these warnings?

7. Originally Posted by qny
This is redundant: the "%d" conversion specification ignores leading whitespace on its own.
Wow, just check it and you are right.Thanks for the information

@Carl
Code:
```while (valida < 0 || valida > 255);
a = valida;```
This might be not what you intended to.The semicolon after the while(..); means that this while has no body.Thus the a=valida; will be executed only once no matter what the condition of while is .
Maybe it should be
Code:
```while (valida < 0 || valida > 255)
{
a = valida;
}```

8. Originally Posted by Carl Fillion
How do I enable these warnings?
gcc -Wall file.c -o executableName

9. No, that is a do while loop I'm trying to make a= valida should only occur once, but what is between the do and the while should be looped.
Is this not how you write those? I'm actually more familiar with java so I tend to mix the languages up a bit =S

10. Also I'm terribly sorry but I have no idea what gcc is?
I'm using visual studio, I've tried looking around to find an option to enable more warning but I can't find anything.
I DO have some warnings already, but a lot less than what Nominal posted

11. Visual studio doesn't have the kind of printf/scanf diagnostics that GCC has.

FWIW, there is no reason why you can't have multiple compilers installed on the same machine.
Getting the same code to compile on two different compilers, and get the same run-time results is a really good way of making sure you're not doing anything suspect (be it language features, uninitialised memory, bad pointers and so on).

12. Oh geez you don't need & before arguments in printf...
Alright I got it sorted out now. Program still isn't working properly but I do understand what is wrong at least.
Thanks for the replies!

P.S, I don't mean to offend anyone, but I hate C programming.
I've done some java, some C++ and some C. And I just keep failing at C for some technical stuff like this, so annoying.