Need some basic help

This is a discussion on Need some basic help within the C Programming forums, part of the General Programming Boards category; I started studying C today, read one book, and I tried to do something myself, immediately run into problems... Okay, ...

  1. #1
    Beginner
    Guest

    Question Need some basic help

    I started studying C today, read one book, and I tried to do something myself, immediately run into problems...
    Okay, I'm trying to do two things here...

    How to make program enter full names?

    I made a program where you enter names and it is supposed to list them and it says:

    Enter 1st name: John McMutton
    Enter 2nd name Enter 3rd Name:


    So what it does, it works fine only with part John, but when I'm trying to add the space, it screws up, how do I do it right?

    Secondly, I tried to make clock time...
    Lets take 24 hours, not PM or AM thingie...

    I want to know how to make program that counts times like:
    13:00 - 15:00 = 2h or 8:00 + 23:45 = 8.15


    Thanks already

  2. #2
    Beginner
    Guest

    Unhappy Okay, here goes

    The name code:

    #include <stdio.h>
    void main(void)

    {
    char name[7][15];
    int i;
    printf("\n");
    for (i= 0; i <= 6; i++ )
    {
    printf("Give %d. name: ", i +1);
    scanf("\n %s", name[i]);
    }
    printf("\nNames were: ");
    for( i = 0; i <= 6;i++)
    printf("\n %s", name[i]):
    }

    that was the name code...

    i have no idea how to make the clock thingie...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    You read the wrong book, if it told you to do this
    > void main(void)
    main returns an int.

    When you do this, add
    &nbsp; return 0;
    to the end of main

    Fix the scanf call like so
    scanf("%s", name[i]);

    > i have no idea how to make the clock thingie...
    Later - do this first
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Beginner
    Guest

    Umm...

    Sorry if I'm getting on your nerves...
    exactly where i should add the return 0; ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    Code:
    int main ( ) {
        // all your code
        return 0;
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Beginner
    Guest

    Unhappy Didn't help...

    Still saying the same thing:

    Give 1. name: John Doe
    Give 2. name: Give 3. name:


    It always comes when i hit the spacebar...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    You mean it looks like this?
    Code:
    Give 1. name: hello world how
    Give 2. name: Give 3. name: Give 4. name: are you today
    Give 5. name: Give 6. name: Give 7. name: fine
    
    Names were:
     hello
     world
     how
     are
     you
     today
     fine
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Beginner
    Guest
    Yup, that's exactly what it does...

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    Ah, got it

    Using scanf is tricker in this case.

    There are several ways to go, depends what you know already.

    Pick one of these which makes most sense. Replace your scanf call with one of these code fragments.

    Code:
            for ( j = 0 ; j < 15-1 ; j++ ) {
                name[i][j] = getchar();
                if ( name[i][j] == '\n' ) {
                    name[i][j] = '\0';
                    break;
                } else {
                    name[i][j+1] = '\0';
                }
            }
    Code:
            fgets( name[i], 15, stdin );
            name[ strlen(name[i]) - 1 ] = '\0';  /* remove the newline */
    Code:
            scanf( "%[^\n]", name[i] );  /* read all chars upto the \n */
            getchar( ); /* remove the \n */
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  2. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 05:28 AM
  3. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM
  4. Basic Graphics programming in C, please help
    By AdRock in forum Game Programming
    Replies: 3
    Last Post: 03-24-2004, 11:38 PM

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