Pelican Parts
Parts Catalog Accessories Catalog How To Articles Tech Forums
Call Pelican Parts at 888-280-7799
Shopping Cart Cart | Project List | Order Status | Help



Go Back   Pelican Parts Forums > Miscellaneous and Off Topic Forums > Off Topic Discussions


Reply
 
LinkBack Thread Tools Rate Thread
Author
Thread Post New Thread    Reply
Registered
 
Join Date: Dec 2002
Location: www.fakelife.com
Posts: 1,672
Send a message via AIM to SlowToady
Need some C++ help

Hey guys...writing a program (or, trying to) for my CS class...I have it about 99% done, but I can't get one nagging detail right.

What I need to do is read data from a file into parallel arrays, which are declared as:
Code:
string Brand[MAX_COMPUTER];
short RAM[MAX_COMPUTER];
where MAX_COMPUTER is a global constant. Ok. Now, the file can have any number of entries in it, including 0 to >MAX_COMPUTERS. I need to read up to and including the first MAX_COMPUTERS entries (each entry is on a new line) and nothing past that. However, I still need to accumulate a counter for entries > MAX_COMPUTER.

I tried something like:
Code:
{
short count = 0;
//open file
//check for proper opening
//file input stream is fin
fin >> Brand[count] >> RAM[count]
while (fin)
{
     count++;
     if (count <= MAX_COMPUTERS)
     {
           fin >> Brand[count] >> RAM[count];
      }
}
fin.close();
return count;
}
Works fine for a file up to MAX_COMPUTERS, but throws Memory Access Violations for entries > MAX_COMPUTERS.

Any ideas? So close to done...

Thanks in advance.

PS: If anyone wants to whole code, just to see what I'm doing, I'll put it online.

Also, if you need to understand the problem more, or I don't explain it clearly enough, you can read about it at http://www.cs.uky.edu/~ryan/CS215_F07/programs/1pgm.html

__________________
I turn away with fear and horror from this lamentable sore of continuous functions without derivatives. --Charles Hermite

Fakelife.com Nothing to do with archery anymore. Porsche/BMW/Ferrari/Honda videos
Old 09-06-2007, 05:41 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #1 (permalink)
Registered
 
Rob Channell's Avatar
 
Join Date: Feb 2004
Location: Decatur/Madison, Alabama
Posts: 1,192
Not sure, but I'm thinking check your array index limits at this point.
__________________
Rob Channell
One Way Motorsports
1979 911SC mostly stock
1972 911T Targa now with a good 2.7
1990 Miata (cheap 'n easy)
1993 C1500 Silverado (parts getter)
Old 09-06-2007, 06:38 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #2 (permalink)
Registered
 
Join Date: Dec 2002
Location: www.fakelife.com
Posts: 1,672
Send a message via AIM to SlowToady
Got it figured out. What was happening with the original loop is that the variable count, declared as a short, would keep counting after reading the 10 entries from the file because, since it isn't entering the IF loops, it's not actually _reading_ from the file, so it continues forever. Actually, it continues until "count" reaches 32767, which is the upper limit of a short, then it rolls over to some negative value (-1 I think) and crashes the program with Memory Access Violations. Am now using "unsigned short count = 0" since the value of count since the value shouldn't ever go negative. Also, have the loop fixed. It is now:

Code:
unsigned short count = 0;
fin.open(filename.c_str());
//file checking is done here, no reason to show it

//priming read
fin >> Brand[count] >> RAM[count];

while (fin)
{
     count++;
     if (count < MAX_COMPUTER)
     {
           fin >> Brand[count] >> RAM[count];
     }
     else 
     {
           break;
     }
}
//close files, cleanup
return count;
}
Much thanks to my Brother in Law for pointing out that I'm a dolt:-D
__________________
I turn away with fear and horror from this lamentable sore of continuous functions without derivatives. --Charles Hermite

Fakelife.com Nothing to do with archery anymore. Porsche/BMW/Ferrari/Honda videos
Old 09-06-2007, 07:18 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #3 (permalink)
Registered
 
djmcmath's Avatar
 
Join Date: Nov 2003
Location: West of Seattle
Posts: 4,718
I was just going to point out the logical error with your bounds check, which you've fixed. Not knowing how big MAX_COMPUTER was, I don't think I would have caught the other problem.

Ah, I do recall the good old days. Now I code in Ruby, where I don't have to worry about variable declarations.
__________________
'86 911 (RIP March '05)
'17 Subaru CrossTrek
'99 911 (Adopt an unloved 996 from your local shelter today!)
Old 09-07-2007, 03:51 AM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #4 (permalink)
Registered
 
Join Date: Dec 2002
Location: www.fakelife.com
Posts: 1,672
Send a message via AIM to SlowToady
Um, anyone know how to control a Bubble Sort algorithm with a bool flag?

Code:
if (choice == 1)
	{
		//same comments from sort_array apply here
		for (unsigned short x = 0; x <= computer_count; x++)
		{
			for (unsigned short j = 0; j < computer_count - 1; j++)
			{
				if (computer[j].Brand > computer[j+1].Brand)
				{
					//This method of swapping is MUCH nice than using multiple arrays
					compdef temp; //create temporary struct
					temp = computer[j+1]; //copy entire struct
					computer[j+1] = computer[j];
					computer[j] = temp;
					//swap values around
				}
			}
		}
So that code like that would exit when there are no more swaps, instead of brute force going through all the iterations of the outer FOR loop?
__________________
I turn away with fear and horror from this lamentable sore of continuous functions without derivatives. --Charles Hermite

Fakelife.com Nothing to do with archery anymore. Porsche/BMW/Ferrari/Honda videos
Old 09-07-2007, 08:39 AM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #5 (permalink)
?
 
Join Date: Apr 2002
Posts: 30,443
Oh god, I'm going back decades (before C, much less ++ ), so I can't offer specifics. In general, if you can't use an expression such as "Do while BooleanFlag = False", or something similar, you can simply set up an outer "If" loop:

Swapped = False
If Swapped = False
....your existing If loop logic...
Swapped = True (when the swap is made...this should terminate the outer loop)

Sorry I don't know C++, and my programming skills are rusty, but maybe this will get you thinking in the right direction...

edited: sorry, I lose my indentations on the looping logic...hope it's still clear as mud


Last edited by KFC911; 09-07-2007 at 09:01 AM..
Old 09-07-2007, 08:58 AM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #6 (permalink)
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

 


All times are GMT -8. The time now is 03:42 PM.


 
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0
Copyright 2025 Pelican Parts, LLC - Posts may be archived for display on the Pelican Parts Website -    DMCA Registered Agent Contact Page
 

DTO Garage Plus vBulletin Plugins by Drive Thru Online, Inc.