Thread: adress 2d array

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    8

    adress 2d array

    I am working on an assignment for class:
    Create a program that allows a user to enter up to 10 addresses of friends. Use a two dimensional array to store the address of friends’. After each address is entered, the user should have the option to enter another address or print out a report that shows each addresses entered thus far.
    I have created a code that is coming up without errors, but i am not getting the desired results.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main ()
    {
        char name[10][10] = {0};
        char address[10][10]= {100};
        int choice;
        
        printf("\n\n*****Welcome to the address book!*****\n\n");
        printf("Please enter a name...\n");
        scanf("%s",name[0]);
        printf("Please enter an address...\n");
        scanf("%s",address[0]);
        
        printf("Would you like to (1)Enter another address, or (2)Print the address book?\n");
        scanf("%i",&choice);
        switch (choice)
        {
            case 1:
                for (int i=0;i<10;i++)              //start of the array loop. should give an exit after each entry
                {
                    printf("Please enter a name...\n");
                    scanf("%s",name[i]);
                    printf("Please enter an address...\n");
                    scanf("%s",address[i]);
                    printf("Would you like to (1) Enter another address, or (2)Print the address book?\n");
                    scanf("%i",&choice);
                    if (choice == 1)
                    {
                        for (int i=0;i<10;i++)      // a loop within a loop = loopception
                        {
                            printf("Please enter a name...\n");
                            scanf("%s",name[i]);
                            printf("Please enter an address...\n");
                            scanf("%s",address[i]);
                            printf("Would you like to (1) Enter another address, or (2)Print the address book?\n");
                            scanf("%i",&choice);
                        }
                    } else if (choice == 2)
                    {
                        for (int i = 0; i<10; i ++)     //should print the array as is.
                        {
                            printf("%s\n", name[i]);
                            printf("%s\n", address[i]);
                        }
                    }
                }
                break;
            case 2:
                for (int i = 0; i<10; i ++)
                {
                    printf("%s\n", name[i]);
                    printf("%s\n", address[i]);
                }
                break;
        }
        return (0);
    }
    My trouble is coming from the the output. I am able to fill the array but i am not able to print my desired results. Where am I losing it in the loop? Also after my first entry if i have space in the "address" input the program prints and ends.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    You need another loop.

    use a do .. while loop to get the user to enter up to ten names
    and addresses.
    Then loop through in a second for () loop to print them out.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    8
    I have actually found that by limiting my number of loops I get the desired results. but I am now having trouble filling the array correctly my scanf wont read a _ when inputting an address. For example

    "please enter an address"
    1687 s water

    would output
    "
    1687
    s
    water'
    and that is what I now need to fix.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main ()
    {
        unsigned char name[10][10] = {0};
        unsigned char address[10][10]= {100};
        int choice;
        
        printf("\n\n*****-Welcome to the address book!-*****\n\n");
        for (int i=0;i<10;i++)              //start of the array loop. should give an exit after each entry
        {
            printf("Would you like to (1)Enter an address, or (2)Print the address book?\n");
            scanf("%i",&choice);
            switch (choice)
            {
                case 1:
                {
                    printf("Please enter a name...\n");
                    scanf("%s",name[i]);
                    printf("Please enter an address...\n");
                    scanf("%s",address[i]);
                }
                    break;
                case 2:
                    for (int i = 0; i<10; i ++)
                    {
                        printf("%s\n", name[i]);
                        printf("%s\n", address[i]);
                    }
                    break;
            }
        }
        return (0);
    }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You mean it doesn't accept names or addresses with space characters?

    That is what scanf()'s %s format is required to do. It reads characters into an array of char, and stops when it encounters whitespace (assuming, of course, the array of char is long enough to hold the data being read).

    If you want to read in a whole line (spaces and all) look up the fgets() function. Bear in mind that

    1) fgets() leaves the training carriage return in the string (or stops if the string is too long). You need to cope with that.
    2) it is not a good idea to mix calls of scanf() - or related functions line fscanf() - with fgets() on the same input stream. Each function stops reading for different reasons, and the results (as seen by the user) can be a bit funky - characters being discarded when they shouldn't be, etc. One technique is to use fgets() to read the string, and then use sscanf() [note the extra s in the name] to interpret content of the string.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    8
    Yes for some reason I cant get the scanf to read a space. It reads it as the end of an input and places it the next slot in array i.e.

    (input)
    Please enter a name...
    Bob Slater
    please enter an address..

    please enter a name
    paul
    please enter an adress
    17363

    (print)
    name:Bob
    address:Slater
    Name: paul
    address:17363

    I am not sure how to make it work the way i want it to.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use fgets instead. The problem is that when you do so, you have to be careful with how it interacts with scanf because if you read with scanf, trailing whitespace is left in the input buffer, which will then be read by fgets if you do not remove it first. One way out is to stick to using fgets, then use sscanf when you need the formatted input facilities provided by scanf.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Anduril gave me this link, scanf(3): input format conversion - Linux man page, this entire sight is pretty awesome for finding the function to complete the task.
    Also fgets(3): input of char/strings - Linux man page. fgets is probably the way to go for recording strings in the switch cases. Using fgets to capture input to set off the conditions of the switch might pose problems because it captures newline though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NUMA Virtual Adress Space / Physical Adress Space
    By NUMA Orly in forum Windows Programming
    Replies: 0
    Last Post: 03-14-2011, 03:19 AM
  2. Replies: 4
    Last Post: 11-01-2009, 06:19 AM
  3. MAC adress
    By crvenkapa in forum Tech Board
    Replies: 1
    Last Post: 02-10-2008, 11:24 AM
  4. Tell adress of object from adress of member
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2007, 05:04 AM
  5. moving from one array adress to another
    By jrb47 in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2006, 05:32 PM

Tags for this Thread