LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2004, 06:34 PM   #1
miguetoo
Member
 
Registered: Mar 2003
Location: soCal..
Distribution: lfs server.. slackware workstation..
Posts: 58

Rep: Reputation: 15
strcmp.. ways to make it work..


hi.. i've got my program slightly finished.. it needs to have one function do a compare of a member of a struct but not sure how it's done.. i've been fiddling with this all night last night.. so here is the code..

this is the entire source..
Code:


#include <iostream.h>

#include <fstream.h>

#include <assert.h>

#include <string.h>

//#include <conio.h>

//#include <stdlib.h>



struct Listnode // i cannot edit structures; this is a given..

{

	char lastname[30];

	char firstname[30];

	int age;

};



struct Listtype

{

	Listnode * person[30];

	int size;

};



void initialize (Listtype &List);

void fillList (Listtype &List);

void printList (Listtype &List);

void doInsert (Listtype &List);

void doDelete (Listtype &List);

int menu (void);



main() // i cannot edit main function; this is a given..

{

	Listtype List;

	int choice;

	initialize (List);

	do

	{

//		system("cls"); it clears output.. this is included in menu() instead

		choice = menu();

		switch (choice)

		{

			case 1: fillList(List); break;

			case 2: printList(List); break;

			case 3: doInsert(List); break;

			case 4: doDelete(List); break;

			case 5: break;

			default: cout << "\nINVALID... 1-5 ONLY!\n";

				cin.ignore();

				cin.ignore();

		}

	}

	while (choice != 4);

}



int menu (void)

{

	int choice;

	cout << "\nMENU FOR LAB1\n";

	cout << "1. Fill the list from file laba1data.txt\n";

	cout << "2. Print the list (include the size of the list)\n";

	cout << "3. Insert a record into the list\n";

	cout << "4. Delete a record from the list\n";

	cout << "5. Quit\n\n";

	cout << "ENTER YOUR CHOICE: ";

	cin >> choice;

//	clrscr();

//	system("cls");

	return choice;



}



void initialize (Listtype &List)

{

	for (int i=0; i<30; i++)

	{

		List.person[i]=NULL;

	}

}



void fillList (Listtype &List)

{

	char firstname[30], lastname[30];

	int age, i=0;

	List.size=0;

	ifstream infile ("/home/school/laps/csit536/lab1data.txt", ios::in);

	assert (!infile.fail());



	while (infile>>lastname)

	{

		infile>>firstname;

		infile>>age;

		List.person[i] = new Listnode;

		strcpy(List.person[i]->lastname, lastname);

		strcpy(List.person[i]->firstname, firstname);

		List.person[i]->age=age;

		i++;

	}

	List.size=i;



}



void printList (Listtype &List)

{

	int i;

	for (i=0; i<List.size; i++)

		cout << i+1 << " " << List.person[i]->lastname << " " << List.person[i]->firstname << " " << List.person[i]->age << endl;

	cout << "\nSize of list= " << List.size << endl;

	return;

}



void doInsert (Listtype &List)

{

	char lastname[30], firstname[30];

	int positioninsert, inserted, age;

	cout << "Enter a record in this format: LASTNAME <SPACE> FIRSTNAME <SPACE> AGE \n";

	cin >> lastname >> firstname >> age;

	cout << "Insert this record at what position: ";

	cin >> positioninsert;

	inserted = positioninsert - 1;

// inserted is the position of inserted in the array

	List.size++;

	for ( ;positioninsert<List.size; positioninsert++)

		List.person[positioninsert]=List.person[inserted];

// count how many times List.person is bumped down for insert.. positioninsert is incremented for further bumping in longer lists

	List.person[inserted] = new Listnode;

	strcpy(List.person[inserted]->lastname, lastname);

	strcpy(List.person[inserted]->firstname, firstname);

	List.person[inserted]->age = age;

}



void doDelete (Listtype &List)

{

	char lastname[30], firstname[30];

//	Listtype todelete;

	int i=0, positionremove;

	cout << "Enter the name of the record to delete: ";

	cin >> lastname >> firstname;

	cout << "lastname is " << lastname << " and firstname is " << firstname << endl;

	cout << "List.person[i]->lastname is " << List.person[i]->lastname << " and List.person[i]->firstname is " << List.person[i]->firstname << endl;

	for (i; i<List.size; i++)

	{

//		if (lastname == List.person[i]->lastname && firstname == List.person[i]->firstname)

		if (strcmp(lastname, List.person[i]->lastname) && strcmp(firstname, List.person[i]->firstname)) 

		{

			positionremove = i;

			cout << "position remove is executed" << endl;

			for (i; i<List.size; i++)

			{

				List.person[i]=List.person[i+1];

			}

			break;

		}

		else if (i=List.size)

		{

			cout << "NAME NOT IN LIST" << endl;

			cout << "Hit return to continue" << endl;

		}

	}

	delete List.person[positionremove];

	List.size--;

}
this part is where i'm stuck..
Code:
void doDelete (Listtype &List)

{

	char lastname[30], firstname[30];

//	Listtype todelete;

	int i=0, positionremove;

	cout << "Enter the name of the record to delete: ";

	cin >> lastname >> firstname;

	cout << "lastname is " << lastname << " and firstname is " << firstname << endl;

	cout << "List.person[i]->lastname is " << List.person[i]->lastname << " and List.person[i]->firstname is " << List.person[i]->firstname << endl;

	for (i; i<List.size; i++)

	{

//		if (lastname == List.person[i]->lastname && firstname == List.person[i]->firstname) // this method doesn't work..

		if (strcmp(lastname, List.person[i]->lastname) && strcmp(firstname, List.person[i]->firstname)) 
// so does this method..
		{

			positionremove = i;

			cout << "position remove is executed" << endl;

			for (i; i<List.size; i++)

			{

				List.person[i]=List.person[i+1];

			}

			break;

		}

		else if (i=List.size)

		{

			cout << "NAME NOT IN LIST" << endl;

			cout << "Hit return to continue" << endl;

		}

	}

	delete List.person[positionremove];

	List.size--;

}
i just want the if statement to execute if lastname & firstname == List.person[i]->lastname & List.person[i]->firstname, respectively..

i've tried outputing the value and it comes out right but the if statement doesn't execute.. help..?
 
Old 03-08-2004, 07:13 PM   #2
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
strcmp returns 0 (false) if the strings are equal, so I think you might want
Code:
if(! strcmp(...)) { ... }
Alex
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What are some easy ways to make my gnome desktop look fantastic? Jengo Ubuntu 4 07-05-2005 04:00 AM
How do I make a change to a current kernel? Would 'make oldconfig' work... jtp51 Slackware 11 11-01-2004 11:02 PM
What are some of the easiest ways to make Linux look like XP? aaronshaf Linux - Software 3 05-20-2004 10:55 AM
'make' and 'make install' commands dont work on my system? ginda Linux - Newbie 9 04-18-2004 11:17 AM
strcmp function does not seemed to work Linh Programming 4 06-12-2003 06:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration