![]() |
|
|
|
Registered
|
Need some C++ help
Hey guys...writing a program (or, trying to
![]() 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]; 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; } 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 |
||
![]() |
|
Registered
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) |
||
![]() |
|
Registered
|
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; }
__________________
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 |
||
![]() |
|
Registered
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!) |
||
![]() |
|
Registered
|
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 } } }
__________________
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 |
||
![]() |
|
?
Join Date: Apr 2002
Posts: 30,443
|
Oh god, I'm going back decades (before C, much less ++
![]() 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.. |
||
![]() |
|
![]() |