10 Replies - 1156 Views - Last Post: 26 February 2012 - 05:05 AM Rate Topic: -----

#1 vaizaren  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 24-February 12

printing garbage - arrays of structure ()

Posted 24 February 2012 - 11:32 PM

/*
author:------
student number:----
section: -----
description: exercise about structures
 */
#include<stdio.h>
#include<string.h>
//declaration of the functions
void choose(int x);
void enterdata();
void viewrecords();
void finddata();
void sortdata();
//declaration of the structure named entity
typedef struct
{
	//declaration of the variables inside the structure criminal
	char Number[10];
	char Surname[20];
	char FirstName[20];
	char location[50];
	char crime[50];
}criminal;
//start of the main program
main()
{
	//declaring variables inside the main
	//option is for the variable responsible for repeating the program
	int option;	
	//start of do-while, for repeating the statements inside while option==1
	do
	{
		//declaring x as integer, for the option the user wants to do
		int x;
		//prompring the user about the program
		printf("\n\n*********************************************************************************\nWelcome to the criminal database systems which keep information on criminals and suspects including their criminal history. These information systems are used to track down other criminal activity within and outside different countries, as well as solve high-end cases such as terrorism attacks, hostage cases, and group-organized criminal movement.\n*********************************************************************************\n\nWhat do you want to do? Choose one among these options.\n\n[1]Enter Criminal Data\n[2]View all Criminal Data\n[3]Find Criminal Data\n[4]Sort Criminal Data\n[5]Exit\n\n");
		//asks the user for the option wanted
		scanf("%d", &x);
		//inputs the value of x in the function choose() and executes it afterwards
		choose(x);
		//prompts the user if he/she wants the program to run again
		if (x==5)
		{
			option=2;
		}
		else
		{
		printf("Do you want to run the program again?\n\n[1]Yes\n[2]No\n\n"); 
		scanf("%d", &option);
		}
	//program will keep on running when option==1
	}while (option==1); 
//end of the main program
}
//functions 
//function choose(int x) holds all functions that will be used in the program
void choose(int x)
{
	//if user chooses 1, it prompts the user what he chose and executes enterdata function
	if (x==1)
	{
		printf("\nYou have chosen to enter criminal data.\n\n");
		enterdata();
	}
	//if user chooses 2, it prompts the user what he chose and executes viewrecords function
	else if (x==2)
	{
		printf("\nYou have chosen to view all the criminal records.\n\n");
		viewrecords();
	}
	//if user chooses 3, it prompts the user what he chose and executes finddata function
	else if (x==3)
	{
		printf("\nYou have chosen to find criminal data.\n\n");
		finddata();
	}
	//if user chooses 4, it prompts the user what he chose and executes sortdata function
	else if (x==4)
	{
		printf("\nYou have chosen to sort criminal data.\n\n");
		sortdata();
	}
	//if user chooses 5, it prompts the user what he chose and executes nothing
	else if (x==5)
	{
		printf("\nYou have chosen to exit the entire program.\n\n");
	}
}
//function for placing the data about the criminals
void enterdata()
{
	int i;
	//for loop, for iterating until the last index
	for(i=0;i<20;i++)
	{
		//declaring criminal crim[20], the array that contains 20 criminal records
		criminal list[20];
		//if its the 1st criminal, we need to print 1st so i used if statement when i=0
		if (i==0)
		{
			printf("\nThis data is for the 1st criminal.\n\n");
		}
		//if its the 2nd criminal, we need to print 2nd so i used if statement when i=1
		else if (i==1)
		{
			printf("\nThis data is for the 2nd criminal.\n\n");
		}
		//if its the 3rd criminal, we need to print 3rd so i used if statement when i=2
		else if (i==2)
		{
			printf("\nThis data is for the 3rd criminal.\n\n");
		}
		//and others are nth criminal so i used it when i!=(0,1 and 2)
		else
		{
			printf("\nThis data is for the %dth criminal.\n\n",i+1);
		}
		//prompts the user to input the criminal number
		printf("Please input for the criminal number of the criminal.\n");
		scanf("%s", list[i].Number);
		//prompts the user to input the surname
		printf("Please input for the surname of the criminal.\n");
		scanf("%s", list[i].Surname);
		//prompts the user to input the first name
		printf("Please input for the first name of the criminal.\n");
		scanf("%s", list[i].FirstName);
		//prompts the user to input the place where the criminal was apprehended
		printf("Please input the place where the criminal was apprehended.\n");
		scanf("%s", list[i].location);
		//prompts the user to input the crime that was commited
		printf("Please input for the crime commited.\n");
		scanf("%s", list[i].crime);
	}
}
//function for printing criminal records placed by the user
void viewrecords()
{
	int i, j;
	criminal list[20];
	//for loop, for iterating until the last index
	for(i=0;i<20;i++)
	{
		printf("Criminal Number:\t%s\nCriminal Surname:\t%s\nCriminal First Name:\t%s\nLocation:\t%s\nCrime:\t%s\n",  list[i].Number, list[i].Surname, list[i].FirstName, list[i].location, list[i].crime);
	}
}
//function for finding criminal data by entering criminal's number
void finddata()
{
}
//function for sorting criminal data based on their surname..in ascending order
void sortdata()
{
	//variable temp[1] for swapping
	int i,j;
	criminal temp[1];
	//declaring criminal crim[20], the array that contains 20 criminal records
	criminal list[20];
	//for loop, for iterating until the last index 20
	for(j=0;j<20;j++)
	{
	    for(i=0;i<19;i++)
	    {
		    //compares the 2 indeces to decide if it is to be swapped or not
		    if(strcmp(list[i].Surname,list[i+1].Surname)>0)
		    {
			    temp[0]=list[i];
			    list[i]=list[i+1];
			    list[i+1]=temp[0];
		    }
	    }
	}
	printf("Criminal data is now sorted based on their surnames.\n\n");
}
//function exit is not included..it is blank, just does nothing 



what's wrong with my code?
it has comments for better understanding

need help asap :|
thank you
im new to structures and need help..

nevermind the function finddata()

i just want to know why my code prints garbage

Is This A Good Question/Topic? 0
  • +

Replies To: printing garbage - arrays of structure ()

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: printing garbage - arrays of structure ()

Posted 24 February 2012 - 11:57 PM

Each of your functions creates its own array of criminals. The only one that gets any real data put into it is the one created in enterdata, but that one ceases to exist (goes out of scope) as soon as enterdata finishes. The other functions created their own arrays but those have nothing but random garbage in them.

You should create one array (database) in your main function, and pass that one array as a parameter to each of the other functions so they'll all be working with the same data.
Was This Post Helpful? 1
  • +
  • -

#3 vaizaren  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 24-February 12

Re: printing garbage - arrays of structure ()

Posted 25 February 2012 - 01:22 AM

thank you mister.
ill comment again what happens after i take your advice

This post has been edited by JackOfAllTrades: 25 February 2012 - 04:18 AM
Reason for edit:: Removed unecessary quote

Was This Post Helpful? 0
  • +
  • -

#4 vaizaren  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 24-February 12

Re: printing garbage - arrays of structure ()

Posted 25 February 2012 - 05:18 AM

View Postr.stiltskin, on 24 February 2012 - 11:57 PM, said:

You should create one array (database) in your main function, and pass that one array as a parameter to each of the other functions so they'll all be working with the same data.

how will if do that sir? im so confused right now
:unsure:
Was This Post Helpful? 0
  • +
  • -

#5 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: printing garbage - arrays of structure ()

Posted 25 February 2012 - 06:20 AM

Here is an example of passing around an array:
#include <stdio.h>

void input(int array[], int size);
void output(int array[], int size);

int main() {
    const int size = 10;
    int array[size];
    input(array, size);
    output(array, size);
    return 0;
}

void input(int array[], int size) {
    for(int i = 0; i < size; ++i) {
        scanf("%d", &array[i]);
    }
}

void output(int array[], int size) {
    for(int i = 0; i < size; ++i) {
        printf("%d ", array[i]);
    }
}

Was This Post Helpful? 0
  • +
  • -

#6 vaizaren  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 24-February 12

Re: printing garbage - arrays of structure ()

Posted 25 February 2012 - 06:40 AM

View PostHezekiah, on 25 February 2012 - 06:20 AM, said:

Here is an example of passing around an array:
#include <stdio.h>

void input(int array[], int size);
void output(int array[], int size);

int main() {
    const int size = 10;
    int array[size];
    input(array, size);
    output(array, size);
    return 0;
}

void input(int array[], int size) {
    for(int i = 0; i < size; ++i) {
        scanf("%d", &array[i]);
    }
}

void output(int array[], int size) {
    for(int i = 0; i < size; ++i) {
        printf("%d ", array[i]);
    }
}

i dont really get what you mean sir..can you show it to me using my variables with comments sir?
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: printing garbage - arrays of structure ()

Posted 25 February 2012 - 06:47 AM

Read the tutorials in my signature.
Was This Post Helpful? 0
  • +
  • -

#8 vaizaren  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 24-February 12

Re: printing garbage - arrays of structure ()

Posted 26 February 2012 - 04:09 AM

still cant solve this code...i really need an answer to this question :sweatdrop: help me pls... pls let me see your modifications to my code >.<
Was This Post Helpful? -1
  • +
  • -

#9 vaizaren  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 24-February 12

Re: printing garbage - arrays of structure ()

Posted 26 February 2012 - 04:19 AM

i never got what youre saying
Was This Post Helpful? 0
  • +
  • -

#10 vaizaren  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 24-February 12

Re: printing garbage - arrays of structure ()

Posted 26 February 2012 - 04:57 AM

..ive tried so far sir.. there is no problem in the compilation..but if i run it.. it crashes...

both on devc++ and visual studio 2010
Was This Post Helpful? 0
  • +
  • -

#11 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: printing garbage - arrays of structure ()

Posted 26 February 2012 - 05:05 AM

Have you tried running and understanding the example which Hezekiah pasted?


Have you read the two links which JackOfAllTrades suggested you look at? (The links explain how functions and arguments work)
http://www.dreaminco...t-i-the-basics/
http://www.dreaminco...ing-and-arrays/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1