Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,137 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,960 people online right now. Registration is fast and FREE... Join Now!




strange problem with gcc

 
Reply to this topicStart new topic

strange problem with gcc

hellraiser
15 Apr, 2007 - 02:46 PM
Post #1

New D.I.C Head
*

Joined: 12 Jan, 2007
Posts: 4


My Contributions
I've written the following program for adding two binary numbers... When I compile with mingw and visual C under Windows everything works fine. However, when using gcc (version 3.3.3) although it compiles with no errors or warnings i get a very strange result...Try 01010101 and 10101010 for example. How is this possible?

Thanks...

CODE

#include <stdio.h>
#include <stdlib.h>

#define N 8

int main()
{
    int A[N], B[N], SUM[N+1];
    int i, c = 0;
    char ch;
    
    printf("Enter 1st binary number (%d digits):", N);
    for(i=0; i<N; i++) {
             ch = getchar();
             A[i] = ch - '0';
    }
    
    fflush(stdin);
    
    printf("\n\nEnter 2nd binary number (%d digits):", N);
    for(i=0; i<N; i++) {
             ch = getchar();
             B[i] = ch - '0';
    }
  
    for(i=N-1; i>=0; i--)
    {
               if(c==0) {
                        if((A[i]==0 && B[i]==1) || (A[i]==1 && B[i]==0) ) { SUM[i+1] = 1;}
                        if(A[i]==1 && B[i]==1) { SUM[i+1] = 0; c = 1;}
                        if(A[i]==0 && B[i]==0) { SUM[i+1] = 0;}
               }
               else {
                        if((A[i]==0 && B[i]==1) || (A[i]==1 && B[i]==0)) {SUM[i+1] = 0;}
                        if(A[i]==1 && B[i]==1) {SUM[i+1]=1; c=1;}
                        if(A[i]==0 && B[i]==0) {SUM[i+1] = 1; c=0;}
               }
              
    }
    
    if(c==1) SUM[0] = 1;
    else SUM[0] = 0;
    
    printf("\n\nThe sum is: ");
    
    for(i=0; i<N+1; i++) printf("%d", SUM[i]);
    
    printf("\n\n");
    return 0;
}



User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Strange Problem With Gcc
15 Apr, 2007 - 03:08 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Can you provide a description of the output you feel is undesired?
User is offlineProfile CardPM
+Quote Post

hellraiser
RE: Strange Problem With Gcc
16 Apr, 2007 - 12:40 AM
Post #3

New D.I.C Head
*

Joined: 12 Jan, 2007
Posts: 4


My Contributions
A few examples:

00000000, 11111111 -> 0421111111

01010101, 10101010 -> 0421010100

11111111, 11111111 -> 1421111110

I always get that 42 and sometimes the results are really messed up (like the 2nd example above). Under windows i get the correct results...
User is offlineProfile CardPM
+Quote Post

hellraiser
RE: Strange Problem With Gcc
17 Apr, 2007 - 01:54 PM
Post #4

New D.I.C Head
*

Joined: 12 Jan, 2007
Posts: 4


My Contributions
I found the problem... It's the fflush(stdin) line...

Although some implementations/compilers of C will let you fflush(stdin) and have the behaviour as expected, it's not really legal C.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:37PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month