![]() |
| | #1 |
| Registered User Join Date: Sep 2007
Posts: 45
| 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();
}
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 | |
| | #2 |
| and the hat of Jobseeking 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. |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Sep 2007
Posts: 45
| 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 | |
| | #4 |
| Ex scientia vera Join Date: Sep 2007
Posts: 464
| Try escaping the asterisk, like this: Code: while( c != '\*' ) ... |
| IceDane is offline | |
| | #5 |
| and the hat of Jobseeking 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). |
| Salem is offline | |
| | #6 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
Code: while ( ( c = getchar () ) != '\n')
sentence[i++] = c;
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 | |
| | #7 |
| Registered User Join Date: Sep 2007
Posts: 45
| |
| frodonet is offline | |
| | #8 | |
| Registered User Join Date: Sep 2007
Posts: 45
| Quote:
second, the do while also doesnt work... can you show me the modification that i need to do ??? | |
| frodonet is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |