Why this do-while loop doesn't work as I expected?

This is a discussion on Why this do-while loop doesn't work as I expected? within the C Programming forums, part of the General Programming Boards category; I just started writing this program... There will be more code inside main, but it already doesn't work as I ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    37

    Why this do-while loop doesn't work as I expected?

    I just started writing this program... There will be more code inside main, but it already doesn't work as I expected it to...
    What it does is: it runs correctly once, and then after execution it runs again, but without stopping for user input? Did I do something wrong???
    Code:
    int main()
    {
    
    
    	char again = 'y';
    	char inputstring[512] = "One, two, three.";
    	char *inputptr;
    	inputptr = inputstring;
    	
    	
    	
    	do
    	{
    		{
    		printf("Enter an English text. Use no more than 511 characters, including white spaces.\n");
    		gets(inputstring);
    		printf("The text you entered was: ");
    		printf(inputstring);
    
    
    		}
    
    
    
    
    
    
    	printf("\nWould you like to enter another text?");
    	again = getchar();
    
    	}
    	while (again == 'y' || again =='Y');
    
    }
    Thanks...

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    Thanks a lot, every single time I'm learning something new here...

  3. #3
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    Post comment

    accordingly to your prog.

    why not ...

    char again ='Y';

    do {
    ..
    ..
    ..
    ..
    } while (toupper(again) == 'Y' );

    >> more faster coding.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The statement:

    if( x == 'a' || x == 'A' )

    Is more than likely as fast, if not faster, than the following statement:

    if( toupper( x ) == 'A' )

    The reason being, the first test does at most two comparisions. The second does one comparision, but has the overhead of either processing a macro (ie: a math operation in changing x to an upper case) or calling a function.

    In which case, the first is likely the faster of the two.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    I'm not supposed to use anything from <ctype.h>, so... toupper would be out of question anyway...
    But thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 01:13 AM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM

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