VERY basic c++ question having to do with arrays

MikeMike

Lifer
Feb 6, 2000
45,885
66
91
I am trying to create a basic database type program for my father so he can use it on his laptop. I know a little but not tons on C++

my code so far is just to get the basic down and get it to work.

The thing i dont know is arrays. He needs to be able to type in a shortname and the program to bring up the product with that shortname.

For example if i type in cp12c it would bring up

CATCH PLAY 1.5 CALL

how do i do this??

have tons of if statements or is it much easier? also how would i be able to compar arrays with a shortname? for example

if (shortname[array]=='123212')
{
cout<<"HI";
}


i know that doesnt work but how can i make it work???


MY BASIC CODING FOR PRACTICE IS BELOW. wat is wrong with it? it will never display hi

#include <iostream.h>

const char array=10;

int main()
{

char* shortname[array];



cout<<"Welcome!! Please type in the shortname of the product you are\nlooking for:::";
cin>>shortname[array];
if (shortname[array]=='13c3')
{
cout<<"HI!!";
}

else if (shortname[array]=='2221k')
{
cout<<"Hi again";
}
else
{
cout<<"There is no product within this database with the following shortname";
cout<<shortname[array];
}

cout<<shortname[array];

return 0;
}


 

singh

Golden Member
Jul 5, 2001
1,449
0
0
It would probably be best to either use a database to run your searches on, or at least store the records in a file. Anyways, an array based program could look something like this:
#include <iostream.h>
#include <string>
using namespace std;


// This stucture will store the short
// name and the corresponding long
// name
struct NamesRef
{
NamesRef(string strShort, string strLong)
{
this->strShort = strShort;
this->strLong = strLong;
}


// Check to see if the short name matches
// If so, set the long name to the match
// and return true
// Else, return false
bool Match(string strShort, string &strLong)
{
// Ignore case and compare
if(stricmp(strShort.c_str(),
this->strShort.c_str()) == 0)
{
strLong = this->strLong;
return true;
}
else
return false;
}

string strShort;
string strLong;
};



void main(void)
{
int o = 0;
const int max_names = 3; // max number of names
NamesRef Names[max_names]=
{
// The Names structure array holds all the
// short-long names up to a maximum
// defines by the variable max_names
NamesRef("cp12c", "CATCH PLAY 1.5 CALL"),
NamesRef("RTCW", "Return To Castle Wolfenstein!"),
NamesRef("VC++", "Visual C++")
};


string strShort;
string strLong;

cout << "Please enter the short name: ";
cout << flush;
cin >> strShort;


// Search for the short name
for(o=0;o < max_names; o++)
{
if(Names[o].Match(strShort, strLong))
{
// Found. Display long name
cout << "\nFull Name is: ";
cout << strLong << "\n";
break;
}
}

// if index is out of bounds, then we
// did NOT find the name
if(o == max_names)
{
cout << "\n** Name not found **\n";
}
}
 

MikeMike

Lifer
Feb 6, 2000
45,885
66
91
damn that is gunna be sum figurin out for me

what does

cout<<flush;
do?

also i sumwhat know how to create files but i do not know how to access and search them with c++

also would there be a way to allow my dad to edit the database file so if they get a new product he can eneter it and also its short name?
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: nourdmrolNMT1
damn that is gunna be sum figurin out for me

what does

cout<<flush;
do?

also i sumwhat know how to create files but i do not know how to access and search them with c++

also would there be a way to allow my dad to edit the database file so if they get a new product he can eneter it and also its short name?

The "flush" just forces the output to the screen. What Operating System is your dad going to use?

There are lots of solutions available, but a nice GUI allowing him access to search/add/delete items would be the best way.

 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
This would be much easier to do in Access or as an Excel sheet since both have good searching and make it easy to add / edit records. Actually even a Wordpad file would work pretty well with one item per line. Sometimes writing code isn't the best answer :) even though doing it in C++ will help you learn.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: DaveSimmons
This would be much easier to do in Access or as an Excel sheet since both have good searching and make it easy to add / edit records. Actually even a Wordpad file would work pretty well with one item per line. Sometimes writing code isn't the best answer :) even though doing it in C++ will help you learn.

Well the ideal way to do it would be to store the records in some sort of a database and write a nice, custom GUI allowing the user to add, delete or search for records. The interface would just wrap up the operations in a SQL query and run it on the database.


 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: singh
Well the ideal way to do it would be to store the records in some sort of a database and write a nice, custom GUI allowing the user to add, delete or search for records. The interface would just wrap up the operations in a SQL query and run it on the database.
Sure, and that would be pretty trivial to do in VB (w/recordsets probably), and not much harder in VC++/MFC if you know your way around the database classes.

But for someone just learning C++, using an existing app like Access or Excel might be a better choice to get something working quickly. If time and effort required isn't an issue then C++ with a homebrew flatfile is a good learning experience.

 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: DaveSimmons
Originally posted by: singh
Well the ideal way to do it would be to store the records in some sort of a database and write a nice, custom GUI allowing the user to add, delete or search for records. The interface would just wrap up the operations in a SQL query and run it on the database.
Sure, and that would be pretty trivial to do in VB (w/recordsets probably), and not much harder in VC++/MFC if you know your way around the database classes.

But for someone just learning C++, using an existing app like Access or Excel might be a better choice to get something working quickly. If time and effort required isn't an issue then C++ with a homebrew flatfile is a good learning experience.

Yes, but you have to have MS Office to get Access or Excel. Which costs $$$:)

 

Chooco

Banned
Apr 5, 2002
731
0
0
sing how did you make that code look like real code? when i post here at anand tech with lots of spaces, it just deleted the spaces and makes everything squished together. check it
1 space
2 space
3 space
4 space
5 space
6 space
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: Chooco
sing how did you make that code look like real code? when i post here at anand tech with lots of spaces, it just deleted the spaces and makes everything squished together. check it
1 space
2 space
3 space
4 space
5 space
6 space

I wrote a program to convert spaces to HTML :D I'm a programmer, after all :) Use the html code ^nbsp; (replace ^ with amperstand) instead of a space.
 

MikeMike

Lifer
Feb 6, 2000
45,885
66
91
sry bout the late responses.

Time is not of the essence but he needs a very very basic program which just has some little sections for him to read the product divided up which wont take long.


Visual basic might be cool i have a book on VB6 but i think thats like 2-3 y.o. whats the latest out?

Windows XP home

 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: nourdmrolNMT1
Visual basic might be cool i have a book on VB6 but i think thats like 2-3 y.o. whats the latest out?
Nothing after VB6 except VB.NET which would require you to buy a new book :) with VB6 you can find a ton of sample code for simple database apps online (www.codeproject.com and Google for more sites).