Thread: increaments that wont work!!

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    14

    increaments that wont work!!

    ok ive wrote an example program to help me with a project but its not working.
    basically,the user has to enter the number "1".after this is done my program SHOULD display the amount of times it has been entered correctly,and incorrectly.(the answer to each of these answers will either be 1 or zero,no loop)

    but the problem is,even when i enter it incorrectly,it says i have entered it correctly once,which i havent!

    Code:
    #include <stdio.h>
    main()
    {
         int crt, incrt, num;
         
         
         
         incrt=0 ;
         crt=0 ;
         num = 1 ;
         
        
         
         printf("please enter the num 1\n") ;
         scanf("%d",&num) ;
         
         if  (num=1)
         {
                      printf("Correct!!") ;
                      crt++ ;
         }//end if crt
         
         else if (num != 1)
         {
                    printf("Incorrect!!") ;
                    incrt++ ;
         }//end if incrt
         
         
         printf("Incorrectly entered = %d",incrt) ;
         printf("Correctly entered = %d",crt) ;
        
         
         
         
         
          }//end main
    any help at all will be much appreciated!

  2. #2
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Quote Originally Posted by Optimus Prime
    Code:
         if  (num==1)
         {
                      printf("Correct!!") ;
                      crt++ ;
         }//end if crt
    heh. watch out for that.

    Quote Originally Posted by Optimus Prime
    Code:
         printf("Incorrectly entered = %d",incrt) ;
         printf("Correctly entered = %d",crt) ;
    what are these doing there?

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    if  (num=1)...
    This sets num to 1. It should be:
    Code:
    if (num == 1)...
    EDIT: Whoops. divineleft beat me to it
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    oh so if i change it it will work?


    the 2 printfs at the end should show how many times its been entered righ or wrong..

  5. #5
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Yes, it will work.

    I still don't know why you need them, if it's correct, you know you entered it correctly once (same applies to != 1). Unless you wanted it to be a loop.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yes. = will assign the second argument to the first, thus it sets num equal to one, making that statement always true. == compares the values and returns true if they are equal.
    EDIT: Aaaaaah! Stop beating me to the post!
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >main()
    Omitting the type doesn't relieve you of your responsibility to return an int. In fact, "good style" C and the latest standard dictate that you must always specify the return type.

    >oh so if i change it it will work?
    Did you try it?
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    yes thank you,i have to aply this to myproject thats why i had the printfs there.thanks to all.and yes i had a main()..

  9. #9
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Quote Originally Posted by Optimus Prime
    yes thank you,i have to aply this to myproject thats why i had the printfs there.thanks to all.and yes i had a main()..
    Prelude meant to use a type with main()

    so

    Code:
    int main()
    {
         //code...
         return (0); // returns 0 because main is an int
    }
    instead of just "main()"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM