473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problems with my string array

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
Feb 13 '07 #1
12 1674
RedSon
5,000 Expert 4TB
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].
Feb 13 '07 #2
AdrianH
1,251 Expert 1GB
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
Feb 13 '07 #3
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <iomanip.h>
  3. #include <conio.h>
  4. #include <fstream.h>
  5. #include <lvp\string.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8.  
  9. ifstream* openFile()
  10. {
  11.  
  12. ifstream* pFileStream = 0;
  13.     for(;;)
  14.     {
  15.     // open the file specified by the user
  16.     char fileName[128]; // file name
  17.     gotoxy(25,10);
  18.     cout << "Enter the name of the file: ";
  19.     cin >> fileName;
  20.  
  21.     // opens file for reading
  22.  
  23.     pFileStream = new ifstream(fileName);
  24.     if (pFileStream->good())
  25.     {
  26.     break;
  27.     }
  28.     gotoxy(20, 11);
  29.     cerr << "Couldn't open. Reenter File Name: " << fileName << endl;
  30.     delete pFileStream;
  31.     }
  32.     return pFileStream;
  33.     }
  34.  
  35. int main ()
  36.  
  37. {
  38.  
  39.  
  40. cout.setf(ios::fixed, ios:: floatfield);
  41. cout.setf(ios::showpoint);
  42. cout << setprecision(2);
  43.  
  44. // variable names
  45. int selection = 0; // this variable is used for the intial process selection
  46.  
  47. // chars }
  48. char num[8]; //  array for the customer number
  49. char phrs[6]; // array for the previous month's hours
  50. char powed[10]; // array for what they owed the previous month saved as a char
  51. char pd[10]; // amount paid the precious month saved as a char
  52. char hr[6]; // hours used this month saved as a char
  53. // }
  54.  
  55.  
  56. long custnum[128];
  57. long prevhrs[128];
  58. float prevowed[128];
  59. long hrs[128];
  60. float owed[128]; // what is owed this month
  61. float paid[128];
  62.  
  63. String custfname[2];// customer first name
  64. String custlname[2]; // customer last name
  65. String meter[2]; // meter #
  66.  
  67. char answer; // answer to the question to input new data
  68. int count = 0; // will count the data as its being read
  69. long total; // practice reading
  70. long accumulated = 0; // the amount accumulated byt the customers
  71. double initialr = .11; // initial usuage rate
  72. double overr = .08; // rate over 200 hrs
  73. char proceed; // stops program until user presses enter
  74. float overage[128]; // what is still owed/overpaid from last month
  75. float over; // used 2 calculate what is owed over 200
  76. long find; // used to find a specific customer
  77. char * pEnd;
  78.  
  79. do{
  80.  
  81. // this will get the data
  82.  
  83. ifstream* pFileStream = openFile();
  84.  
  85.     while (!pFileStream->eof())
  86.     {
  87.     // read value
  88.  
  89.     (*pFileStream) >> num >> custlname[count] >> custfname[count] >> meter[count]
  90.     >> phrs >> powed >> pd >> hr;
  91.  
  92.     // this will convert those strings that need 2 be numbers
  93.  
  94.     custnum[count] = strtol(num,&pEnd,10);
  95.     prevhrs[count] = strtol(phrs,&pEnd,10);
  96.     prevowed[count] = strtod(powed,&pEnd);
  97.     hrs[count] = strtol(hr, &pEnd, 10);
  98.     paid[count] = strtod(pd, &pEnd);
  99.  
  100.     if (!pFileStream->eof())
  101.     {
  102.     count ++;
  103.     }
  104.     else if (pFileStream->eof())
  105.     {
  106.     break;
  107.     }
  108.     }
  109.  
  110. // this will make the selection screen
  111.  
  112.  
  113. // will calculate what the customer owes this month before the program is run
  114.  
  115. for (int x = 0; x <= count; x++)
  116.     {
  117.     if (hrs[x] <= 200 && hrs[x] > 0)
  118.         owed[x] = hrs[x] * initialr;
  119.     else if (hrs[x] > 200)
  120.         {
  121.         over = hrs[x] - 200;
  122.         owed[x] = (200 * initialr) + (over * overr);
  123.         }
  124.     else if (hrs <=0)
  125.         owed[x] = 0;
  126.  
  127.         overage[x] = prevowed[x] - paid[x];
  128.         owed[x] = owed[x] + overage[x];
  129.     }
  130. clrscr();
  131. do {
  132. cout  << " \t Welcome to the E-Z Watt Electric Company Monthly Bill System";
  133. cout << endl << endl << endl << "\t \t \t Please choose a selectoin(1-4):";
  134. cout << endl << endl << " \t \t \t 1. View last Month's records";
  135. cout << endl << " \t \t \t 2. View the current Month's records";
  136. cout << endl << " \t \t \t 3. Bill for a specific customer";
  137. cout << endl << " \t \t \t 4. End(or enter new data)";
  138. cout << endl << endl << "\t \t \t : ";
  139. cin >> selection;
  140.  
  141. // error trap for wrong selection
  142.  
  143. while (selection < 1 || selection > 4)
  144.      {
  145.      cin.clear();
  146.      cin.ignore(100,'\n');
  147.      cout << "\t \t \t Invalid Selection" << endl << "\t \t \t Choose a selectin(1-4): ";
  148.      cin >> selection;
  149.  
  150.      }
  151.  
  152. switch (selection)
  153.      {
  154.  
  155.               case 1:
  156.               clrscr();
  157.               {
  158.  
  159.               for (int x = 0; x <= count; x++)
  160.                      {
  161.                       cout.setf(ios::left);
  162.                       cout << "Customer Number: "  << custnum[x] << endl;
  163.                       cout << "Customer Name:   "  << custlname[x] << custfname[x] << endl;
  164.                       cout << "Meter ##:        "  << meter[x] << endl;
  165.                       cout << "Previous Hours:  "  << prevhrs[x] << endl;
  166.                       cout << "Amount Owed:     "  << prevowed[x] << endl;
  167.                       cout << "Amount Paid:     "  << paid[x] << endl << endl;
  168.                       cout.unsetf(ios::left);
  169.  
  170. // adds up previous hrs used
  171.                       if (prevhrs > 0)
  172.                           accumulated = accumulated + prevhrs[x];
  173.                       else
  174.                           accumulated = accumulated + 0;
  175. // compares accumulated hrs to total
  176.                             }
  177.                 cout << "Total kilowatts used last month: " << accumulated;
  178.                 cout << endl << endl;
  179.                 cout << " \t \t Press any key(but space) then Enter to Proceed: ";
  180.                 cin >> proceed;
  181.                 clrscr();
  182.                 break;
  183.                 }
  184.               case 2:
  185.               {
  186.  
  187.               clrscr();
  188.               gotoxy(25,0);
  189.               total = hrs[0];
  190.               cout << "Total kilowatt usage this month: " << total; // remember to set total 2 header reading
  191.               cout << endl << endl;
  192.  
  193.               for (int x = 0; x <= count; x++)
  194.                      {
  195.                       cout.setf(ios::left);
  196.                       cout << "Customer Number:            "  << custnum[x] << endl;
  197.                       cout << "Customer Name:              "  << custlname[x] << custfname[x] << endl;
  198.                       cout << "Meter ##:                   "  << meter[x] << endl;
  199.                       cout << "Hours:                      "  << hrs[x] << endl;
  200.                       cout << "Over/Under Pay Prev. Month: " << overage[x] << endl;
  201.                       cout << "Amount Owed:                "  << owed[x] << endl << endl;
  202.                       cout.unsetf(ios::left);
  203.  
  204. // adds up previous hrs used
  205.                       if (hrs[x] > 0)
  206.                           accumulated = accumulated + hrs[x];
  207.                       else
  208.                           accumulated = accumulated + 0;
  209.  
  210. // compares accumulated hrs to total
  211.                       }
  212.                       if (total == accumulated)
  213.                             cout << endl<< endl << "Records are in order";
  214.                       else if (total < accumulated)
  215.                             {
  216.                             cout << endl << endl << "More kilowatts were used than were recoreded this month: ";
  217.                             long more = accumulated - total;
  218.                             cout << more;
  219.                             cout << endl << "Check records";
  220.                             }
  221.                       else if (total > accumulated)
  222.                             {
  223.                             cout << endl << endl << "Fewer kilowatts were used than were recoreded this month: ";
  224.                             long less = total + accumulated;
  225.                             cout << less;
  226.                             cout << endl << "Check records";
  227.                             }
  228.  
  229.  
  230.                 cout << endl << endl;
  231.                 cout << " \t \t Press any key(but space) then Enter to Proceed: ";
  232.                 cin >> proceed;
  233.                 clrscr();
  234.                 break;
  235.                 }
  236.  
  237.  
  238.               case 3:
  239.               {
  240.  
  241.  
  242.               do {
  243.               clrscr();
  244.               gotoxy(20,10);
  245.               cout << "Enter the customer number: ";
  246.               gotoxy(15,11);
  247.               cout << "Drop all 0's (123 instead of 0123): ";
  248.               cin >> find;
  249.               int flag = 0; // used to determine if person is in data
  250.  
  251.               // searches for person
  252.  
  253.               for (int x = 0; x <= count; x++)
  254.                   {
  255.  
  256.  
  257.                   if (find == custnum[x])
  258.                       {
  259.                       clrscr();
  260.                       cout.setf(ios::left);
  261.                       cout << "Customer Number:            "  << custnum[x] << endl;
  262.                       cout << "Customer Name:              "  << custlname[x] << custfname[x] << endl;
  263.                       cout << "Meter ##:                   "  << meter[x] << endl;
  264.                       cout << "Prev Hours:                 "  << prevhrs[x] << endl;
  265.                       cout << "Amount Owed:                "  << prevowed[x] << endl;
  266.                       cout << "Amount Paid:                "  << paid[x] << endl;
  267.                       cout << "Over/Under Pay Prev. Month: " << overage[x] << endl;
  268.                       cout << "Hours This Month:           "  << hrs[x] << endl;
  269.                       cout << "Amount Owed:                "  << owed[x] << endl << endl;
  270.                       cout.unsetf(ios::left);
  271.                       flag ++;
  272.                       break;
  273.                       }
  274.                       }
  275.  
  276.                       cin.clear();
  277.                       cin.ignore(100,'\n');
  278.  
  279.               // message if person is not in data
  280.  
  281.               if (flag == 0)
  282.               {
  283.                   clrscr();
  284.                   gotoxy(20,10);
  285.                   cout << "Customer cannot be found";
  286.                   gotoxy(10, 11);
  287.                  }
  288.               else
  289.                   {
  290.                   cout << endl << endl;
  291.                   }
  292.  
  293.                   cout << "Would you like to search again? (Y for Yes. N for No): ";
  294.                   cin >> answer;
  295.  
  296.               while ((answer != 'y' && answer != 'Y') && (answer != 'n' && answer != 'N'))
  297.               {
  298.               cin.ignore(100,'\n');
  299.               gotoxy(30,10);
  300.               cout << "Invalid Selection";
  301.               gotoxy(30,11);
  302.               cout << "(Y for Yes. N for No): ";
  303.               cin >> answer;
  304.               clrscr();
  305.               }
  306.               }
  307.  
  308.              while (answer == 'y' || answer == 'Y');
  309.  
  310.                 cout << endl << endl;
  311.                 cout << " \t \t Press any key(but space) then Enter to Proceed: ";
  312.                 cin >> proceed;
  313.                 clrscr();
  314.                 break;
  315.                 }
  316.  
  317.               case 4:
  318.               {
  319.               clrscr();
  320.               gotoxy(10,10);
  321.               cout << "Would you like to enter a new file? (Y for Yes. N for No): ";
  322.               cin >> answer;
  323.               clrscr();
  324.  
  325.              while ((answer != 'y' && answer != 'Y') && (answer != 'n' && answer != 'N'))
  326.              {
  327.              cin.clear();
  328.              cin.ignore(100,'\n');
  329.              gotoxy(30,10);
  330.              cout << "Invalid Selection";
  331.              gotoxy(30,11);
  332.              cout << "(Y for Yes. N for No): ";
  333.              cin >> answer;
  334.              clrscr();
  335.              }
  336.      }
  337. }
  338.  
  339. } while (selection !=4);
  340. } while (answer == 'y' || answer == 'Y');
  341.  
  342. clrscr();
  343.  
  344. gotoxy(35,10);
  345.  
  346. cout << "Have a good day";
  347.  
  348. return 0;
  349. }
Feb 13 '07 #4
RedSon
5,000 Expert 4TB
Can someone add some code tags to this please?
Feb 13 '07 #5
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
Feb 13 '07 #6
RedSon
5,000 Expert 4TB
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.
Feb 13 '07 #7
Ganon11
3,652 Expert 2GB
Code tags added.
Feb 13 '07 #8
AdrianH
1,251 Expert 1GB
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:
Expand|Select|Wrap|Line Numbers
  1.     String custfname[2];// customer first name
  2.     String custlname[2]; // customer last name
  3.     String meter[2]; // meter #
  4.  
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
Feb 13 '07 #9
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.
Feb 13 '07 #10
AdrianH
1,251 Expert 1GB
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
Feb 13 '07 #11
I have been having this problem for days on my program and the vector finally fixed it!!!!

THANKS SO MUCH!!!!
Feb 13 '07 #12
AdrianH
1,251 Expert 1GB
I have been having this problem for days on my program and the vector finally fixed it!!!!

THANKS SO MUCH!!!!
Glad to help.


Adrian
Feb 13 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
4
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...
7
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...
0
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...
4
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...
9
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...
4
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...
5
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...
9
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 <<...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.