Hi There,
I have to do this read a list of names from an input file of my own, In main() I declared a Namelist[][] object, read several names from thefile using redirection and then print out the list. I have to add the name to the list only if the name is not already there and the list is not full.I also have to check if the name exists with another method.I wrote these methds in a class NameList and used the object in main.

1-> I am having problem with the spaces in the file.When I read the contents and print them, it prints out the spaces as some junk chars.I really want to print only the names in the file.

2->I cannot get to write the add(char name[]) method which compares the names in the file with the list.

Here is my code:

#include<iostream.h>
#include<fstream>
#include <string.h>
using namespace stdin;

int main( int argc, char* argv[] )
{
	char word[10][21];
      Namelist List[10][21]=new NameList();
	int i,j;
	 ifstream indata(argv[1]);
	 if(indata.is_open())
	   {  while(!indata.eof())
	 	  {
	 	  for(int i=0; i<10; i++)	 		             {
  	     indata.getline(word[i],20,'\n');//the list shud have 10 names of each 20 chars long.The problem is it prints out even the spaces as some junk				 		  }
	 	 }
	 	  indata.close();
	   }
	 else cout << "Unable to open file";

	 for (i=0; i<10; i++) //loop to show the array

	 {
	 	for (j =0; j <20; j++)
	 	{
	 		cout<<word[i][j];
	 	}
	 	cout<<endl;

	 }
	 bool isThere;
	for(int i=0;i<10;i++)
	{
		isThere=strcmp(list[i],name[i]);

	}
	return 0;
}

1. Use code tag (see this forum rules):
[code=c++] source code(s)

[/code]
2. >it prints out the spaces as some junk chars
This code can't print anything because you can't compile it: no such namespace as stdin and no such header as <iostream.h> in C++.
3. You are trying to print all 20 chars but getline didn't read all these chars, it stops when a line ended. Use cout << word[i] << endl; code to print words.

Hi!
Thanks for the reply!yes now I modified the code like this.
Sorry for missing on namespace!!bUt I still cannot figure out how to address the add method on class NameList which adds the name to list after checking if its there.And I use a method contain(char []) to do this.How can I address the add or contain methods to update list in the main program?......Here is the code...

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

class NameList{

	char list[10][21];

      int noofNames;
      public:
      NameList();
      int contain(char []);
      void add(char []);
      int numNames(void);
  };

	NameList::NameList()
	{
		noofNames=0;
	}

	int NameList:: contain(char name[])
	{
		int flag=0;
		for(int i=0;i<10;i++)
		{
			if(!strcmp(name,list[i]))
			{
				return 1;
			}
			else return 0;
		 }

	}

	void NameList::add(char name[])
	{
		int count=0;
		bool j;
		while(count<10)
		{
			for(int i=0;i<10;i++)
			{
				if(contain(name[i])!=1)
				{
					strcpy(list[count][i],name[i]);
					count++;
				}
				else
				cout<<"The is already there in the list "<<endl;
			}

		}
		if(count==10)
		{
			cout<<"The list is full!"<<endl;
		}

	}

	int NameList::numNames()
	{
		int noNames=0;

	}



int main( int argc, char* argv[] )
{
	char word[21];
    NameList List=new NameList();

	int i,j,count=0;

   cout << "prog is : " << argv[ 1 ] << '\n';

  ifstream indata(argv[1]);

  if(indata.is_open())
  {
	  while(!indata.eof()&count<10)
	  {
		  for(int i=0; i<10; i++)
		  {
			  indata.getline(word,20,'\n');
			  List.add(word);

			  cout<<word<<endl;
			  ++count;

		  }

	  }
	  indata.close();
  }
else cout << "Unable to open file";


  return 0;
}

What do you mean to update it in the main form? You need to insantiate your class in the main form, and have a member function in the class which will return data to main. An alternative to this is to just make the list contained within the class public, but this is generally not good practice.

It seems you don't understand basics of character strings presentation and handling in C. Moreover, you present the source code which contains lots of errors. You (and me) can't compile it again. It looks like you have a car without wheels and engine but ask me where is a road to the nearest gas station.

Let's start from the beginning. The char list[10][21] declaration defines an array of 10 arrays of 21 chars. Once more: you have ten arrays of arrays. That's the 1st array (well, the 1st array index is 0 in C and C++ ): list[0] . That's the 2nd array: list[1] - and so on.

Now i-th array list[i] contains exactly 21 char elements. All str family C library functions work with such arrays of char. Look at your add member function code:

if (contain(name[i]) != 1)
{
    strcpy(list[count][i],name[i]);

Think:
1. name[i] is an array. An array is equal (or not equal) to 1???
2. strcpy wants two char arrays. But list[count][i] is NOT an array! It's i-th char of list[count] array. The name[i] construct is NOT an array too, it's i-th char of an array name!

Have you ever seen your compiler diagnostic messages? What for you send this code with severy syntax and semantics errors? What for you write yet another function if the previous one generates compiler diagnostics?

Are you trying to teach yourself c++? If so you need to find a very basic guide that takes you down to the fundamentals of the language to start constucting your knowledge foundation upon. Learn the basics of arrays, and more specifically character arrays in this case before tampering with udt's (such as classes).

I am here to ask some help and figure out whats wrng with my code..I am learning the language now...I dont want anyone to waste time in pointing out fingers ..rather we should be there to encourage each other in a positive way!

Anyways thanks for the "attitude" shown guys......it drove me in 48 hrs after writing the helloworld program to code for an application.and run everything w/o any errors or messages in compiler..

Ofcos when we use a compiler, I or you or anyone out there would see the messages.I tried to post the code only to seek out and discuss with some clearheaded souls out there, as I coundt figure out the meaning of it...Guess this shud be it!


It seems you don't understand basics of character strings presentation and handling in C. Moreover, you present the source code which contains lots of errors. You (and me) can't compile it again. It looks like you have a car without wheels and engine but ask me where is a road to the nearest gas station.

Let's start from the beginning. The char list[10][21] declaration defines an array of 10 arrays of 21 chars. Once more: you have ten arrays of arrays. That's the 1st array (well, the 1st array index is 0 in C and C++ ): list[0] . That's the 2nd array: list[1] - and so on.

Now i-th array list[i] contains exactly 21 char elements. All str family C library functions work with such arrays of char. Look at your add member function code:

if (contain(name[i]) != 1)
{
    strcpy(list[count][i],name[i]);

Think:
1. name[i] is an array. An array is equal (or not equal) to 1???
2. strcpy wants two char arrays. But list[count][i] is NOT an array! It's i-th char of list[count] array. The name[i] construct is NOT an array too, it's i-th char of an array name!

Have you ever seen your compiler diagnostic messages? What for you send this code with severy syntax and semantics errors? What for you write yet another function if the previous one generates compiler diagnostics?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.