C assignment assist

locutus24

[H]ard|Gawd
Joined
Sep 13, 2004
Messages
1,642
Two weeks I have been working on this program. The user will enter data into the structure then using strcmp I will search for a piece of information and print the results out. A very straightforward program, but I can't get it to print out.

Code:
/*
*Name: 
*Assignment: Structures input assignment
*Date Written: 7 August 2013
*Course: CS133U Summer 2013
*Purpose: Accept user input into
*a structure
*
*Sources: "C A Reference Manual"
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//Defining a structure with
//a tag of Customer
typedef struct{
		char firstName[30];
		char lastName[30];
		char street[35];
		char city[20];
		char state[3];
		int zip;
		char phone[15];
		int accountId;
	}Customer;

int main(){
//information is an array of 10
//Customer structures	
	Customer information[10];
//Char state is for searching
	char state[3];	
//i is for first for loop
//search is for second for loop
//count is for strcmp result
	int i, search, count;

//For loop to accept user input
	for (i = 0; i <= 2; i++){

		printf("Type customer name %d: ", i);
		scanf(" %s", information[i].firstName);

		printf("Type customer last name %d: ", i);
		scanf(" %s", information[i].lastName);

		printf("Type customer street %d: ", i);
		scanf(" %s", information[i].street);

		printf("Type customer city %d: ", i);
		scanf(" %s", information[i].city);

		printf("Type customer state %d: ", i);
		scanf(" %s", information[i].state);

		printf("Type customer zip code %d: ", i);
		scanf(" %d", &information[i].zip);

		printf("Type customer phone number %d: ", i);
		scanf(" %s", information[i].phone);

		printf("Type customer account id %d: ", i);
		scanf(" %d", &information[i].accountId);
	}

//Search for the state in the struct	
	printf("Enter state to search: ");
	scanf(" %s", state);

//Same thing as the first for loop,
//start at zero and increment
	for (search = 0; search <= 2; search++){
		
	//Using strcmp to search the elements		
		if(strcmp(information[search].state, state) != 0){
		count = 1;

		break;
		}
		
		else
			count = 0;

	}

//Below is where it either prints a result if > 1 or prints a 
//message that it couldn't be found

	if (count >= 1){


		printf("Found data for: \n"
			"Customer number: %d\n"
			"Account: %d\n"
			"Name: %s %s\n"
			"Address: %s %s %s %d\n"
			"Phone number: %s\n", 
			informationcopy[search].accountId, 
			informationcopy[search].accountId, 
			informationcopy[search].firstName,
			informationcopy[search].lastName, informationcopy[search].street,
			informationcopy[search].city, informationcopy[search].state, 
			informationcopy[search].zip, informationcopy[search].phone);


	}

	else 
		printf("Not found\n");
	
	return 0;
}

If I enter co all lower and then search co, it won't print that array element. Did I miss a step, because stepping through in a linear fashion everything lines up and should work.

Thank you for any assistance
 
First, I'd like to ask why you're doing your results printing at the end of the program. Have you considered doing this inside the for loop, immediately after determining whether or not a particular array element is a match?

Right now, you're searching through the array, setting a flag the first time you find an element that matches, and then stopping the search in order to print out the match. Do you think this approach is thorough? What will happen if all of the states are co and the user searches for co? Would that behavior be the desired behavior?


Additionally, how else did you test the program? I know what is wrong here, but I would like to lead you into figuring this out through a good testing procedure. What did you try? Did you try searching for a state that won't match?
 
First, I'd like to ask why you're doing your results printing at the end of the program. Have you considered doing this inside the for loop, immediately after determining whether or not a particular array element is a match?

Right now, you're searching through the array, setting a flag the first time you find an element that matches, and then stopping the search in order to print out the match. Do you think this approach is thorough? What will happen if all of the states are co and the user searches for co? Would that behavior be the desired behavior?

That is the other problem, the program to receive full credit must match up all possible results and print each of them. This would require an additional loop, but I am not sure how to flag an array element or copy it to use in an additional loop. Right now the very best I could code was search for one element, using break to print that out. I haven't been able to figure how to print multiple results.

"Prompt the user for values for each of the variables in each structure, i.e. you will be asking for 10 firstName values, 10 lastName values, and so forth. HINT: Loops...

After all of the values for the structure variables have been input, you need to prompt the user for a state string, and then display the values of all variables in each structure where the state string matches the user's input."
 
That is the other problem, the program to receive full credit must match up all possible results and print each of them. This would require an additional loop, but I am not sure how to flag an array element or copy it to use in an additional loop.

An additional loop is not required to achieve that.

Dogs has given you enough information to address that part of the program already, as well as how to find what is causing your "search" to fail. It's a classic error.

Also, where did "informationcopy" come from? That code is not going to compile.
 
Last edited:
An additional loop is not required to achieve that.

Dogs has given you enough information to address that part of the program already, as well as how to find what is causing your "search" to fail. It's a classic error.

What I realized is I had a remnant of my memcpy function leftover with, which was the informationcopy array of structures. Once I change this, I get this output:

Type customer name 1: name1
Type customer last name 1: last1
Type customer street 1: street1
Type customer city 1: city1
Type customer state 1: or
Type customer zip code 1: 9734
Type customer phone number 1: 92342
Type customer account id 1: 1
Type customer name 2: name2
Type customer last name 2: last2
Type customer street 2: street2
Type customer city 2: city2
Type customer state 2: co
Type customer zip code 2: 9342
Type customer phone number 2: 923423
Type customer account id 2: 2
Enter state to search: or
Found data for:
Customer number: 2
Account: 2
Name: name2 last2
Address: street2 city2 co 9342
Phone number: 923423
 
You should be able to see from that test what is wrong now.

If not, make the code adjustments and perform the test that Dog's suggested and the cause of your issue should become immediately clear.
 
I failed to understand the return value of 'strncmp' because bool values say that 1 is true and zero is false. I thought this applied to 'strncmp' as well. But I found a useful website that explained it better:

"Returns

The strncmp function returns an integer. The return values are as follows:

Return Value Explanation
0 s1 and s2 are equal
Negative integer The stopping character in s1 was less than the stopping character in s2
Positive integer The stopping character in s1 was greater than the stopping character in s2"

Strncmp website reference

Code:
		if(strncmp(information[search].state, state, 2) == 0){

		printf("Found data for: \n"
			"Customer number: %d\n"
			"Account: %d\n"
			"Name: %s %s\n"
			"Address: %s %s %s %d\n"
			"Phone number: %s\n", 
			information[search].accountId, 
			information[search].accountId, 
			information[search].firstName,
			information[search].lastName, information[search].street,
			information[search].city, information[search].state, 
			information[search].zip, information[search].phone);

		}

FML :( everything is fixed. I need a beer, two weeks just for that small solution.

Thank you for your assistance
 
There you go!

It&#8217;s an extremely common mistake, so I wouldn&#8217;t feel too bad about it.

As you write more code and work with more languages you&#8217;ll get used to unwritten idioms such as &#8220;compare&#8221; does not mean &#8220;test for equality&#8221; (it&#8217;s just one possible outcome).

Another useful little hint &#8230;

Be careful with the notion of Boolean values in C. In comparisons and evaluations C will treat 0 as false and ANY non-zero value as true (this can be extremely useful, especially in bit manipulations). C99 added a &#8220;_Bool&#8221; type which only accepts 0 and 1.

If you see &#8220;bool&#8221; in a C program, check the file for inclusion of &#8220;stdbool.h&#8221;, if present then the bool is just a macro that resolves to _Bool. If not, make sure you check to see where &#8220;bool&#8221; is defined and understand what&#8217;s being done there.

Enjoy that beer!
 
Welcome to software development :D

Beat me to it. I've found 99% of bugs are those "Whoops, why didn't I find that two weeks ago" type of problems, rather then the "Oh crap, I'm leaking memory!" type of problems.
 
Back
Top