GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Oct-2004, 02:12
Kay Chan Kay Chan is offline
New Member
 
Join Date: Sep 2004
Posts: 15
Kay Chan is on a distinguished road

Problem in array


I want to store the starting port in an array. If the starting ports exists in the array, then it won't be stored. However, I'm in trouble about scanning. When the starting port exists in the array, then the loop stops. The txt file is:

ABC FFF 1
DEF FFF 2
ABC FFF 2 <---after loading this, the program is stopped.
GTH FFF 2
FTR FFF 2
KOJ FFF 2
DEF FFF 2

How to make it keep going and ignore the duplicate name ?
CPP / C++ / C Code:
    while ( !feof(fptr) ){
		
		fscanf(fptr, "%s %s %i", Starting_port, Destination_port, &distance );		

		if( ArrayChecking(Starting_port) != true ) {	
		    strcpy(  ports[count].IDName, Starting_port );
			cout << count << " " << ports[count].IDName << endl;
			count++;
		} 
	}

bool ArrayChecking( char * starting_port ){
	
	for ( int i = 1; i <= 101; i++ ){
		if ( strcmp( ports[i].IDName, starting_port ) == 0 ){
			return true;
		}
	}
}
Last edited by dsmith : 05-Oct-2004 at 07:07. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 05-Oct-2004, 07:58
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Kay Chan
I want to store the starting port in an array. If the starting ports exists in the array, then it won't be stored. However, I'm in trouble about scanning. When the starting port exists in the array, then the loop stops. The txt file is:

ABC FFF 1
DEF FFF 2
ABC FFF 2 <---after loading this, the program is stopped.
GTH FFF 2
FTR FFF 2
KOJ FFF 2
DEF FFF 2

How to make it keep going and ignore the duplicate name ?
CPP / C++ / C Code:
   
...


bool ArrayChecking( char * starting_port ){
	
	for ( int i = 1; i <= 101; i++ ){
		if ( strcmp( ports[i].IDName, starting_port ) == 0 ){
			return true;
		}
	}
}

What is the return value when the comparison fails? Didn't your compiler give any warnings? You should put
CPP / C++ / C Code:
return false;

after the loop.

However, I can't see how this would cause your program to hang up.

You are also not using feof() properly, but that couldn't cause the program to hang up either.

I would have to see more of your program to try to see what's happening.

Here's a question: Have you allocated storage for all of the struct elements?

Let's see the struct definition and initialization of ports.

Regards,

Dave
  #3  
Old 05-Oct-2004, 21:16
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Check the tutorial section for aaron's tutorial on I/O and look at the feof() entry. That will (hopefully) explain why you aren't using feof() properly.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Debugging problem.... Max C Programming Language 4 04-Aug-2004 04:59
Speed up C++ code about 3d array! Truong Son C++ Forum 0 16-Mar-2004 21:52
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 11:52
Problem with array norok MySQL / PHP Forum 3 16-Jul-2003 02:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.