Like Tree1Likes
  • 1 Post By Nominal Animal

What is wrong with this???

This is a discussion on What is wrong with this??? within the C Programming forums, part of the General Programming Boards category; So here's my program, I'm fairly sure the problem is in the beginning, as I've tried removing entirely the first ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    6

    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;
    }
    Last edited by Carl Fillion; 11-18-2012 at 09:22 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by std10093 View Post
    Code:
    scanf(" %d", &valida);
    (adding a space before d )
    This is redundant: the "%d" conversion specification ignores leading whitespace on its own.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    826
    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]
    Salem likes this.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    6

    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. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    How do I enable these warnings?

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by qny View Post
    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. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Carl Fillion View Post
    How do I enable these warnings?
    gcc -Wall file.c -o executableName

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    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. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    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. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-14-2011, 05:35 PM
  2. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  3. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-27-2005, 11:48 PM
  4. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  5. what am i doing wrong?
    By jlmac2001 in forum C++ Programming
    Replies: 7
    Last Post: 09-13-2003, 10:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21