Thread: what's wrong with these?

  1. #1
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38

    what's wrong with these?

    i try to make a program that will tell the user if there is a 0 in his input but why won't it run the way i want it to?
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main(){
        int num[10];
        int i= 0, cnt=0;
        printf("Give me 10 numbers that are greater or equal to 0\n");
        for(i=0; i<10; ++i){
                 scanf("%d", &num[i]);
                 num[i] = i;
                 }
        for(i=0; i<10; ++i)
                 if(num[i] == 0)
                     printf("Yes, there is a 0\n");
                 else
                     printf("No, there is no 0\n");
        getch();
        return 0;
    }

    TO ALL THOSE WITH FRIENDSTER, ADD ME!

    i'm [email protected]
    a newbie in the field of Computer Science!! Pray for all the student's success!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're assigning the counter variable to the array.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You realize that integers are more than one character long, at times, right? Therefor, you know that "3401" is one integer, right? What you should be doing is scanning characters and look for '0'.

    Quote Originally Posted by quzah
    You're assigning the counter variable to the array.
    Also that.
    Sent from my iPadŽ

  4. #4
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38
    the brackets don't work...
    i want it to output only one sentence and even though there is no 0, it outputs "Yes, there is a 0"

    TO ALL THOSE WITH FRIENDSTER, ADD ME!

    i'm [email protected]
    a newbie in the field of Computer Science!! Pray for all the student's success!

  5. #5
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38
    how can i scan characters?

    TO ALL THOSE WITH FRIENDSTER, ADD ME!

    i'm [email protected]
    a newbie in the field of Computer Science!! Pray for all the student's success!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the FAQ on reading a line of text.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38
    i still don't get it you know...

    TO ALL THOSE WITH FRIENDSTER, ADD ME!

    i'm [email protected]
    a newbie in the field of Computer Science!! Pray for all the student's success!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read <--- CLICK CLICK

    Now you know how to read a line from the user. Since you already know how to read each element of an array, it shouldn't be hard to figure out how to read each character in the array and compare it to '0'.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    This any better? Note the line I commented out!!


    Code:
    #include <stdio.h>
    #include <conio.h>
    int main(){
        int num[10];
        int i= 0, cnt=0;
        printf("Give me 10 numbers that are greater or equal to 0\n");
        for(i=0; i<10; ++i){
                 scanf("%d", &num[i]);
           /*   num[i] = i; */
                 }
        for(i=0; i<10; ++i)
                 if(num[i] == 0)
                     printf("Yes, there is a 0\n");
                 else
                     printf("No, there is no 0\n");
        getch();
        return 0;
    }

  11. #11
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    You put 0-9 inthe array every time methinks!!

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by esbo
    This any better? Note the line I commented out!!
    Not by much! The scanf() should be avoided, I'd say.

    However, I do believe I misunderstood the point of this program to find digits equal to zero, not numbers. I guess I thought that since the latter just seems illogical.
    Last edited by SlyMaelstrom; 09-10-2006 at 08:05 PM.
    Sent from my iPadŽ

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You'll probably want to have the second for loop set a flag if it encounters a 0 in the array, which you can check after the loop is finished. As written, if the user enters more than one 0 it will print "Yes, there is a 0" for each one.
    Code:
    0 0 0 0 0 0 0 0 0 0
    Yes, there is a 0
    Yes, there is a 0
    Yes, there is a 0
    Yes, there is a 0
    Yes, there is a 0
    Yes, there is a 0
    Yes, there is a 0
    Yes, there is a 0
    Yes, there is a 0
    Yes, there is a 0
    The variable cnt is unused. (Turning on compiler warnings would probably find this for you. For gcc add -W -Wall to the command line/compiler options.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    ComSci newbie...ICS kid
    Join Date
    Jul 2006
    Location
    PHILIPPINES!!!
    Posts
    38
    that's right.. i get an answer for each one even though i only want ONE answer.

    TO ALL THOSE WITH FRIENDSTER, ADD ME!

    i'm [email protected]
    a newbie in the field of Computer Science!! Pray for all the student's success!

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So you know how to fix it, right? Set a variable, say contains_zero, to 1 in the loop instead of printing a message, and then at the end of the loop print a message depending on the value of contains_zero.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM