Like Tree1Likes

Urgent helpppp!!

This is a discussion on Urgent helpppp!! within the C Programming forums, part of the General Programming Boards category; Originally Posted by Noor Why wont this work? Pay attentio to my comments inside your code Code: #include <stdio.h> int ...

  1. #16
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by Noor View Post
    Why wont this work?
    Pay attentio to my comments inside your code

    Code:
    #include <stdio.h>
    int main(void)
    {
    int digit1,digit1,digit3,digit4;
    //         ^^^^^^ repeat same variable?
    
    printf("Enter an encrypted 4-digit combination:");
    scanf("%d%d%d%d \n",&digit1,&digit2,&digit3,&digit4);
    //             ^^^           ^^^^^^
    // what's with the whitespace?
    // digit2 undefined
    
    // let's suppose the digits were 4, 7, 0, and 3
    digit1=digit1+digit4;                          // digit1 = 7 (4 + 3)
    digit2=9-digit2;                               // digit2 = 2 (9 - 7)
    digit3=9-digit3;                               // digit3 = 9 (9 - 0)
    digit4=digit1-digit4;                          // digit4 = 4 (7 - 3)
    digit1=digit1-digit4;                          // digit1 = 3 (7 - 4)
    
    printf("The real combination is:"); 
    scanf("%d%d%d%d",digit1,digit2,digit3,digit4); // 3294
    // scanf??? don't you want to print the result now?
    return 0; 
    }
    Please improve your indentation. Thank you.
    Last edited by qny; 09-19-2012 at 12:58 PM. Reason: scanf?

  2. #17
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    Code:
    #include <stdio.h>
    int main(void)
    {
    int digit1,digit2,digit3,digit4;
    
    printf("Enter an encrypted 4-digit combination:");
    scanf("%d%d%d%d",&digit1,digit2,&digit3,&digit4);
    digit1=digit1+digit4;
    digit2=9-digit2;
    digit3=9-digit3; 
    digit4=digit1-digit4;
    digit1=digit1-digit4;
    printf("The real combination is:%d",digit1,digit2,digit3,digit4);
    return 0;
    }
    i put in 8021 and thsi is what i got:
    The real combination is:11735520

  3. #18
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    You have been told TWICE what you are doing wrong! Repeating here:
    Quote Originally Posted by Salem View Post
    you will need to type in
    1 2 3 4
    rather than
    1234

    Quote Originally Posted by Salem View Post
    Next, you need to type in say
    1 2 3 4
    as opposed to say
    1234

    That is, you need a space between each digit.
    Now, if you still can't do it correctly, you might have to talk to your instructor.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Break it down, to debug it:

    1) Are you getting the digits into the variables, correctly? Use a printf("%d %d %d %d\n",digit1,digit2,digit3,digit4); getchar(); getchar(); (two of them here), line of code, right after the input is finished, to see how it looks.

    2) Check your equations. What should they be? If you should have only 4 digits, then any digit over 9 is (almost surely), wrong.

  5. #20
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,500
    Quote Originally Posted by Noor View Post
    Why wont this work?
    Code:
    #include <stdio.h>
    int main(void)
    {
    int digit1,digit1,digit3,digit4;
    
    printf("Enter an encrypted 4-digit combination:");
    scanf("%d%d%d%d \n",&digit1,&digit2,&digit3,&digit4);
    digit1=digit1+digit4;
    digit2=9-digit2;
    digit3=9-digit3; 
    digit4=digit1-digit4;
    digit1=digit1-digit4;
    printf("The real combination is:"); 
    scanf("%d%d%d%d",digit1,digit2,digit3,digit4);
    return 0; 
    }
    1) Your first scanf has not changed. See my earlier post on how to do that

    2) You are using scanf at the end of this when you should be using printf.
    Code:
    printf("The real combination is:"); 
    scanf("%d%d%d%d",digit1,digit2,digit3,digit4);
    The only things wrong with your original code were: Line 6 - remove the space and '\n' character as I suggested in post 10 - Remove the '&' in front of the variable names in the last printf as I suggested in post 11.

    If you go back to the original code and change these two things, your code will run.
    Fact - Beethoven wrote his first symphony in C

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using files to store aint working. HELPPpp!!!!
    By success972 in forum C Programming
    Replies: 3
    Last Post: 04-13-2009, 01:32 AM
  2. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 06:35 AM
  3. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 08:05 PM
  4. Help!!! Urgent
    By ashesh in forum C Programming
    Replies: 5
    Last Post: 03-20-2003, 06:19 AM
  5. helpppp!!!!
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 02-13-2002, 09:25 AM

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