Thread: Help.. newbie for loop..stuck with the loop..

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    14

    Help.. newbie for loop..stuck with the loop..

    Hi all master,

    question:
    Write a C program to convert lines of input text to uppercase.
    Do not use any standard library functions.
    The program prompts the user to enter each line of text and does the conversion when the
    text is entered. When an asterisk is entered, the program terminates. Assume that only
    lowercase characters and space are entered.
    Example execution (input text in italics):
    Please enter text line: spiderman
    SPIDERMAN
    Please enter text line: sports car
    SPORTS CAR
    Please enter text line:*
    HAVE A NICE DAY


    Please kindly help me take a look for the program written below:
    as i encountered problem with the loop..convert from small letter to upper letter without issue...

    Code:
    #include <stdio.h>
    
    main()
    {
    
    
    
    	char c, d;
    	int i;
    
    
    	do{
    
    	printf("Please enter text line:");
    
    	  scanf("&#37;c",&c);
    
    
    			d=(c&0x5f);
    			printf("%c", d);
    			
    
    	}while(c!='*');
    
    
    
    	printf("Have a good day\n\n");
    }
    Last edited by jochen; 09-29-2007 at 04:07 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    d=(c&0x5f)
    While that may work, I greatly prefer:
    Code:
    #include <ctype.h>
    /* ... */
    d = toupper(c);
    You're also missing a closing curly brace (}) at the end of main().

    You're also not going to be able to hold a line in a single character . . .

    I suggest fgets(); or else print "Please enter text line" only when c is '\n'.
    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.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Its probably a better idea to use getchar(), than scanf() to get a character. You could put it in a while loop something like:
    Code:
    #include <stdio.h>
    
    int main()
    {
        char c;
        while( (c=getchar()) != '*' )
        {
            printf("&#37;c", c);
        }  
    
        return 0;
    }
    [edit] If you want to use a string then you will need a character array to hold it in. To get a string as input use fgets(), like dwk said. [/code]

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Might be a good idea to check for EOF too, in which case c would have to be an int.

    [edit] Oh, and putchar() could be used instead of printf(). [/edit]
    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.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    14
    Quote Originally Posted by dwks View Post
    Code:
    d=(c&0x5f)
    While that may work, I greatly prefer:
    Code:
    #include <ctype.h>
    /* ... */
    d = toupper(c);
    You're also missing a closing curly brace (}) at the end of main().

    You're also not going to be able to hold a line in a single character . . .

    I suggest fgets(); or else print "Please enter text line" only when c is '\n'.
    Sorry while i copy too fast without copy the curly brace (}) at the end of main().
    instead i use c=getchar();

    but the loop problem keep happened.therefore change to
    scanf(); cause really newbie.


    as request by the question given, cant use any library function,

    therefore i can only use ]
    Code:
    d=(c&0x5f)
    .
    can someone enlighten me what's wrong with my loop?

    i keep try and try...seens like
    Code:
    while(c!='*');
    keep loop and loop...end up please enter text line keep looping...

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're only reading one character at a time in the loop. So if you entered something like "tEXt*\n", you'd get
    Code:
    tPlease enter text line:ePlease enter text line:x\
    Please enter text line:tPlease enter text line:*
    for output. Probably not what you were looking for.

    Print the "Please enter text line" message only when the user enters a '\n'. Or perhaps read in text line by line instead of character by character.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    14
    sorry...in doubt..
    how to let the program delay for example 10seconds to show have a good day?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Like, have the program pause for 10 seconds? There's no portable way. See the FAQ, I think there's something on that in there.

    You can use Sleep() from <windows.h>.
    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.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    edited to include time delay I found at http://www.newton.dep.anl.gov/askasci/comp99/CS042.htm
    Unfortunately a value of < 10 ..makes it pause for like 45 seconds from what I see.
    <5 goes for about 9 seconds... if you need it precise then thats a different story. I bet it depends on your compiler too


    Putting what the other person said..
    I don't get what the c&0x5f does though...
    My working equivalent based on the ascii table. by looking at Uppercase 'A' and Lower case 'a'
    I don't believe I used a library function ... as long as you understand what you're code does..

    if (c >='a' && c<='z')
    d=(char)((int)c-32);
    else
    d=c;


    Below seems to work decent..
    Code:
    #include <stdio.h>
    #include <time.h>
    main()
    {
    	char c=' ', d;
    	  time_t start_time, cur_time;
    int i;
     
    
    printf("Please enter text line:");
    	do{
    if (c=='\n')
    	printf("Please enter text line:");
    
    	  scanf("&#37;c",&c);
    	  	d=(c&0x5f);
    	  	/*
    	  	if (c >='a' && c<='z')
    d=(char)((int)c-32);
    */
    			printf("%c", d);
    			
    
    	}while(c!='*');
    time(&start_time);	
    	  do
             {
                     time(&cur_time);
             }
             while((cur_time - start_time) < 5);
    
    
    	
    	printf("Have a good day\n\n");
    }
    Last edited by xsouldeath; 09-29-2007 at 05:06 PM.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't get what the c&0x5f does though...
    It masks away the one bit difference between upper and lower case letters in the ASCII character set. It's kind of dicey though, because this doesn't account for non-latin alphabet characters. toupper is a much better choice because if the character doesn't have an upper case equivalent, it returns the character you passed.

    >d=(char)((int)c-32);
    Why all the casting?
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    14
    hi xsouldeath;


    sorry for my poor understanding...

    Code:
    printf("Please enter text line:");
    
    do{
    if (c=='\n')
    	printf("Please enter text line:");
    is it mean when we press enter (new line), will print out ("Please enter text line:")?
    cool.....


    thanks all of u....

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    Quote Originally Posted by jochen View Post
    hi xsouldeath;


    sorry for my poor understanding...

    Code:
    printf("Please enter text line:");
    
    do{
    if (c=='\n')
    	printf("Please enter text line:");
    is it mean when we press enter (new line), will print out ("Please enter text line:")?
    cool.....


    thanks all of u....
    Yes,
    You print "please enter text line: "
    before looping and then you only want it to say enter a new text line when they enter the "\n" newline (i.e. PRESS ENTER KEY) character. I hope that clarifies it.
    and Prelude has a point about unnecessary casting (d=c-32; ) works fine.
    by the way to match the output above add (before the printf)
    if (c!='*')
    printf("&#37;c", d);
    It would be a trickier special case if you had to allow strings like "Hi my name is *" to be entered, without exiting. i.e. exit only when the entire string entered is only "*".. but ya don't mind that.
    Last edited by xsouldeath; 09-29-2007 at 05:31 PM.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    14
    hi xsouldeath,

    for the delay, is there any other method without use of library function..

    cause the question not allow to use other library funtion other than <stdio.h>


    therefore all senior, any method?
    long time ago..did learn some basic...cant remember sort of count delay to let program terminate later.....

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >for the delay, is there any other method without use of library function..
    Well, at this point all you have left without digging a little too deep is the crappy inaccurate way:
    Code:
    #define MAGIC_NUMBER 12345678
    
    unsigned long x;
    
    for ( x = 0; x < ULONG_MAX && x < MAGIC_NUMBER; x++ )
      ;
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    14
    Quote Originally Posted by Prelude View Post
    >for the delay, is there any other method without use of library function..
    Well, at this point all you have left without digging a little too deep is the crappy inaccurate way:
    Code:
    #define MAGIC_NUMBER 12345678
    
    unsigned long x;
    
    for ( x = 0; x < ULONG_MAX && x < MAGIC_NUMBER; x++ )
      ;

    what is ULONG_MAX ? need to re-declare?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  4. Help! Stuck in a loop!
    By raell in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2003, 10:47 AM
  5. stuck in a loop
    By sweetly in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2003, 01:24 PM