ok.. i have a program that's set up like this..
#include <iostream.h>
#include <lvp/string.h>
int main()
{
String name[2];
}
now, this isnt may whole program. When i try to raise the ##2 in the array i get an error from my compiler that there is a stacker error and the program freezes. Can anyone tell me how i can fix this. my problem might be somewhere in my program so please ask if you want 2 see the whole thing.
Thanks, onorinbejasus
12 1674
Im not sure what you are trying to do but if you do String array[2] and then do array[2] = "" you are going to get an error because nothing exists at array[2]. Arrays that are of length two have two indexes one at array[0] and one at array[1].
now, this isnt may whole program. When i try to raise the ##2 in the array i get an error from my compiler that there is a stacker error and the program freezes. Can anyone tell me how i can fix this. my problem might be somewhere in my program so please ask if you want 2 see the whole thing.
If what RedSon has told you doesn't ring any bells, then yeah, let’s see the whole thing. There is not enough information to do any sort of analysis.
Oh and clear up some semantics, your compiler isn't generating the stack error message, your programme is. Though not explicitly stated in your code, it is implicit code put in by the compiler to tell you that there is a bug in your programme.
Adrian
im reading data from a into these arrays, note that its only the strings that are giving me problems. this program works 100% until i raise the number in the string variable array... heres the code - #include <iostream.h>
-
#include <iomanip.h>
-
#include <conio.h>
-
#include <fstream.h>
-
#include <lvp\string.h>
-
#include <stdlib.h>
-
#include <stdio.h>
-
-
ifstream* openFile()
-
{
-
-
ifstream* pFileStream = 0;
-
for(;;)
-
{
-
// open the file specified by the user
-
char fileName[128]; // file name
-
gotoxy(25,10);
-
cout << "Enter the name of the file: ";
-
cin >> fileName;
-
-
// opens file for reading
-
-
pFileStream = new ifstream(fileName);
-
if (pFileStream->good())
-
{
-
break;
-
}
-
gotoxy(20, 11);
-
cerr << "Couldn't open. Reenter File Name: " << fileName << endl;
-
delete pFileStream;
-
}
-
return pFileStream;
-
}
-
-
int main ()
-
-
{
-
-
-
cout.setf(ios::fixed, ios:: floatfield);
-
cout.setf(ios::showpoint);
-
cout << setprecision(2);
-
-
// variable names
-
int selection = 0; // this variable is used for the intial process selection
-
-
// chars }
-
char num[8]; // array for the customer number
-
char phrs[6]; // array for the previous month's hours
-
char powed[10]; // array for what they owed the previous month saved as a char
-
char pd[10]; // amount paid the precious month saved as a char
-
char hr[6]; // hours used this month saved as a char
-
// }
-
-
-
long custnum[128];
-
long prevhrs[128];
-
float prevowed[128];
-
long hrs[128];
-
float owed[128]; // what is owed this month
-
float paid[128];
-
-
String custfname[2];// customer first name
-
String custlname[2]; // customer last name
-
String meter[2]; // meter #
-
-
char answer; // answer to the question to input new data
-
int count = 0; // will count the data as its being read
-
long total; // practice reading
-
long accumulated = 0; // the amount accumulated byt the customers
-
double initialr = .11; // initial usuage rate
-
double overr = .08; // rate over 200 hrs
-
char proceed; // stops program until user presses enter
-
float overage[128]; // what is still owed/overpaid from last month
-
float over; // used 2 calculate what is owed over 200
-
long find; // used to find a specific customer
-
char * pEnd;
-
-
do{
-
-
// this will get the data
-
-
ifstream* pFileStream = openFile();
-
-
while (!pFileStream->eof())
-
{
-
// read value
-
-
(*pFileStream) >> num >> custlname[count] >> custfname[count] >> meter[count]
-
>> phrs >> powed >> pd >> hr;
-
-
// this will convert those strings that need 2 be numbers
-
-
custnum[count] = strtol(num,&pEnd,10);
-
prevhrs[count] = strtol(phrs,&pEnd,10);
-
prevowed[count] = strtod(powed,&pEnd);
-
hrs[count] = strtol(hr, &pEnd, 10);
-
paid[count] = strtod(pd, &pEnd);
-
-
if (!pFileStream->eof())
-
{
-
count ++;
-
}
-
else if (pFileStream->eof())
-
{
-
break;
-
}
-
}
-
-
// this will make the selection screen
-
-
-
// will calculate what the customer owes this month before the program is run
-
-
for (int x = 0; x <= count; x++)
-
{
-
if (hrs[x] <= 200 && hrs[x] > 0)
-
owed[x] = hrs[x] * initialr;
-
else if (hrs[x] > 200)
-
{
-
over = hrs[x] - 200;
-
owed[x] = (200 * initialr) + (over * overr);
-
}
-
else if (hrs <=0)
-
owed[x] = 0;
-
-
overage[x] = prevowed[x] - paid[x];
-
owed[x] = owed[x] + overage[x];
-
}
-
clrscr();
-
do {
-
cout << " \t Welcome to the E-Z Watt Electric Company Monthly Bill System";
-
cout << endl << endl << endl << "\t \t \t Please choose a selectoin(1-4):";
-
cout << endl << endl << " \t \t \t 1. View last Month's records";
-
cout << endl << " \t \t \t 2. View the current Month's records";
-
cout << endl << " \t \t \t 3. Bill for a specific customer";
-
cout << endl << " \t \t \t 4. End(or enter new data)";
-
cout << endl << endl << "\t \t \t : ";
-
cin >> selection;
-
-
// error trap for wrong selection
-
-
while (selection < 1 || selection > 4)
-
{
-
cin.clear();
-
cin.ignore(100,'\n');
-
cout << "\t \t \t Invalid Selection" << endl << "\t \t \t Choose a selectin(1-4): ";
-
cin >> selection;
-
-
}
-
-
switch (selection)
-
{
-
-
case 1:
-
clrscr();
-
{
-
-
for (int x = 0; x <= count; x++)
-
{
-
cout.setf(ios::left);
-
cout << "Customer Number: " << custnum[x] << endl;
-
cout << "Customer Name: " << custlname[x] << custfname[x] << endl;
-
cout << "Meter ##: " << meter[x] << endl;
-
cout << "Previous Hours: " << prevhrs[x] << endl;
-
cout << "Amount Owed: " << prevowed[x] << endl;
-
cout << "Amount Paid: " << paid[x] << endl << endl;
-
cout.unsetf(ios::left);
-
-
// adds up previous hrs used
-
if (prevhrs > 0)
-
accumulated = accumulated + prevhrs[x];
-
else
-
accumulated = accumulated + 0;
-
// compares accumulated hrs to total
-
}
-
cout << "Total kilowatts used last month: " << accumulated;
-
cout << endl << endl;
-
cout << " \t \t Press any key(but space) then Enter to Proceed: ";
-
cin >> proceed;
-
clrscr();
-
break;
-
}
-
case 2:
-
{
-
-
clrscr();
-
gotoxy(25,0);
-
total = hrs[0];
-
cout << "Total kilowatt usage this month: " << total; // remember to set total 2 header reading
-
cout << endl << endl;
-
-
for (int x = 0; x <= count; x++)
-
{
-
cout.setf(ios::left);
-
cout << "Customer Number: " << custnum[x] << endl;
-
cout << "Customer Name: " << custlname[x] << custfname[x] << endl;
-
cout << "Meter ##: " << meter[x] << endl;
-
cout << "Hours: " << hrs[x] << endl;
-
cout << "Over/Under Pay Prev. Month: " << overage[x] << endl;
-
cout << "Amount Owed: " << owed[x] << endl << endl;
-
cout.unsetf(ios::left);
-
-
// adds up previous hrs used
-
if (hrs[x] > 0)
-
accumulated = accumulated + hrs[x];
-
else
-
accumulated = accumulated + 0;
-
-
// compares accumulated hrs to total
-
}
-
if (total == accumulated)
-
cout << endl<< endl << "Records are in order";
-
else if (total < accumulated)
-
{
-
cout << endl << endl << "More kilowatts were used than were recoreded this month: ";
-
long more = accumulated - total;
-
cout << more;
-
cout << endl << "Check records";
-
}
-
else if (total > accumulated)
-
{
-
cout << endl << endl << "Fewer kilowatts were used than were recoreded this month: ";
-
long less = total + accumulated;
-
cout << less;
-
cout << endl << "Check records";
-
}
-
-
-
cout << endl << endl;
-
cout << " \t \t Press any key(but space) then Enter to Proceed: ";
-
cin >> proceed;
-
clrscr();
-
break;
-
}
-
-
-
case 3:
-
{
-
-
-
do {
-
clrscr();
-
gotoxy(20,10);
-
cout << "Enter the customer number: ";
-
gotoxy(15,11);
-
cout << "Drop all 0's (123 instead of 0123): ";
-
cin >> find;
-
int flag = 0; // used to determine if person is in data
-
-
// searches for person
-
-
for (int x = 0; x <= count; x++)
-
{
-
-
-
if (find == custnum[x])
-
{
-
clrscr();
-
cout.setf(ios::left);
-
cout << "Customer Number: " << custnum[x] << endl;
-
cout << "Customer Name: " << custlname[x] << custfname[x] << endl;
-
cout << "Meter ##: " << meter[x] << endl;
-
cout << "Prev Hours: " << prevhrs[x] << endl;
-
cout << "Amount Owed: " << prevowed[x] << endl;
-
cout << "Amount Paid: " << paid[x] << endl;
-
cout << "Over/Under Pay Prev. Month: " << overage[x] << endl;
-
cout << "Hours This Month: " << hrs[x] << endl;
-
cout << "Amount Owed: " << owed[x] << endl << endl;
-
cout.unsetf(ios::left);
-
flag ++;
-
break;
-
}
-
}
-
-
cin.clear();
-
cin.ignore(100,'\n');
-
-
// message if person is not in data
-
-
if (flag == 0)
-
{
-
clrscr();
-
gotoxy(20,10);
-
cout << "Customer cannot be found";
-
gotoxy(10, 11);
-
}
-
else
-
{
-
cout << endl << endl;
-
}
-
-
cout << "Would you like to search again? (Y for Yes. N for No): ";
-
cin >> answer;
-
-
while ((answer != 'y' && answer != 'Y') && (answer != 'n' && answer != 'N'))
-
{
-
cin.ignore(100,'\n');
-
gotoxy(30,10);
-
cout << "Invalid Selection";
-
gotoxy(30,11);
-
cout << "(Y for Yes. N for No): ";
-
cin >> answer;
-
clrscr();
-
}
-
}
-
-
while (answer == 'y' || answer == 'Y');
-
-
cout << endl << endl;
-
cout << " \t \t Press any key(but space) then Enter to Proceed: ";
-
cin >> proceed;
-
clrscr();
-
break;
-
}
-
-
case 4:
-
{
-
clrscr();
-
gotoxy(10,10);
-
cout << "Would you like to enter a new file? (Y for Yes. N for No): ";
-
cin >> answer;
-
clrscr();
-
-
while ((answer != 'y' && answer != 'Y') && (answer != 'n' && answer != 'N'))
-
{
-
cin.clear();
-
cin.ignore(100,'\n');
-
gotoxy(30,10);
-
cout << "Invalid Selection";
-
gotoxy(30,11);
-
cout << "(Y for Yes. N for No): ";
-
cin >> answer;
-
clrscr();
-
}
-
}
-
}
-
-
} while (selection !=4);
-
} while (answer == 'y' || answer == 'Y');
-
-
clrscr();
-
-
gotoxy(35,10);
-
-
cout << "Have a good day";
-
-
return 0;
-
}
Can someone add some code tags to this please?
i kno the code is messy, but i only just finished my first semester of C++ in high school (mostly self taught). here is what occurs, I read multiple lines of data like this and it gets saved into array. Everything is fine, it works great with two lines of data. but when i want to increase the amount of data i want to put in it, i have to increase the array number. here is an example of a line of data im reading:
78453456 Kleiner, Georgia 12IRNSNCIE14 124 13.64 15.00 174
every thing reads fine, but like i said when i go to increase "String name[2];" to lets say like 10 i get the error
Its not that your code is particularly messy versus any other code that is posted on thescripts, its just that you didnt put code tags around your code. The code tags preserve whitespace and make it easier to read by not inserting linebreaks.
i kno the code is messy, but i only just finished my first semester of C++ in high school (mostly self taught). here is what occurs, I read multiple lines of data like this and it gets saved into array. Everything is fine, it works great with two lines of data. but when i want to increase the amount of data i want to put in it, i have to increase the array number. here is an example of a line of data im reading:
78453456 Kleiner, Georgia 12IRNSNCIE14 124 13.64 15.00 174
every thing reads fine, but like i said when i go to increase "String name[2];" to lets say like 10 i get the error
Ok, I took your code and reindented it. Man, this yonger generation. ;) jk lol.
I would seriously consider breaking this down into smaller functions. I’m really against switch statements that are larger than a normal viewing window can hold. Really easy to make errors. Your switch satement of 4 cases shouldn’t be longer than 14 lines. If you are doing heavy optimisation, maybe double or triple that. Yours is about 175 lines. It truly a bit much.
Ok, I didn't read the entire code, but I'm assuming that you are referring to these: -
String custfname[2];// customer first name
-
String custlname[2]; // customer last name
-
String meter[2]; // meter #
-
If you have parallel arrays, you better make sure that they all increase uniformly at the same time. Use a constant and change the constant's value when you want to increase the number of elements. Otherwise, you can find that you can put stuff into one of them but then get creamed when putting other stuff in the others.
An even better method than parallel arrays is to use a structure to keep these linked together more rigouresly.
Hope this helps.
Adrian
hmmm... well, my arrays are controlled by a count while its being read, so they are always being read like u said. i just needed to set an arbitrary number cause i never know how many lines of data i will be reading. thats where my problem is. when i raise it i get the error and the program freezes.
hmmm... well, my arrays are controlled by a count while its being read, so they are always being read like u said. i just needed to set an arbitrary numer cause i never know how many lines of data i will be reading. thats where my problem is. when i raise it i get the error and the program freezes.
Ah, well laddie, then you be needin' to be using a template container class. (Sorry, felt Scottish there for a sec. I think it has passed now. :))
My recommendation is to join all these related elements into a struct. Then use a template class from the STL (standard template library). If you are making millions of records, and don't need to randomly access them, use the list<> template. If you are making a hundreds of them and are accessing them randomly, use vector<>.
See http://www.sgi.com/tech/stl/table_of_contents.html] for more on the STL.
Hope this helps.
Adrian
I have been having this problem for days on my program and the vector finally fixed it!!!!
THANKS SO MUCH!!!!
I have been having this problem for days on my program and the vector finally fixed it!!!!
THANKS SO MUCH!!!!
Glad to help.
Adrian
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Michiel |
last post by:
Hello,
I am not sure this is the right place to ask. I tried the ZSI mailing
list, but got no response there.
My goal is to write some web services with ZSI to be used by a client
written in...
|
by: Arne |
last post by:
From: "Arne de Booij" <a_de_booij@hotmail.com>
Subject: Comma delimited array into DB problems
Date: 9. februar 2004 10:39
Hi,
I have an asp page that takes input from a form on the previous...
|
by: Stingray |
last post by:
Are there any know problems with using a MemoryStream as a backing store for
a CryptoStream? I'm trying to simply encrypt and decrypt text in memory.
I'd like to create some simple methods to...
|
by: crawlerxp |
last post by:
This is the problem: I do not get the output I need when encoding and
decoding data using rijndael alghoritm.
Look at the code and see what the problem is actually:
Please paste this code into...
|
by: Wayne Wengert |
last post by:
I am still stuck trying to create a Class to use for exporting and importing
array data to/from XML. The format of the XML that I want to import/export
is shown below as is the Class and the code I...
|
by: robbie.carlton |
last post by:
Hello!
I've programmed in c a bit, but nothing very complicated. I've just
come back to it after a long sojourn in the lands of functional
programming and am completely stumped on a very simple...
|
by: bob garbados |
last post by:
I'm new to web services and I'm trying to interface with a payment gateway
for an online store. I'm trying to do this without Visual Studio and I'm
stuck...
I created my proxy class from the...
|
by: BeruthialsCat |
last post by:
First go with trying to import xml to a database and whilst i have
managed to do what i want i find that the xml files we have here at
work are gonna cause me problems. I have a function that...
|
by: Felix85 |
last post by:
53 void room2File(room r){
54 ofstream outfile("../gamefiles/rooms/" +
r.getRoomNumber() + ".room");
55
56 outfile << r.getRoomNumber() << "\n";
57 outfile <<...
|
by: Mike |
last post by:
Hi,
I am new to C and having problems with the following program.
Basically I am trying to read some files, loading data structures into
memory for latter searching.
I am trying to use structres...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |