Thread: Name to Email

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    19

    Name to Email

    Hi!

    I am writing a very simple program that turns someone's name (written first name last name) into an email address in the following format ==>> [email protected]. However when I run the program the output is like this
    => first name(except first initial) [email protected].
    Below is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main()
    
    
    {
        char first;
        char ch;
    
    
    
    printf("enter your firstname lastname:");
    
    
        first  = getchar();
    
        while( (ch=getchar())!=' ')
            printf("%c", ch);
    
        while((ch=getchar())==' ')
            ;
    
    
        do
        { 
            printf("%c",ch);
        }while((ch=getchar())!='\n');
    
        printf("_%c",first);
    printf("@student.smc.edu\n");
    
        return 0;
    
    }
    
    



    And the output:

    enter your firstname lastname: lisa smith

    [email protected]



    It should say [email protected] . Can somebody please help me fix the code, so it prints [email protected] ? Thank you!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You need to read firstname and lastname into char array variables (make sure they're large enough to hold a name). Look into the fgets function, or scanf for this*. Then, you can use printf with the %s modifier to print your email address.

    * EDIT: You could continue to use getchar to read letters into a char array, but it's more tedious than using fgets or scanf, and you have to remember to null terminate the strings.
    Last edited by anduril462; 06-28-2013 at 01:10 PM.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    19
    Thank you! However, we are not permitted to use arrays (do to the fact that 90% of the people in the class I'm taking don't even know what an array is.) So I'm assuming I'll have to fix my code somehow so the loop (which causes the getchar to read letters) creates an output where the characters making up the first name are placed after the lastname_, though when I attempt to do that, I get stuck in an infinite but static loop.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Do you have to read them in first last order, or can you read the lastname first, in which case this becomes trivial?

    EDIT: Can you post a link to the full assignment description, or copy-paste it exactly? Perhaps there is some clue we're missing. Otherwise, I see no way to do this without some sort of array or more complex logic (recursion is one relatively easy possibility) or data structure (e.g. stack).
    Last edited by anduril462; 06-28-2013 at 01:26 PM.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    19
    Actually, I am to read them in last first order. ==>> [email protected] <<== Like that

    Sure

    Assignment.
    You are to ask a user for first name followed by last name.
    Example:
    Lisa Smith
    Your program should get this data and make an e-mail like this
    [email protected]

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A simple way to do this is to have two char arrays: lastname[50], and firstname[50], and use them. Order won't be a problem. Remember to add the underline between them, and then the suffix: @blank.com.

    Use %s for the scanf() and printf() format code for both strings.

    And you're set.

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    19
    I was able to get the entire last name to print (using a do while loop), but when I attempt to do the same thing for the first name, I get an unwanted result.

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    19
    Thank you. I will just ask my professor if he could make an exception and allow me to use an array. (We were supposed to do this with whatever we have learned at this point. He hasn't taught us arrays yet, though I do know how to use them) Though I was told it can be done without arrays.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Maybe I'm missing something really trivial, or some little trick to solving this without arrays, but I don't think so.

    I see nothing that forbids use of arrays in that assignment, nor the use of recursion. Has your professor covered either of those topics yet? Have you talked about strings? Can you use those? A string is really just an array of characters, so if strings are okay, then so are arrays in this case. There is simply no reasonable way to store a string of data like a first name, without an array.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh! You can't use arrays!!

    Really?

    When you start programming, isn't it time the students learned about the basic data structures they can use in programming? Frame a wooden house, but you can't use a hammer or a nail, or a saw, or ...

    What has the instructor hinted or stated, that you SHOULD use?

  11. #11
    Registered User
    Join Date
    Jun 2013
    Posts
    19
    No, he has not taught us about any of the things you have mentioned (but I DO know how to use them). All we have learned at this point is loops (while, for, do while) , if else statements, get char, printf, scanf, pointers,... Basic stuff. We are to learn about arrays next week, immediately after the assignment is due. The last time I did an assignment with arrays, he made me redo it, using a while loop. And I am wondering, how is it possible to do that with the last name but not the first name? Sorry, I'm such a programming n00b, I started about 2 weeks ago.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The assignment CAN be done with just char's, but it's like building a fence where you have to mine the metal and forge the wire you need, then grow the trees and mill them down to make the fence posts.

    Maybe I'm missing something here - not used to thinking along these lines.

    Does the first name HAVE TO be given to the program first? If the order could be changed to lastname given first, then firstname, it would be simpler.
    Last edited by Adak; 06-28-2013 at 01:54 PM.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if else statements, get char, printf, scanf, pointers,... Basic stuff.
    Please don't tell me your prof's idea of using scanf to read a string is

    char *str;
    scanf("%s",str);


    because it's broken!

    It's even stranger that you didn't learn anything about arrays when you learnt about pointers, given the close relationship between them.
    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.

  14. #14
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    The assignment CAN be done with just char's, but it's like building a fence where you have to mine the metal and forge the wire you need, then grow the trees and mill them down to make the fence posts.

    Maybe I'm missing something here - not used to thinking along these lines.
    It's not bad to occasionally do something mundane without one or more of your usual tools. It keeps the mind keen.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by sonjared View Post
    It's not bad to occasionally do something mundane without one or more of your usual tools. It keeps the mind keen.
    You'll change your mind the first time you need to hang wall board, and have one arm in a cast and sling.

    It DOES refresh your dictionary of cuss words quite keenly, however.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# email
    By mixalissen in forum C# Programming
    Replies: 5
    Last Post: 07-02-2008, 09:53 AM
  2. Email...
    By Junior89 in forum Windows Programming
    Replies: 2
    Last Post: 04-02-2007, 12:56 AM
  3. i got this in an email !
    By blitzkrieg in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 02-07-2003, 06:14 PM
  4. email
    By iain in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-13-2002, 10:57 PM
  5. email in dos
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 01-13-2002, 12:10 PM

Tags for this Thread