Thread: [NEED HELP] I just can't solve this program problem!

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

    Unhappy [NEED HELP] I just can't solve this program problem!

    Hi, there this one lab tutorial i need to do:
    The question goes like this :

    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: lord of the rings
    LORD OF THE RINGS
    Please enter text line: star wars
    STAR WARS
    Please enter text line: money no enough
    MONEY NO ENOUGH
    Please enter text line: *
    Have a good day.

    Ive done my program.

    Here it is :

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    main ()
    {
        char c, sentence[80];
        int i=0 ,array=0;
        
        while ( c != '*')
        {
        puts ("\n\nEnter a line of text :");
        while ( ( c = getchar () ) != '\n')
              sentence[i++] = c;
              
        sentence[i] = '\0';
       
        puts("\nThe Converted Text is:");
            
        while (array != (strlen (sentence) ))
        {      
             sentence[array] = toupper ( sentence[array] );
             array++;
        }  
        puts(sentence);
        
        
        }
        getchar();
    }
    The problem is, when i try to loop the program so that it will keep inquire the user to enter new text, it seems that my buffer doesn't get flushed. Means that if i enter a it will converts to A, and when the next line comes and i enter b, it will show AB. I tried flushall(), it doesn't work.

    Second, when i loop with while ( c = ! '*'), it just doesn work, it won't terminate even when i enter asterisk (*).

    Can anyone help me !!!!!!!!!!!!!!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to reset i back to 0 when the loop restarts.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by Salem View Post
    You need to reset i back to 0 when the loop restarts.
    thanks. i've managed to settle the flushing of the memory, i need to set my i=0 and my array=0 and then its working fine.

    But why when i enter asterisk (*), it doesnt terminate...is my code

    while ( c != "*") correct ?????

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Try escaping the asterisk, like this:

    Code:
    while( c != '\*' )
    ...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well c starts off uninitialised (one bad point), then thereafter it will always contain \n (which is the exit condition of the inner while loop).
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by frodonet View Post
    thanks. i've managed to settle the flushing of the memory, i need to set my i=0 and my array=0 and then its working fine.

    But why when i enter asterisk (*), it doesnt terminate...is my code

    while ( c != "*") correct ?????
    It is syntactically correct (using single-quotes, not double quotes), but since
    Code:
        while ( ( c = getchar () ) != '\n')
              sentence[i++] = c;
    guarantees that the only value that c can have after this loop is '\n' [that's the value it has when it exits the loop], your outer while-loop will never see any other condition after the first iteration. The value of c at the start of the first iteration of the loop is undefined (you haven't give c a value), so it may not enter the loop if you are unlucky enough that the "random" value that c happens to have is '*'.

    One solution would be that you compare the string sentence with the string-constant "*" using strcmp() - again, make sure the string is initialized, or you'll have undefined behaviour in the first iteration of the loop.

    Whilst this is a "style" issue, rather than correct/incorrect code, I would prefer to see the outer loop as a "do - while" rather than "while"...

    "do - while" is a "do this at least once, until < some condition > is false", so it's implicit from the code itself that you wish the content of the loop to happen AT LEAST once. It saves the reader of the code to figure out "what is the condition at the beginning of the loop at the first iteration - will this loop be entered at all?".


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by IceDane View Post
    Try escaping the asterisk, like this:

    Code:
    while( c != '\*' )
    ...
    this one doesn't work

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by matsp View Post
    It is syntactically correct (using single-quotes, not double quotes), but since
    Code:
        while ( ( c = getchar () ) != '\n')
              sentence[i++] = c;
    guarantees that the only value that c can have after this loop is '\n' [that's the value it has when it exits the loop], your outer while-loop will never see any other condition after the first iteration. The value of c at the start of the first iteration of the loop is undefined (you haven't give c a value), so it may not enter the loop if you are unlucky enough that the "random" value that c happens to have is '*'.

    One solution would be that you compare the string sentence with the string-constant "*" using strcmp() - again, make sure the string is initialized, or you'll have undefined behaviour in the first iteration of the loop.

    Whilst this is a "style" issue, rather than correct/incorrect code, I would prefer to see the outer loop as a "do - while" rather than "while"...

    "do - while" is a "do this at least once, until < some condition > is false", so it's implicit from the code itself that you wish the content of the loop to happen AT LEAST once. It saves the reader of the code to figure out "what is the condition at the beginning of the loop at the first iteration - will this loop be entered at all?".


    --
    Mats
    i have tried strcmp() - doesn't work
    second, the do while also doesnt work...

    can you show me the modification that i need to do ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  2. Replies: 4
    Last Post: 05-25-2008, 12:31 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  5. Program abort due to memory problem
    By cazil in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2002, 12:55 PM