Please help me with cases!!!

This is a discussion on Please help me with cases!!! within the C Programming forums, part of the General Programming Boards category; HI everyone, I hopw someone out there can help me as this problem is really annoying me. I'm trying to ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    Question Please help me with cases!!!

    HI everyone, I hopw someone out there can help me as this problem is really annoying me. I'm trying to write a program that adds two 2's complement binary numbers together. I have done this using cases, and a switch statement. When compliling there are no errors, but when i go to execute it, it doesn't print out anything. I've checked to see where my code is failing by putting printf("I'm here!"); after the switch statement, but this does not print out, so it seems to be my switch statement that is wrong. I have not used switch or cases before so could someone please have a look at my program to see where i'm going wrong.
    Here is my code:

    #include <string.h>
    #include <stdio.h>
    #include <math.h>

    void bintodec(int);
    void printbin(void);
    char store[32]={0};
    char addbin1[32]={0};
    char addbin2[32]={0};
    char answer[32]={0};


    int count=0;
    int n=0;
    int i=0;
    int result=0;
    int length,ab_length;
    int leftbit=0;
    int j=0;
    int cb=0;
    int k=0;
    unsigned int Total=0;

    int main()
    {
    printf("Enter binary number: ");
    scanf("%s", &store);

    printbin();

    bintodec(Total);

    printf("Please enter two equal length binary numbers\n");
    printf("1st binary number: \n");
    scanf("%s", &addbin1);
    printf("2nd binary number: \n");
    scanf("%s", &addbin2);
    printf("These are your two chosen binary numbers:\n 1st: %s\n 2nd: %s\n", addbin1, addbin2);
    ab_length=strlen(addbin1)-1;
    for(j=ab_length;j>=0;j--)
    {
    if(addbin1[j] =='0')
    {
    switch(addbin2[j])
    {
    printf("I'm here!");
    case '0':
    {
    if(cb =='0')
    {
    answer[j] = '0';
    }
    else if (cb == '1')
    {
    answer[j] ='0';
    }
    }
    case '1':
    {
    if(cb=='0')
    {
    answer[j] ='0';
    cb = '1';
    }
    }
    }
    }

    else if(addbin1[j] =='1')
    {
    switch(addbin2[j])
    {
    case '0':
    {
    if(cb == '0')
    {
    answer[j] ='1';
    }
    else if(cb=='1')
    {
    answer[j]='0';
    cb = '1';
    }
    }
    case '1':
    {
    if(cb == '0')
    {
    answer[j] = '0';
    cb = '1';
    }
    else if (cb == '1')
    {
    answer[j] = '1';
    cb = '1';
    }
    }
    }
    }
    }
    if(cb == '1')
    {
    printf("overflow has occurred");
    }

    printf("Your answer is: %s ", answer);
    for(k=ab_length; k>=0; k--)
    {
    printf("%c", answer[k]);
    }

    return 0;
    }


    void printbin(void)
    {
    printf("The binary number you entered is: %s \n", store);
    }

    void bintodec(int)
    {

    length = strlen(store)-1;


    for(i=length;i>0;i--)
    {
    if (store[i]=='1')
    {
    result += pow(2,strlen(store)-i-1);

    }
    }
    if(store[0] == '0')
    {
    printf("The decimal is %d", result);
    }

    else if(store[0] =='1')
    {

    leftbit = pow(2, strlen(store)-1);
    /*printf("leftbit is:%d", leftbit);*/
    Total = result - leftbit;
    }

    printf("The result is %d\n", Total);
    }

    Thank you!!!



  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > When compliling there are no errors, but when i go to execute it, it doesn't print out anything.
    Odd, I get 5 warnings.

    cpr-test.c: In function `main':
    cpr-test.c:27: warning: char format, different type arg (arg 2)
    > scanf("%s", &store);
    Should be
    scanf("%s", store);
    cpr-test.c:32: warning: char format, different type arg (arg 2)
    cpr-test.c:34: warning: char format, different type arg (arg 2)

    cpr-test.c:44: warning: unreachable code at beginning of switch statement
    > printf("I'm here!");
    If it's not inside a case, then it isn't going to happen

    cpr-test.c: In function `bintodec':
    cpr-test.c:112: parameter name omitted
    > void bintodec(int)
    You mean
    void bintodec(int foo_var_name)
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 07:42 AM
  2. How define template with "exception" cases?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2008, 12:55 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. I need to use 0 to quit a program
    By cwest in forum C Programming
    Replies: 2
    Last Post: 12-15-2001, 07:37 AM

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