Thread: problem with do-while loop and char

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    9

    Post problem with do-while loop and char

    THIS IS A PART OF MY PROG.. I M USING A DO-WHILE LOOP. i want e to b a character rather than an integer but when ever i try to run my prog with e as a character it doesnt work all the same... i did change the values in if condition to characters as well... can u plz tell me how to work this with e as a character????

    printf("\n\n\nPress 1 to EXIT or 2 to CONTINUE : ");
    scanf("%d",&e);
    if (e==1)
    break;
    else
    printf ("error: incorrect input");
    }
    while (e==2);
    getch();

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    a character? like when you input "exit", it would be exit? and "continue" it would continue?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Start by fixing the errors first.
    Do not use clrscr. It is non-standard.
    You have failed to include proper headers for the functions you use.
    You lack prototypes of your custom functions.
    main must always return int.
    Some of your functions lack a return type.
    Finally, your indentation sucks. You must address that.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    like when i press Y the prog continue n when i press N it stops

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    @elysia.... there r no errors... it runs smoothly.... i just want e to work with characters.... thnx for the advice though ill work on them

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When scanf() pulls in a char, it leaves a newline char behind. It does the same thing for numbers.

    Whitespace, like newline char's, are skipped over by scanf(), when it's looking for a number for input.

    For char's, scanf() will not skip over a newline char - it will accept the newline as valid input, and make your program act nutty.

    Common problem, by the way. To stop it, add in a

    myVariable = getchar() right after the scanf() line of code. Obviously, don't use the variable you just got from the user for this. If you have to use a garbage variable, do that. (I call mine "gar" for short, when I need it).

    The variable can be a char, or an integer, it doesn't matter. It just needs to pull the newline char, off of the keyboard's buffer.

    No fflush(stdin) won't work - that only works on output buffers, not input.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    you can make the variabel into char type..
    so, you use %s when you scan,, or you can use getch(include<conio.h>)
    after that, you use if to select which one is true
    eq,
    Code:
    char s;
    
    s=getch();
    if(s=='y'||s=='Y')
    {blablabla}
    else if(s=='n'||s=='N')
    {blablabla}
    actually it's so much way to make that, but it is one of them..
    hope it work.. ^^

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    thanx alot pple.... adak ur way worked out.. kinshara the thing is i cant make things work quite the way i wanted to with if statement thanx though... THANKS A MILLION EVERY1

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You THINK it works, but it all honestly, what you do is bad practice.
    Put the code inside a C++ compiler or turn up the warnings on your C compiler.
    It will list a lot of things you should fix.
    Remember: just because it runs does not mean that it is RIGHT.
    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
    Nov 2009
    Posts
    9
    k elysia i dont knw how to indent it ane better but i have added to prototypes, wat did u mean main must always return int.
    Some of your functions lack a return type???

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indentation: it is called google. For example: Indent style - Wikipedia, the free encyclopedia

    main returns int:
    You have: void main()
    Should be: int main()

    Some functions lack return type:
    You have: root ()
    Should be int root()
    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
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    im having another problem just saw it... the root function im want it to return a float value to main... how do i do that cuz no matter what value i enter the answer give 0.0000 ...????

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then declare it to return float.
    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.

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    hey thaanx... that did do the trick.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't get loop to work or validation
    By eminem3150 in forum C++ Programming
    Replies: 11
    Last Post: 01-15-2008, 06:21 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

Tags for this Thread