Some quite different questions

So I have to make an address book until Wednesday. First of all, it has to write and read its information from a binary file. The program must be able to operate with or without arguments passed to main function. If at least one argument is passed, it is assumed to contain the filename to open, if more than one is passed, every argument except the first is ignored. If there are no arguments passed, the first thing the program should do is ask the user to input a filename.
This is the first thing I need help for.
We have to make each entry using structures and no classes. The program must be able to create new entry (done), list all entries (done) search entries, change entries and delete entries, which I have no idea how to do, since it is stated that the file can contain limitless entries, so I cannot use an array to store the entries and their number (which I believe would help me in changing/deleting entries). If you need my code so far, I will post it.
Thanks for the reply but I have read the first article and it is of no help... keep in mind I know nothing about arguments and this article gives me the basics, but nothing more...
Well you'll need to check to see if a command line argument has been passed in. The command line argument will essentially be a character array, so you can use that directly as the filename when it comes to file I/O.

Use an if statement to see if any command line arguments exist. If they do, use that for the filename, else prompt for the filename.
Thanks Hutch but this still does not do it for me... in the article above there is nothing that tells me how to do this... a simple piece of code would be great, if possible
On many IDEs, if you start a new "console project" and have it add a Main entry point, it will look something like this;

1
2
3
4
int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

The first argument, argc, counts how many arguments were passed to the command line. The second one contains those arguments.

Your code will work by checking the value of argc, and then knowing which element of TCHAR is the filename, or whatever.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
   cout << "Exe: " << argv[0] << endl;
   cout << "Arg count: " << argc << endl;

   if(argc > 1)
   {
      for(int i=1; i<argc; i++)
      {
         cout << "Arg[" << i << "]: " << argv[i] << endl;
      }
   }
   else
   {
      cout << "No arguments passed in" << endl;
   }

   return 0;
}
Thanks that did it for me! Now I have troubles with the search option of the address book.Basically the user inputs a string and the program searches through the structures.Problems I am having is that the entries for the first and last name and email are all char[] arrays. What kind of function do I need to use when I need to check whether these char arrays contain the user inputted string, because strcmp does not seem to work for me
anyone?
Another great problem occured and it seems quite simple and yet i cannot get rid of it...
int choice() {
int option=0;
cout<<"[1] Add"<<endl;
cout<<"[2] Change"<<endl;
cout<<"[3] Show all"<<endl;
cout<<"[4] Search"<<endl;
cout<<"[5] Exit"<<endl;
cout<<"Enter an integer ";
cin>>option;
if (option<1 || option>5 || !isdigit(option)){
cout<<"Wrong choice!"<<endl;
choice();}
return option;
this thing works perfectly when the user inputs a negative number or anything above 5.. but enters an endless loop when the user inputs words
i tried with the !option statement too but its the same...
Last edited on
also I cant get strstr() to work because the second argument must be const char* and I need the user to input the words for searching...
You could use a buffer to allow the user the enter as many characters as they wanted, but only the first would be evaluated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const int MAX_BUFFER = 256;
char buffer[MAX_BUFFER];
char input;

cin.getline(buffer, MAX_BUFFER);
input = buffer[0];

if(!isdigit(input) || atoi(&input) < 1 || atoi(&input) > 5)
{
   // invalid
}
else
{
   // valid
}
Topic archived. No new replies allowed.