Pelican Parts Forums

Pelican Parts Forums (http://forums.pelicanparts.com/)
-   Off Topic Discussions (http://forums.pelicanparts.com/off-topic-discussions/)
-   -   Need some C++ help (http://forums.pelicanparts.com/off-topic-discussions/365791-need-some-c-help.html)

SlowToady 09-06-2007 05:41 PM

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

Rob Channell 09-06-2007 06:38 PM

Not sure, but I'm thinking check your array index limits at this point.

SlowToady 09-06-2007 07:18 PM

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

djmcmath 09-07-2007 03:51 AM

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. :)

SlowToady 09-07-2007 08:39 AM

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?

KFC911 09-07-2007 08:58 AM

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:)


All times are GMT -8. The time now is 04:33 AM.

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


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