Thread: Printing problems

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    5

    Printing problems

    I'm trying to write a program that all yous to enter a name into an array and them print that name when you type in it's index. The print command is in a function separate from the main one. The problem is that whenever I try to print that name, I always get (null).

    Here is the code I am working with:

    Code:
    char *first[50];
    char *last[50];
    char blank[50];
    char blank2[50];
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    printf("Enter a name (last, first): ");
    		scanf("%s %s", &blank2, &blank);
    		
    		
    		
    		first[i] = blank;
    		last[i] = blank2;
    		
    		printf("%s %s", &*last[i], &*first[i]);
    		i = i + 1;
    		getchar();
    //To test the program, I have it print out the name I just entered.  This part works works fine.
    
    printf("Enter the index of the name you wish to see (realize that the numbers start a 0): ");
    		scanf("%s", &number);
    		print(number);
    
    }
    
    
    void print(int index){
    
    printf("%s %s", &*last[index], &*first[index]);
    getchar();
    
    //this is the problem area.  
    //I want to print the name associated with the index I inputed, but all I get is two "(null)'s", 
    //one for the first name and one for the last name.
     
    }
    By the way, I'm programming using Visual Studio.

  2. #2
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Hi

    First of all, you have to declare a function prototype at the begining of your code so the linker knows where to link your function calls. Add something like this at the begining of your code (after global variables before main())

    void print(int index);

    If I were you, I would use a while() loop and a control phrase to finish the loop to get the entries. In the way you use, the user can enter only one name! Store the entered named into a char* array. And fetch and diplay the value by using an index.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    ok be prepared to make a few changes in your code:
    >scanf("%s %s", &blank2, &blank);
    blank2 and blank alread are addresses ,so it should be scanf("%s %s",blank2,blank);
    >first[i] = blank;
    u have not declared and initiliazed i any where;
    so use int i=0 or int i =1 as u prefer before this assignment.
    >scanf("%s", &number);
    it should be int number; scanf("%d",&number);

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    I did initialize i and number, I just forgot to copy that part of the code. Sorry about that. Also I put the "&'s" in there because the program will have run-time errors if I don't have them.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    Upon further inspection, it turns out that I was trying to solve the wrong problem. I put in a few more print statements to test the output and it turned out that I wasn't writing to the array properly.

    So any, I figured out what I was doing wrong and it works now. Thank you for your help, everyone!

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Except for one thing....
    Code:
    scanf("%s",blank);
    is identical to
    Code:
    gets(blank);
    and gets() should be avoided at all cost... see this
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with functions and for loops
    By slava in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 01:30 PM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. Printing using DrawText() or TextOut()
    By Eversman in forum Windows Programming
    Replies: 1
    Last Post: 05-24-2004, 12:12 PM
  4. Printing :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-10-2002, 02:49 PM
  5. Printing
    By Dual-Catfish in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-25-2002, 08:10 PM