C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-23-2007, 01:52 PM   #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 !!!!!!!!!!!!!!!!
frodonet is offline   Reply With Quote
Old 09-23-2007, 01:58 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
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.

Salem is offline   Reply With Quote
Old 09-23-2007, 02:06 PM   #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 ?????
frodonet is offline   Reply With Quote
Old 09-23-2007, 02:20 PM   #4
Ex scientia vera
 
Join Date: Sep 2007
Posts: 464
Try escaping the asterisk, like this:

Code:
while( c != '\*' )
...
IceDane is offline   Reply With Quote
Old 09-23-2007, 02:20 PM   #5
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
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.

Salem is offline   Reply With Quote
Old 09-23-2007, 02:28 PM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 09-23-2007, 02:42 PM   #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
frodonet is offline   Reply With Quote
Old 09-23-2007, 02:44 PM   #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 ???
frodonet is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with my program i cant figure out... youareafever C Programming 7 11-01-2008 11:56 PM
A problem with STL vector/list/map with string as a key in multi-threaded program sveta_c123 Linux Programming 4 05-25-2008 12:31 AM
Inheritance and Dynamic Memory Program Problem goron350 C++ Programming 1 07-02-2005 02:38 PM
Problem reading data from file and record it for use in program timmer C Programming 20 06-12-2005 11:53 PM
Program abort due to memory problem cazil C++ Programming 5 01-21-2002 12:55 PM


All times are GMT -6. The time now is 12:01 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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