names instead of numbers

names instead of numbers

Page 1 of 1

2 Replies - 624 Views - Last Post: 22 May 2008 - 05:56 PM Rate Topic: -----

#1 serginsurge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-May 08

names instead of numbers

Post icon  Posted 22 May 2008 - 05:24 AM

How do i make it so that i can search for names instead of numbers?

#include<iostream.h>
#include<time.h>
#include<iomanip.h>

int main()
{
const int LISTLENGHT=100;
int i, number;
bool j;
int intlist[LISTLENGHT];

j=false;
srand(time(NULL));

for(i=0;i<LISTLENGHT; i++)
{
intlist[i]=rand()%1000+1;
cout<<setw(12)<<intlist[i];
}
cout<<"What number are you looking for?";
cin>>number;
j=false;

for (i=0; i<LISTLENGHT;i++)
{
if( intlist[i]==number)
{
cout<<number<<" was found at position"<<(i+1)<<endl;;
j=true;
  }
  }
  
  if (j== false)
  cout<<number<<"is not on the list"<<endl;
 
  return 0;
  }



Is This A Good Question/Topic? 0
  • +

Replies To: names instead of numbers

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: names instead of numbers

Posted 22 May 2008 - 06:31 AM

  • Fill your table with a bunch of words.
  • Ask the user to enter a string
  • Compare the strings using strcmp (str1, str2)
  • Or compare the strings using the == operator (if you use C++ strings rather than C-Style strings)
A sample program to check for equality in strings:
#include <iostream>
#include <string>

using namespace std;

int main ()
{
    // C++ style strings:
    string str1 = "hello";
    string str2 = "hello";
    if (str1 == str2)
        cout << "C++ strings match!" << endl;

    if ( !strcmp (str1.c_str(),str2.c_str()) )
        cout << "C++ strings match!" << endl;

    char str3[] = "world";
    char str4[] = "world";

    if (str3 == str4) // this one will not print, since the == operator does not work
        cout << "C strings match!" << endl; // with C-Style strings

    if ( !strcmp (str3,str4) )
        cout << "C strings match!" << endl;

    system ("pause");
    return 0;
}

Hope this helps :)

This post has been edited by gabehabe: 22 May 2008 - 06:39 AM

Was This Post Helpful? 0
  • +
  • -

#3 serginsurge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-May 08

Re: names instead of numbers

Posted 22 May 2008 - 05:56 PM

that really helped, thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1