Like Tree2Likes
  • 1 Post By Subsonics
  • 1 Post By vars19

Code help!!

This is a discussion on Code help!! within the C Programming forums, part of the General Programming Boards category; So i wrote out a snippet of my code for a program that is supposed to add and later simplify ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    19

    Code help!!

    So i wrote out a snippet of my code for a program that is supposed to add and later simplify two fractions. To check if the first part of my program was working, I ran a side program with this code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int a, b, c, e, num, first, second, den, whole, newnum, t;
            
        do
        {
            printf(">> ");
            scanf("%d/%d + %d/%d", &a, &b, &c, &d);
            if((b==0)||(d=0))
            {
                printf("Invalid input: Denominator cannot be zero!");
            }
            if ((b!=0)&&(d=!0))
            {
                break;
            }
        }
        while((b==0)||(d==0));
        if(b<0)
        {
            a=-a;
            b=abs(b);
        }
        if(d<0)
        {
            c=-c;
            d=abs(d);
        }
        printf("%d %d %d %d\n", a, b, c, d);
        first=a*d;
        second=b*c;
        num=first+second;
        den=b*d;
        printf("%d %d %d %d\n", first, second, num, den);
        return 0;
    }
    I printed out values for a b c and d to make sure the computer was getting the correct values from the user. The user's input is supposed to be in this form: a/b + c/d wherein a b c and d are provided by the user. For some reason, whatever is inputted in the d variable, the program still prints out 1 for the d variable. The other variables are fine. Because the d always comes out as 1, then the corresponding variables that rely on what d is come out wrong.

    Please help! I cant seem to locate the problem. Here is a snippet of what happens when i run this program

    Name:  Untitled.jpg
Views: 96
Size:  21.9 KB

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by haroldco View Post
    So i wrote out a snippet of my code for a program that is supposed to add and later simplify two fractions. To check if the first part of my program was working, I ran a side program with this code:
    Code:
    #include <stdio.h>
        do
        {
            printf(">> ");
            scanf("%d/%d + %d/%d", &a, &b, &c, &d);
            if((b==0)||(d=0)) // <-- d = 0
            {
                printf("Invalid input: Denominator cannot be zero!");
            }
            if ((b!=0)&&(d=!0)) // <-- d = !0
            {
                break;
            }
        }
    You don't need that second if statement. But your problem is that you assign 0 to d in the first if statement.

    In the second statement you assign !0 to d meaning 1.
    dennis.cpp likes this.

  3. #3
    Registered User vars19's Avatar
    Join Date
    Jan 2012
    Location
    London
    Posts
    6
    For a start, correct me if I'm just blind but where did you declare d?
    dennis.cpp likes this.

  4. #4
    Registered User vars19's Avatar
    Join Date
    Jan 2012
    Location
    London
    Posts
    6
    when i always get stuck i print out a hello from all the loops and methods etc.

    i did that with your code, Im only a beginner and i don't really use c much but this shows where your value for d is being changed to '0'

    d = the correct input until it reaches the second if statement with the break inside:

    Code:
     scanf("%d%d%d%d", &a, &b, &c, &d);
    printf("%d %d %d %d\n", a, b, c, d);
    if((b==0)||(d==0))
    {
    printf("Invalid input: Denominator cannot be zero!");
    }
    printf("%d %d %d %d\n", a, b, c, d);
    if ((b!=0)&&(d=!0))
    {
    break;
    }
    printf("%d %d %d %d\n", a, b, c, d);
    

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 03:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 08:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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