Thread: Having 3 codes into 1

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    5

    Having 3 codes into 1

    Hi everyone. I'm new to coding, just learning C.

    I have three seperate codes I want to use and put together as one code:
    1st:
    Code:
    {
        char firstName[255];
        char lastName[255];
            
        printf("Enter your First Name: ");
        scanf("%s", firstName);
        printf("Enter your Last Name: ");
        scanf("%s", lastName);
        printf("Hello, %s %s!\n", firstName, lastName);
        
    }
    2nd:
    Code:
    {
        char favColor[20];
        printf("Hello, firstname, What is your favorite color? ");
        gets(favColor);
        printf("\nThanks, Buddy! Now I know that your favorite color is %s\n", favColor);
        printf("\n");
    }
    3rd:
    Code:
    {
        bool stayAlive = true;
        char* userPrompt = "Do you want to exit this program (Y/N)? ";
        char c;
        printf("%s", userPrompt);
        while(stayAlive)
        {
            c = getchar();
            if(c == '\n')
                continue;
            else if(c == 'Y' || c == 'y')
                stayAlive = false;
            else
                printf("%s", userPrompt);
        }
        puts("\n\nHave a nice day, Buddy!");
        return 0;
        
    }
    I want to have all of these into one code for this program to work. The basics on what I want is:
    - Person enters their name (Code 1)
    - Gives a Greeting (Code 1)
    - Welcomes the persons name, and asks their fav. color. (Code 2)
    - Program tells them their fav. color. (Code 2)
    - After those are done, program asks if person wants to leave. (Code 3)
    All in that order. I was wondering if someone could help put this all together.
    It all words excepte code 2. It prints the code, but doesnt allow the user to enter any info.

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you use scanf(), you leave the newline char behind in the input stream. So in #2, the gets() reads the input stream UP TO THE FIRST NEWLINE IT FINDS, and says "I'm done!", and the code goes on to the next line of code.

    Add a getchar() after the scanf(), and see if that doesn't help out the gets() in #2. (This is a very frequent problem with students learning C, because neither the input stream, nor the newline, can be seen.)

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Quote Originally Posted by Adak View Post
    When you use scanf(), you leave the newline char behind in the input stream. So in #2, the gets() reads the input stream UP TO THE FIRST NEWLINE IT FINDS, and says "I'm done!", and the code goes on to the next line of code.

    Add a getchar() after the scanf(), and see if that doesn't help out the gets() in #2. (This is a very frequent problem with students learning C, because neither the input stream, nor the newline, can be seen.)
    Ok, I'm not understanding what you are saying....
    could you provide an example?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @jacob ...

    Have you tried just pasting the 3 sections into one .c file and compiling it?

    From there its mostly a matter of eliminating errors and debugging your code.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Yes, I did try. But still didnt work.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by JacobC View Post
    Ok, I'm not understanding what you are saying....
    could you provide an example?
    Sure. print any value that you have entered via scanf(). Does the cursor stay on the same row of text as it started with?

    Yes, it does. That means that the object you entered does NOT contain a newline char - but clearly you DID hit the enter key! So where is the newline that was generated when you pressed the enter key?

    Hiding in the Amazon jungle?

    Nope! It's waiting in your keyboard buffer - for your very NEXT input - to POUNCE on it. It's no problem for numbers, because they're larger, and scanf() for them, will scan over them -- too small they are. But for a char? They're perfectly sized. For a fgets() that always ends it's input by checking for a newline - perfect!

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Quote Originally Posted by Adak View Post
    Sure. print any value that you have entered via scanf(). Does the cursor stay on the same row of text as it started with?

    Yes, it does. That means that the object you entered does NOT contain a newline char - but clearly you DID hit the enter key! So where is the newline that was generated when you pressed the enter key?

    Hiding in the Amazon jungle?

    Nope! It's waiting in your keyboard buffer - for your very NEXT input - to POUNCE on it. It's no problem for numbers, because they're larger, and scanf() for them, will scan over them -- too small they are. But for a char? They're perfectly sized. For a fgets() that always ends it's input by checking for a newline - perfect!
    Ok, when I execuite the command, this is what I get,
    http://i1198.photobucket.com/albums/...re524/hihi.jpg
    It skips the second code...
    could you rewrite it so I can see what I did wrong?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Hiding in the Amazon jungle?
    That would explain the outcry about this constantly accumulating pile of bits of trash down there....

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JacobC View Post
    Ok, when I execuite the command, this is what I get,
    http://i1198.photobucket.com/albums/...re524/hihi.jpg
    It skips the second code...
    could you rewrite it so I can see what I did wrong?
    Yes it does... and Adak just told you why.
    You need to listen to him.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    Quote Originally Posted by CommonTater View Post
    Yes it does... and Adak just told you why.
    You need to listen to him.
    like I said, I have no ........ing clue on what to do in C.
    Im totally new, and know nothing.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JacobC View Post
    like I said, I have no ........ing clue on what to do in C.
    Im totally new, and know nothing.
    Then you don't need to be coming in here swearing and whining.

    Grab yourself a decent book or tutorial... start on page 1, read the whole thing, every word, do all the examples and exercises... by the time you get to the last page I should hope you will realize just how ridiculous you sound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c codes need help,thanks
    By hth373737 in forum C Programming
    Replies: 21
    Last Post: 11-23-2008, 10:45 PM
  2. Need some C++ codes...
    By blah569 in forum C++ Programming
    Replies: 5
    Last Post: 10-05-2005, 02:21 PM
  3. zip codes
    By missny in forum C Programming
    Replies: 10
    Last Post: 09-16-2005, 02:39 AM
  4. VK codes
    By Hunter2 in forum Windows Programming
    Replies: 10
    Last Post: 08-24-2002, 09:58 AM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 04:06 PM