Like Tree3Likes

What the ****?

This is a discussion on What the ****? within the C Programming forums, part of the General Programming Boards category; Code: int i=0; char a,b,c,pom; do{ scanf ("%c",&pom); if (pom<65 || (pom>90 && pom<97) || pom>122){ printf ("ERROR"); return 0; ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    30

    What the ****?

    Code:
    int i=0;
    char a,b,c,pom;
    
    do{
    scanf ("%c",&pom);
    if (pom<65 || (pom>90 && pom<97) || pom>122){
    printf ("ERROR"); return 0;
    }
    if (i==0) a=pom;
    if (i==1) b=pom;
    if (i==2) c=pom;
    i++;
    }
    while (i < 3);
    why is it always throwing me ERROR? :S

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Because after every letter you type, you enter a newline character by pressing enter and that causes the ERROR.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    how can i fix it? :S while staying at the do while loop, i can do it with if's but i would like to do it with do while...

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Add this, to basically ignore newlines
    if ( pom == '\n' ) continue;
    turke92 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Firstly, ditch the magic numbers from your code; you don't want incomprehensible 65's and 122's lying around. Represent them meaningfully. 65 represents a capital A, I think, so replace 65 with 'A' (including the single quotes). This will be translated back into a 65 by the compiler. Likewise, replace the other magic numbers with the characters they represent.

    You can add another thing to deal with the newline to your if statement. The newline is represented by '\n'.
    Code:
    while(!asleep) {
       sheep++;
    }

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    solved. i can just input them into one 3-letter word... regarding the "magic" numbers... teachers orders...

    another way: space before %c in scanf!
    Last edited by turke92; 11-03-2011 at 01:20 PM.

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    If your teacher is telling you to produce code that's harder to read and less portable, he is the one in need of instruction.
    Code:
    while(!asleep) {
       sheep++;
    }

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    he wants us to learn some ascii values...

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by turke92 View Post
    he wants us to learn some ascii values...
    Then he should show you THIS rather than starting you off into bad programming practices.
    turke92 likes this.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    we have tables but we need to know some basic values without checking tables!

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by turke92 View Post
    we have tables but we need to know some basic values without checking tables!
    No you don't... you use statements like ... if (char == 'a') you don't put magic numbers in your code.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    look, it's not up to me what he wants us to learn...

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by turke92 View Post
    look, it's not up to me what he wants us to learn...
    Make your instructor happy by doing it his way. Just realize that his way is stupid.
    turke92 likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    New problem!

    The printout must be alphabetized but capital letters must be after the small ones if letters are same...

    Example:
    Input: B b z
    Output: b B z

    Example 2:
    Input: A B z
    Output: A B z

    Code:
    #include <stdio.h>
    
    int main(){
    
    
    char a,b,c,pom,first,second,third;
    int i=0;
    
    
    printf ("Input 3 letters: \n");
    
    
    do{
        scanf (" %c",&pom);
        if (pom<'A' || (pom>'Z' && pom<'a') || pom>'z'){
            printf ("Error.");return 0;
        }
        if (i==0) a=pom;
        if (i==1) b=pom;
        if (i==2) c=pom;
        i++;
        }
    while (i<3);
    
    
    third= (a>b?(a>c?a:c):b>c?b:c);
    first = (a<b?(a<c?a:c):b<c?b:c);
    second = (a+b+c) - (najveci+najmanji);
    
    
    printf ("%c %c %c", first, second, third);
    
    
    return 0;
    }
    I have got to this. Please help me out with solving same letters if one is small and the other one is capital!

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    30
    My bad I forgot to translate "najmanji" and "najveci". "najmanji" is "first" and "najveci" is "third".

    P.S. Sorry for double-posting but I can't edit the above post?

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

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