Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Drowning into an ocean of despair called C++


ghost's Avatar
0 0

Hmmmm……does anyone know how I can implement a search function within an array in C++???


ghost's Avatar
0 0

Same coding as in my other post


ghost's Avatar
0 0

What kind of search are you looking to do?

give an example of the array you want to search ect.


ghost's Avatar
0 0

cubeman372 wrote: What kind of search are you looking to do?

give an example of the array you want to search ect.

[see below for codes]


ghost's Avatar
0 0

Hmm a chunk of code that asks for details and stores them in an array right?

Now what do you want the search function to search for exactly?


ghost's Avatar
0 0

The array is gonna store data about employees and i need to be able to retrieve the data for ex by using name as a parameter…

Search for the record of Mr X for example…


ghost's Avatar
0 0

Ahh in that case use the names of people array[MrX] instead of the standard integer form array[0]

and then just cin the name to a empty variable loop through the array for name matches and cout the matches.

NOTE: I know this is long winded and there is a simpler way to do it, but I cant be arsed to get my C++ book out. :p


ghost's Avatar
0 0

a more smplistic way of finding the contents of an array, is to use a for loop:

using namespace std;

int main ()
{
	int arr[10];
	arr[0]=1;
	arr[1]=2;
	arr[2]=3;
	arr[3]=4;
	arr[4]=5;
	arr[5]=6;
	arr[6]=7;
	arr[7]=8;
	arr[8]=9;
	arr[9]=10;
	//search for the number 1;
	for(int b=0;b<10;b++)
	{
		if(arr[b] == 1)
		{
			cout << "arr[" << b << "] holds the value of 1" << endl;
		}
		else
		{
			cout << "arr[" << b << "] doesnt not hold the value of 1" << endl;
		}
	}
	system("pause");
}```

ghost's Avatar
0 0

Thnx but it's not based on ay parameter the user would input…


ghost's Avatar
0 0

Then edit it to do so.


ghost's Avatar
0 0

Updated the code…NOW IT DOESN'T WORK!!!!

// 789.cpp : main project file.

#include "stdafx.h" #include "stdlib.h" #include "time.h" #include <sys/types.h> #include <sys/timeb.h> #include <stdio.h> //Returns values to the progra fo the Date function #include <iostream> #include <string.h> using namespace std;

struct employee { char name[30]; char address[35]; char gender; char team; char project[30]; char department; float number_project_completed; float sales_pro;//sales made from the totality of projects float rank;//an assignment for the renk which the person possese within the company. The smallest number representing the most important char not;//nothing to do with program//to be removed

}; //array decl employee dbase[10]; int i = 1; int choice;

int main() { //nothing to do with program//to be removed

//Declarations for time reading and displaying
char tmpbuf[128], timebuf[26], ampm[] = &quot;AM&quot;;//setting AM format
struct _timeb tstruct;


cout&lt;&lt;&quot;&#92;n================================================================================&quot;;
cout&lt;&lt;&quot;			ey3s of cha0s program				S.P&quot;;
cout&lt;&lt;&quot;&#92;n================================================================================&quot;;

// Set time zone from TZ (Time Zone) environment variable. If TZ is not set,
// the operating system is asked to obtain the default value 
// for the variable in order to find the time. 
//
_tzset();

// Displaying operating system-style date and time. in this case, that of Windows XP
_strtime_s( tmpbuf, 128 );
printf( &quot;&#92;n time:&#92;t&#92;t&#92;t&#92;t%s&#92;n&quot;, tmpbuf );
_strdate_s( tmpbuf, 128 );
printf( &quot;&#92;n date:&#92;t&#92;t&#92;t&#92;t%s&#92;n&quot;, tmpbuf );

cout&lt;&lt;&quot;&#92;n--------------------------------------------------------------------------------&quot;;
cout&lt;&lt;&quot;&#92; Enter Choice.. 1: enter new record...2:do nothing&quot;;
cin&gt;&gt;choice;
switch (choice)
{
case 1:
	cout&lt;&lt;&quot;&#92;n [If a statement appears several times, then you have surely made a mistake] &#92;n&quot;;
	cout&lt;&lt;&quot;Please use only non-capital letters...&lt;&lt;a&gt;&gt; not &lt;&lt;A&gt;&gt;&quot;;
	cout&lt;&lt;&quot;&#92;n      &quot;;

	cout &lt;&lt; &quot;&#92;n Please enter name: &quot;;
	cin &gt;&gt; dbase[i].name;
	cout &lt;&lt; &quot;&#92;n Please enter address: &quot;;
	cin &gt;&gt; dbase[i].address;

//Start of loop aiming at restricting values for &quot;gender&quot;
//While loop
do
  
{cout&lt;&lt;&quot;&#92;n Enter gender of employee:&quot;;   
cout&lt;&lt;&quot;Key in M for Male and F for Female: &quot;;
	cin&gt;&gt;dbase[i].gender;


	}  while (dbase[i].gender != &#39;f&#39;&& dbase[i].gender != &#39;m&#39;);
		
		if (dbase[i].gender == &#39;m&#39; )
		cout&lt;&lt;&quot;&#92;n You entered male&quot;;
		else 
			if (dbase[i].gender == &#39;f&#39;)
			cout&lt;&lt;&quot;&#92;n You entered Female&quot;;
	cout &lt;&lt; &quot;&#92;n &quot;;

//Start of loop aiming at restricting values for &quot;department&quot;
//While loop
do
			
	{cout&lt;&lt;&quot;&#92;n Enter Department. R: Research		D:	Development	&quot;;
	cin&gt;&gt;dbase[i].department;
	
	}  while (dbase[i].department != &#39;d&#39;&& dbase[i].department != &#39;r&#39;);
		//Capital letters not considered by program
		if (dbase[i].department == &#39;d&#39; )
		cout&lt;&lt;&quot;&#92;n You entered Development Department&quot;;
		else 
			if (dbase[i].department == &#39;r&#39;)
			cout&lt;&lt;&quot; &#92;n You entered Research Department&quot;;
		
	cout&lt;&lt;&quot;&#92;n&quot;;

// Start of loop aiming at restricting values for &quot;team&quot;

do 	{cout&lt;&lt;&quot;&#92;n Enter Team.	a: Team Alpha	o: Team Omega	&quot;;
	cin&gt;&gt;dbase[i].team;
	}	while (dbase[i].team != &#39;a&#39;&& dbase[i].team != &#39;o&#39;);
			
			if (dbase[i].team == &#39;a&#39; )
			cout&lt;&lt;&quot;&#92;n You entered Team Alpha&quot;;
			else
				if (dbase[i].team == &#39;o&#39; )
				cout&lt;&lt;&quot;&#92;n You entered Team Omega&quot;;


	cout&lt;&lt;&quot;&#92;n&quot;;

	cout&lt;&lt;&quot;&#92;n Enter Project the person is responsible of :&quot;;
	cin&gt;&gt;dbase[i].project;
	cout&lt;&lt;&quot;Project name is: &quot;&lt;&lt;dbase[i].project;
	cout&lt;&lt;&quot;&#92;n&quot;;
	
	cout&lt;&lt;&quot;&#92;n Enter number of projects completed by the employee: &quot;;
	cin&gt;&gt;dbase[i].number_project_completed;
	cout&lt;&lt;&quot;The employee has acheived &quot;&lt;&lt;dbase[i].number_project_completed&lt;&lt;&quot; projects &quot;;
	cout&lt;&lt;&quot;&#92;n &quot;;
	cout&lt;&lt;&quot;&#92;n Input name&quot;;
	cin&gt;&gt;dbase[i].name;

	for(dbase[i] &lt; 11);
	{ 
		if 
		(dbase[i]== dbase[i].name)
		{
		cout&lt;&lt;&quot;array&quot;&lt;&lt;dbase[i].name&lt;&lt;dbase[i].address;
		}
		else cout&lt;&lt;&quot;not found&quot;;
	}
		


	cout&lt;&lt;&quot;&#92;n This shit works!!!!!!!!&quot;;
	cin&gt;&gt;dbase[i].not;
	
	break;

case 2:
	cout&lt;&lt;&quot;&#92;n This **** works!!!!!!!!&quot;;
	cin&gt;&gt;dbase[i].not;
	break;
default:
	cout&lt;&lt;&quot;Error!!!! Brain Crash!!&quot;;
	cin&gt;&gt;dbase[i].not;

}

return 0; }


ghost's Avatar
0 0

The only thing I can say about your code is that 'o' != 'O'

(POC)

void CompareOutput()
{
static char lowercaseO = &#39;o&#39;;
static char uppercaseO = &#39;O&#39;;

/* Proof of Concept that &#39;o&#39; != &#39;O&#39; */

std::cout&lt;&lt;(int)lowercaseO
         &lt;&lt;&quot;&#92;n&#92;r&quot;
         &lt;&lt;(int)uppercaseO
         &lt;&lt;&quot;&#92;n&#92;r&quot;;

/* Solution: Use toupper() or tolower() to
   make input conform to one case         */

std::cout&lt;&lt;&quot;&#92;n&#92;r&quot;
         &lt;&lt;(int)(toupper(lowercaseO))
         &lt;&lt;&quot;&#92;n&#92;r&quot;
         &lt;&lt;(int)(toupper(uppercaseO))
         &lt;&lt;&quot;&#92;n&#92;r&quot;;
}

ghost's Avatar
0 0

Okay…thnx..

got it!

Problem solved…

…hmm..now i need to make a file…and be able to extract data from it….


ghost's Avatar
0 0

ey3s here:: modified the code..works now..


#include &quot;stdafx.h&quot;
#include &quot;stdlib.h&quot;
#include &quot;time.h&quot;
#include &lt;sys/timeb.h&gt;
#include &lt;stdio.h&gt;
#include &lt;iostream&gt;
//#include &lt;string&gt; to be used later for files i think
using namespace std;

struct employee
{
	//different data types
	char name[30];
	char address[35];
	char gender;
	char team;
	char project[30];
	char department;
	float number_project_completed;
	float sales_pro;//sales made from the totality of projects
	float rank;//an assignment for the renk which the person possese within the company. The smallest number representing the most important
	char not;//nothing to do with program//to be removed

};
//array declaration
employee dbase[10];
int i = 0; //pointer 1
int x = 0;// pointer 2
int choice;

int main()
{
	
	char tmpbuf[128], timebuf[26], ampm[] = &quot;AM&quot;;//setting AM format
 

	cout&lt;&lt;&quot;&#92;n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=&quot;;
	cout&lt;&lt;&quot;			ey3s of cha0s program				S.P&quot;;
	cout&lt;&lt;&quot;&#92;n==*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=&quot;;
	
	// Set time zone from TZ (Time Zone) environment variable. If TZ is not set,
    // the operating system is asked to obtain the default value 
    // for the variable in order to find the time. 
    //
    _tzset();

    // Displaying operating system-style date and time. in this case, that of Windows XP
    _strtime_s( tmpbuf, 128 );
    printf( &quot;&#92;n time:&#92;t&#92;t&#92;t&#92;t%s&#92;n&quot;, tmpbuf );
    _strdate_s( tmpbuf, 128 );
    printf( &quot;&#92;n date:&#92;t&#92;t&#92;t&#92;t%s&#92;n&quot;, tmpbuf );

//selection
	cout&lt;&lt;&quot;&#92;n--------------------------------------------------------------------------------&quot;;
	cout&lt;&lt;&quot;&#92; Enter Choice...  Enter New Record:1    Test:2   &quot;;//test 2 to be rm
	cin&gt;&gt;choice;

	switch (choice)
	{
	case 1:
		cout&lt;&lt;&quot;&#92;n [If a statement appears several times, then you have surely made a mistake] &#92;n&quot;;
		cout&lt;&lt;&quot;Please use only non-capital letters...&lt;&lt;a&gt;&gt; not &lt;&lt;A&gt;&gt;&quot;;//problem to be fixed later
		cout&lt;&lt;&quot;&#92;n      &quot;;

		cout &lt;&lt; &quot;&#92;n Please enter name: &quot;;
		cin &gt;&gt; dbase[i].name;
		cout &lt;&lt; &quot;&#92;n Please enter address: &quot;;
		cin &gt;&gt; dbase[i].address;

//Start of loop aiming at restricting values for &quot;gender&quot;
	//While loop [iteration]
	do
	  
	{cout&lt;&lt;&quot;&#92;n Enter gender of employee:&quot;;   
	cout&lt;&lt;&quot;Key in M for Male and F for Female: &quot;;
		cin&gt;&gt;dbase[i].gender;


		}  while (dbase[i].gender != &#39;f&#39;&& dbase[i].gender != &#39;m&#39;);
			
			if (dbase[i].gender == &#39;m&#39; )
			cout&lt;&lt;&quot;&#92;n You entered male&quot;;
			else 
				if (dbase[i].gender == &#39;f&#39;)
				cout&lt;&lt;&quot;&#92;n You entered Female&quot;;
		cout &lt;&lt; &quot;&#92;n &quot;;

//Start of loop aiming at restricting values for &quot;department&quot; [iteration]
	//While loop
	do
				
		{cout&lt;&lt;&quot;&#92;n Enter Department. r: Research		d:	Development	&quot;;
		cin&gt;&gt;dbase[i].department;
		
		}  while (dbase[i].department != &#39;d&#39;&& dbase[i].department != &#39;r&#39;);
			//Capital letters not considered by program--2 be fixed LATER but SOON
		
			if (dbase[i].department == &#39;d&#39; )
			cout&lt;&lt;&quot;&#92;n You entered Development Department&quot;;
			else 
				if (dbase[i].department == &#39;r&#39;)
				cout&lt;&lt;&quot; &#92;n You entered Research Department&quot;;
			
		cout&lt;&lt;&quot;&#92;n&quot;;

// Start of loop aiming at restricting values for &quot;team&quot; [iteration]

	do 	{cout&lt;&lt;&quot;&#92;n Enter Team.	a: Team Alpha	o: Team Omega	&quot;;
		cin&gt;&gt;dbase[i].team;
		}	while (dbase[i].team != &#39;a&#39;&& dbase[i].team != &#39;o&#39;);
				
				if (dbase[i].team == &#39;a&#39;)
				cout&lt;&lt;&quot;&#92;n You entered Team Alpha&quot;;
				else
					if (dbase[i].team == &#39;o&#39;&& dbase[i].team == &#39;O&#39; )
					cout&lt;&lt;&quot;&#92;n You entered Team Omega&quot;;


		cout&lt;&lt;&quot;&#92;n&quot;;

		cout&lt;&lt;&quot;&#92;n Enter Project the person is responsible of :&quot;;
		cin&gt;&gt;dbase[i].project;
		cout&lt;&lt;&quot;Project name is: &quot;&lt;&lt;dbase[i].project;
		cout&lt;&lt;&quot;&#92;n&quot;;
		
		cout&lt;&lt;&quot;&#92;n Enter number of projects completed by the employee: &quot;;
		cin&gt;&gt;dbase[i].number_project_completed;
		cout&lt;&lt;&quot;The employee has acheived &quot;&lt;&lt;dbase[i].number_project_completed&lt;&lt;&quot; projects &quot;;
		cout&lt;&lt;&quot;&#92;n &quot;;

//searching technique -- for statement/loop
		char name1[30];
		cout&lt;&lt;&quot;&#92;n Input name of employee you wanna search data for: &quot;;
		cin&gt;&gt;name1;
	
		for( x=0; x&lt;=i ; x++)
			{ 
				if (strcmp(dbase[i].name, name1) == 0)
		
					cout&lt;&lt;&quot;address  &quot;&lt;&lt;dbase[i].address;
					
				else cout&lt;&lt;&quot;not found&quot;;
			}
			


		cout&lt;&lt;&quot;&#92;n This one** works!!!!!!!!&quot;;
		cin&gt;&gt;dbase[i].not;
		
		break;

	case 2:
		cout&lt;&lt;&quot;&#92;n This **** works too!!!!!!!!!!&quot;;
		cin&gt;&gt;dbase[i].not;
		break;


	default:
		cout&lt;&lt;&quot;Error!!!! No such choice available!!&quot;;
		cin&gt;&gt;dbase[i].not;

	}
//end of searching

return 0;
} 

//FILE STRUCTURE TO BE DONE```

ghost's Avatar
0 0

ey3s here:: modified the code..works now..


#include &quot;stdafx.h&quot;
#include &quot;stdlib.h&quot;
#include &quot;time.h&quot;
#include &lt;sys/timeb.h&gt;
#include &lt;stdio.h&gt;
#include &lt;iostream&gt;
//#include &lt;string&gt; to be used later for files i think
using namespace std;

struct employee
{
	//different data types
	char name[30];
	char address[35];
	char gender;
	char team;
	char project[30];
	char department;
	float number_project_completed;
	float sales_pro;//sales made from the totality of projects
	float rank;//an assignment for the renk which the person possese within the company. The smallest number representing the most important
	char not;//nothing to do with program//to be removed

};
//array declaration
employee dbase[10];
int i = 0; //pointer 1
int x = 0;// pointer 2
int choice;

int main()
{
	
	char tmpbuf[128], timebuf[26], ampm[] = &quot;AM&quot;;//setting AM format
 

	cout&lt;&lt;&quot;&#92;n=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=&quot;;
	cout&lt;&lt;&quot;			ey3s of cha0s program				S.P&quot;;
	cout&lt;&lt;&quot;&#92;n==*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=&quot;;
	
	// Set time zone from TZ (Time Zone) environment variable. If TZ is not set,
    // the operating system is asked to obtain the default value 
    // for the variable in order to find the time. 
    //
    _tzset();

    // Displaying operating system-style date and time. in this case, that of Windows XP
    _strtime_s( tmpbuf, 128 );
    printf( &quot;&#92;n time:&#92;t&#92;t&#92;t&#92;t%s&#92;n&quot;, tmpbuf );
    _strdate_s( tmpbuf, 128 );
    printf( &quot;&#92;n date:&#92;t&#92;t&#92;t&#92;t%s&#92;n&quot;, tmpbuf );

//selection
	cout&lt;&lt;&quot;&#92;n--------------------------------------------------------------------------------&quot;;
	cout&lt;&lt;&quot;&#92; Enter Choice...  Enter New Record:1    Test:2   &quot;;//test 2 to be rm
	cin&gt;&gt;choice;

	switch (choice)
	{
	case 1:
		cout&lt;&lt;&quot;&#92;n [If a statement appears several times, then you have surely made a mistake] &#92;n&quot;;
		cout&lt;&lt;&quot;Please use only non-capital letters...&lt;&lt;a&gt;&gt; not &lt;&lt;A&gt;&gt;&quot;;//problem to be fixed later
		cout&lt;&lt;&quot;&#92;n      &quot;;

		cout &lt;&lt; &quot;&#92;n Please enter name: &quot;;
		cin &gt;&gt; dbase[i].name;
		cout &lt;&lt; &quot;&#92;n Please enter address: &quot;;
		cin &gt;&gt; dbase[i].address;

//Start of loop aiming at restricting values for &quot;gender&quot;
	//While loop [iteration]
	do
	  
	{cout&lt;&lt;&quot;&#92;n Enter gender of employee:&quot;;   
	cout&lt;&lt;&quot;Key in M for Male and F for Female: &quot;;
		cin&gt;&gt;dbase[i].gender;


		}  while (dbase[i].gender != &#39;f&#39;&& dbase[i].gender != &#39;m&#39;);
			
			if (dbase[i].gender == &#39;m&#39; )
			cout&lt;&lt;&quot;&#92;n You entered male&quot;;
			else 
				if (dbase[i].gender == &#39;f&#39;)
				cout&lt;&lt;&quot;&#92;n You entered Female&quot;;
		cout &lt;&lt; &quot;&#92;n &quot;;

//Start of loop aiming at restricting values for &quot;department&quot; [iteration]
	//While loop
	do
				
		{cout&lt;&lt;&quot;&#92;n Enter Department. r: Research		d:	Development	&quot;;
		cin&gt;&gt;dbase[i].department;
		
		}  while (dbase[i].department != &#39;d&#39;&& dbase[i].department != &#39;r&#39;);
			//Capital letters not considered by program--2 be fixed LATER but SOON
		
			if (dbase[i].department == &#39;d&#39; )
			cout&lt;&lt;&quot;&#92;n You entered Development Department&quot;;
			else 
				if (dbase[i].department == &#39;r&#39;)
				cout&lt;&lt;&quot; &#92;n You entered Research Department&quot;;
			
		cout&lt;&lt;&quot;&#92;n&quot;;

// Start of loop aiming at restricting values for &quot;team&quot; [iteration]

	do 	{cout&lt;&lt;&quot;&#92;n Enter Team.	a: Team Alpha	o: Team Omega	&quot;;
		cin&gt;&gt;dbase[i].team;
		}	while (dbase[i].team != &#39;a&#39;&& dbase[i].team != &#39;o&#39;);
				
				if (dbase[i].team == &#39;a&#39;)
				cout&lt;&lt;&quot;&#92;n You entered Team Alpha&quot;;
				else
					if (dbase[i].team == &#39;o&#39;&& dbase[i].team == &#39;O&#39; )
					cout&lt;&lt;&quot;&#92;n You entered Team Omega&quot;;


		cout&lt;&lt;&quot;&#92;n&quot;;

		cout&lt;&lt;&quot;&#92;n Enter Project the person is responsible of :&quot;;
		cin&gt;&gt;dbase[i].project;
		cout&lt;&lt;&quot;Project name is: &quot;&lt;&lt;dbase[i].project;
		cout&lt;&lt;&quot;&#92;n&quot;;
		
		cout&lt;&lt;&quot;&#92;n Enter number of projects completed by the employee: &quot;;
		cin&gt;&gt;dbase[i].number_project_completed;
		cout&lt;&lt;&quot;The employee has acheived &quot;&lt;&lt;dbase[i].number_project_completed&lt;&lt;&quot; projects &quot;;
		cout&lt;&lt;&quot;&#92;n &quot;;

//searching technique -- for statement/loop
		char name1[30];
		cout&lt;&lt;&quot;&#92;n Input name of employee you wanna search data for: &quot;;
		cin&gt;&gt;name1;
	
		for( x=0; x&lt;=i ; x++)
			{ 
				if (strcmp(dbase[i].name, name1) == 0)
		
					cout&lt;&lt;&quot;address  &quot;&lt;&lt;dbase[i].address;
					
				else cout&lt;&lt;&quot;not found&quot;;
			}
			


		cout&lt;&lt;&quot;&#92;n This one** works!!!!!!!!&quot;;
		cin&gt;&gt;dbase[i].not;
		
		break;

	case 2:
		cout&lt;&lt;&quot;&#92;n This **** works too!!!!!!!!!!&quot;;
		cin&gt;&gt;dbase[i].not;
		break;


	default:
		cout&lt;&lt;&quot;Error!!!! No such choice available!!&quot;;
		cin&gt;&gt;dbase[i].not;

	}
//end of searching

return 0;
} 

//FILE STRUCTURE TO BE DONE```

Uber0n's Avatar
Member
0 0

ey3s_of_cha0s wrote: ey3s here:: modified the code..works now..

Sweet :D see you on msn


ghost's Avatar
0 0

i need to know how to convert char to int in c++….

also got problems with the file structure…

and need to compare fields within the array to sort them out…is this possible???

Kinda like display all the records in order of [any integer related to them]


ghost's Avatar
0 0

Take a look at std::atoi()

As for your other two questions, I'm not quite sure what you're asking.


ghost's Avatar
0 0

No need for atoi(), a simple markupstatic_cast&lt;int&gt;(myCharValue); will work just fine (assuming he really wants char to int and not an array of characters). Also, C++ has the standard template library for a reason, so why aren't you using std::string etc. As for your other questions, well you'll have to be a bit more specific on what you mean.

Cheers, T-Metal