Help with while's and scanf's...

This is a discussion on Help with while's and scanf's... within the C Programming forums, part of the General Programming Boards category; hey im new here and i have an exam tomorrow so i need to learn how to do this!!! the ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    12

    Help with while's and scanf's...

    hey
    im new here and i have an exam tomorrow so i need to learn how to do this!!!

    the question is to write a program that prompts the user to enter a value, with a message then being displayed that is dependent on teh value entered....
    the message should be 1 = happy first birthday
    17 = you can learn how to drive
    30 = still young
    40 = getting older

    so far i got:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <CLIB.H>
    
     main()
     { 
       int c;
       printf("Enter value: \n");
       
       while (1)
       scanf("%d",&c);
       
       switch(c)
       {
         case 1: printf("hello you");
       break;
       case 17: printf("you can learn to drive");
       break;
       case 30: printf("still young");
       break;
       case 40: printf("getting older");
       break;
       }
     
       return 0;
       
    
     }
    what am i doing wrong?
    thanks
    Last edited by m600; 01-19-2010 at 01:15 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > case '1'
    Try
    case 1

    Plus a couple of other syntax errors, which I assume because you typed the code rather than copy/pasted.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    tried that and im not getting any errors.....
    all it does is just let me type
    anything else that can help?
    thanks

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    while (1)
    scanf("%d",&c);

    There is your problem right there.
    The while statement affects only the scanf because you haven't added any braces to specify what it should cover.

    Also, do please post your real code because the current one will give you compile errors.
    If you don't get any errors, then this cannot possible be your real code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    while (1)
    scanf("%d",&c);

    There is your problem right there.
    Ie, remove the ; at the end of that line.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    thats was my real code... i corrected the int,c; already
    how do i add braces to specify what it should cover?
    im kinda new at this so yeah....
    thanks a lot


    @MK27 it says there is a semi colon missing if i try run it without it
    thanks

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Quote Originally Posted by MK27 View Post
    Ie, remove the ; at the end of that line.
    Say what?

    Quote Originally Posted by m600 View Post
    thats was my real code... i corrected the int,c; already
    how do i add braces to specify what it should cover?
    im kinda new at this so yeah....
    thanks a lot
    Example:
    Code:
    while(true)
        statement 1;
        statement 2;
    Code:
    while(true)
    {
        statement 1;
        statement 2;
    }
    Red belongs to loop; green does not.
    Last edited by Elysia; 01-19-2010 at 01:14 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    YES IT WORKS!!!
    thank you so much, is it alright if i ask you guys one more thing?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Sure. Ask away.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    ok i have to write a program which counts from 1 to 15 and adds all the odd numbers

    Code:
    #include<stdio.h>
    
    main()
    {
      int a,sum;
    
        sum = 1;
      for (a=1; a<16; a++)
     {
        sum = sum+a;
     }
    	printf("%d", sum);
    
    return 0;
    }
    this is what i got so far but i dont know how to make it count in steps of two, i've messed about with the for statement but the only thing i could think of was have 1:2:15 in it. i was under the impression that 1:2:15 means start number : steps : end number
    any help appreciated

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Since a for loop is generally of the type:
    for (init; condition; post)
    It means you can control what it does before it starts, the condition for when it should loop and what it does after each iteration.
    Currently, you add one to a after each iteration.
    But if you would just want odd numbers, then what should you do?
    If you were to count all odd numbers by hand, how would you do it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    Say what?
    All apologizes -- didn't read the OP very closely. Or maybe at all, it seems

    "Jumping the gun", they call it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    Quote Originally Posted by Elysia View Post
    Since a for loop is generally of the type:
    for (init; condition; post)
    It means you can control what it does before it starts, the condition for when it should loop and what it does after each iteration.
    Currently, you add one to a after each iteration.
    But if you would just want odd numbers, then what should you do?
    If you were to count all odd numbers by hand, how would you do it?
    well i would want the a++ to be a+2, but when ever i try doing that it doesnt work....

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    a++ is the same as a = a + 1 or even a += 1.
    a + 2 is ... well, it adds 2 to a, but never stores the result anywhere so the result goes lost.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    sweet Elysia i got it now!!
    thanks a lot everyone!!!
    i gotta go sleep now
    take care

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