h/w help

This is a discussion on h/w help within the C Programming forums, part of the General Programming Boards category; Code: else if (input = 'I') fputs("@#", stdout); etc......

  1. #16
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    else if (input = 'I')
    fputs("@#", stdout);
    etc...
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #17
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    the code works but its still the same problem.


    it subsitutes the vowels with a #, on the the other hand each vowel is required to have its own subsitute.

    ie

    user enters as i
    out put #s %


    any ideas on how to do this?

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look at your if check. See the problem?
    Code:
    if( foo = 'a' )
    This assigns 'a' to 'foo'. Try it with two equal signs.

    = assigns
    == checks equality

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

  4. #19
    Registered User
    Join Date
    Oct 2003
    Posts
    12

    Talking

    It worked

    Whats confusing is that the output for the first series of characters is not working correctly if you start it with 'a'

    sample output

    all i need in life i c

    Encryted Text Below...
    ll @ n%%d @n l@f% @ c


    As you can see, the character that is to be in the place of 'a' is missing. If i enter the same sentence again, without exiting the program then it will correctly display.



    Code:
    #include "stdio.h"
    #include "stdafx.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    int input;
    
    printf("\nEnter text to encypt...\n");
    	
    getchar();
    
    printf("\n\nEncryted Text Below...\n");
    
    do{
    	input = getchar();
    	
    	// if a character is equivalent to a vowl. it will be repalced with what it is == to.
    	if(input != 'a' && input != 'i' && input != 'o' && input != 'u' && input != 'e' && input != 'A' && input != 'I' && input != 'O' && input != 'U' && input != 'E') 
    		fputc(input,stdout);
    	
    	else if (input == 'a')
    		fputs("!", stdout);
    	else if (input == 'i')
    		fputs("@", stdout);
    	else if (input == 'o')
    		fputs("#", stdout);
    	else if (input == 'u')
    		fputs("$", stdout);
    	else if (input == 'e')
    		fputs("%", stdout);
    	else if (input == 'A')
    		fputs("^", stdout);
    	else if (input == 'I')
    		fputs("@#", stdout);
    	else if (input == 'O')
    		fputs("@#", stdout);
    	else if (input == 'U')
    		fputs("^%^", stdout);
    	else if (input == 'E')
    		fputs("%$@", stdout);
    	
    		}
    while(input != '0'); // check to make sure user has input
    
    
    return 0;
    }

  5. #20
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    it's because of the extra call to getchar():
    Code:
    printf("\nEnter text to encypt...\n");
    	
    getchar();  // here
    
    printf("\n\nEncryted Text Below...\n");
    one solution I can think of is using ungetc, something like this:
    Code:
    printf("\nEnter text to encypt...\n");
    	
    input = getchar();
    
    printf("\n\nEncryted Text Below...\n");
    
    ungetc( input , stdin );

  6. #21
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by glUser3f
    ...
    one solution I can think of is using ungetc, ...
    Yuk! Change the first getchar() to fgets() to read the entire line in all at once into a buffer.

    Then in the while loop, simply look at each character in the buffer.

    And if you know the switch statement, use that instead of all the if statements. If not, get rid of the HUGE if statement and put the fputc(input,stdout); at the end of your if-block:
    Code:
    else if (input == 'O')
        fputs("@#", stdout);
    else if (input == 'U')
        fputs("^%^", stdout);
    else if (input == 'E')
        fputs("%$@", stdout);
    else
        fputc(input,stdout); // If no if above got called, do not translate
    Last edited by WaltP; 10-21-2003 at 09:38 AM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick H/W question...
    By djz3r0 in forum C++ Programming
    Replies: 15
    Last Post: 09-12-2004, 08:36 PM

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