Thread: What is wrong with this???

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 10:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-14-2011, 06: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-28-2005, 12:48 AM
  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