Thread: Junk from memory after program runs

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    5

    Junk from memory after program runs

    I have a simple program that accepts user input of first and last names. . Input is terminated if "blank/empty" string is entered The names are concatenated.

    The output prints however many first and last names.

    After the program prints however many names, there are several lines of junk as if the loop still running.

    If there a way to set output loop equal to the input loop?




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {//start of main function
    
     char firstName[50] = "";
     char lastName[50] = "";
     char catName[50][50];
     int i = 0;
     int j = 0;
     int INPUT = 50;
     int OUTPUT[INPUT] = 50;
     for (i = 0; i < INPUT; i++){
                printf("Please enter first name\n");
                gets(firstName);
        if(strcmp(firstName, "")==0){
            break;
        }
        printf("Please enter last name\n");
        gets(lastName);
            strcat(firstName, " ");
            strcat(firstName,lastName);
            strcpy(catName[i], firstName);
                
     }
    
     for (i = 0; i < OUTPUT; ++i) {
      printf("%s\n",catName[i]);
     }
    
     system("pause");
     return 0;
    }//end of main function.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for (i = 0; i < OUTPUT; ++i)
    You need to output the number of names entered, not the entire array (which does contain junk).

    Also, never use gets()
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    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
    Oct 2016
    Posts
    5
    At a loss on how to output the number of names.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you have a counter of the number of names entered, if you think about it.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    5
    Figured it out. Thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program in c that runs on a PC but not on another
    By first100 in forum C Programming
    Replies: 2
    Last Post: 03-17-2015, 09:13 AM
  2. Error when my program runs
    By Just4Learning in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2007, 07:20 PM
  3. Get an Error as soon as the program runs.
    By sara.stanley in forum C++ Programming
    Replies: 5
    Last Post: 05-28-2006, 11:47 PM
  4. Program runs choppy
    By Smartiepants586 in forum C Programming
    Replies: 11
    Last Post: 06-22-2005, 04:57 AM
  5. multiple runs of a program
    By unregistered in forum Linux Programming
    Replies: 5
    Last Post: 03-15-2002, 07:18 AM

Tags for this Thread