|  | |  |
12-02-2003, 01:23 PM
|
#1 (permalink)
| | Registered User
Join Date: May 2003 Location: Paris, France
Posts: 31
| Wierd Problems.... I'm working on a project due next week (not tommorow for once!) and every time I run it the program crashes... I seriously cant figure out whats going on...
Below is the source Code: #include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
int GetCust();
int DisplayCustomers();
struct Customers
{int Num;
char Pkg;
float Hours;
char Bill[8];
};
Customers People[49];
int main()
{
GetCust();
DisplayCustomers();
system("PAUSE");
return 0;
};
int GetCust()
{
ifstream InFile;
InFile.open("C:\\CustomerFile.txt");
for (int i=0;!InFile.eof();i++)
{
InFile >> People[i].Num
>> People[i].Pkg
>> People[i].Hours
>> People[i].Bill[8];
}
InFile.close();
};
int DisplayCustomers()
{
for (int i=0;i<49;i++)
{
cout << People[i].Num;
cout << People[i].Pkg;
cout << People[i].Hours;
cout << People[i].Bill << endl;
};
} And here is the file its reading from Code: 101 A 7 07262003
102 a 10 07262003
103 a 11 07262003
104 a 12.5 07262003
105 a 13 07262003
106 a 15 07262003
107 a 18 07262003
108 a 20 07262003
109 a 22 07262003
110 a 25 07262003
111 a 30 07262003
201 B 7 07262003
202 b 10 07262003
203 b 11 07262003
204 b 12.5 07262003
205 b 13 07262003
206 b 15 07262003
207 b 18 07262003
208 b 20 07262003
209 b 22 07262003
210 b 25 07262003
211 b 30 07262003
301 C 7 07262003
302 c 10 07262003
303 c 11 07262003
304 c 12.5 07262003
305 c 13 07262003
306 c 15 07262003
307 c 18 07262003
308 c 20 07262003
309 c 22 07262003
310 c 25 07262003
311 c 30 07262003
404 d 32 07162003
405 c 32 07161989
406 c 32 07162021
407 c 32 7162021
408 c 32 11162O12
409 c 32 00312003
410 c 32 13312003
411 c 32 04002003
412 c 32 04312003
413 c 32 03322003
414 c 32 02292003
415 c 32 02302000
416 b 721 06302003
417 b 745 07312003
418 b 673 02282003
419 b 697 02292004 Any ideas? |
| |
12-03-2003, 06:19 AM
|
#2 (permalink)
| | Newbie
Join Date: Jun 2002 Location: Denmark
Posts: 1,726
| hmmm.. Quote:
struct Customers
...
Customers People[49];
| Shouldn't this be something like: Code: class Customers
...
Customers People[49]; Since it's C++, or Code: struct Customers
...
struct Customers People[49]; To keep it strict C, or Code: typedef struct {
...
} Customers;
Customers People[49]; To use the identifier you plan on using, as strict C. |
| |
12-03-2003, 11:17 AM
|
#3 (permalink)
| | LOAD "*",8,1
Join Date: Feb 2003 Location: la.ca.us
Posts: 254
| the struct declaration is not the problem here. it's not pretty, but his compiler seems to allow it.
the main errors are:
trying to read a string into a single char.
trying to store 9 chars in an 8 character array.
not returning an integer from functions declared as int.
you might want to try turning on all warnings on whatever compiler you're using. might save you alot of trouble. |
| |
12-03-2003, 08:41 PM
|
#4 (permalink)
| | Registered User
Join Date: May 2003 Location: Paris, France
Posts: 31
| well, nothing needs to be passed, as the structure is a global...
as for your first point of trying to read a string into a single char... its a character array, so that wouldnt be a problem would it? |
| |
12-03-2003, 09:12 PM
|
#5 (permalink)
| | [code][/code] enforcer
Join Date: Mar 2003 Location: Netherlands
Posts: 1,544
| As usual, many less seasoned programmers have problems with handling "characters".
But first things first.
You declared GetCust() and DisplayCustomers() int. But they aren't returning a value. So it should be as void declared.
Some crappy compilers don't give an error, but really, it is fundamentally wrong.
Now the big issue before releasing the code:
What happens if you do Code: "blahblah" >> People[i].Bill[8]; You will store the "b" in Bill[8]. That's it!
So this was correct: Code: char Bill[8];
"Blahblahblah" >> Bill;
cout << Bill; Well, ugly code but it shows the very basics. You were suspecting something like that huh?
But why making your life hard when there is the famous <string> header? Or <string.h> header.
Look at the code below and see how easy it could have been  . Code: #include <iostream>
#include <fstream>
#include <string>
using namespace std;
void GetCust();
void DisplayCustomers();
struct Customers
{
int Num;
char Pkg;
float Hours;
string Bill;
};
Customers People[49];
int main()
{
GetCust();
DisplayCustomers();
return 0;
};
void GetCust()
{
ifstream InFile;
InFile.open("E:\\Apps\\Microsoft Visual Studio\\MyProjects\\CustHelp\\CustomerFile.txt");
for (int i=0;InFile;i++)
{
InFile >> People[i].Num
>> People[i].Pkg
>> People[i].Hours
>> People[i].Bill;
}
InFile.close();
};
void DisplayCustomers()
{
for (int i=0;i<49;i++)
{
cout << People[i].Num << " ";
cout << People[i].Pkg << " ";
cout << People[i].Hours << " ";
cout << People[i].Bill << endl;
}
}; By the way, observe the << " " addition I entered. Makes reading much friendlier. But you would have found it if only your program could run for once.
Also look the way I did checking of the "end of file". Code: for (int i=0;InFile;i++) Sometimes even C++ can be simple. Good luck. Hope to see ya next time.
:rock:
__________________ |
| |
12-03-2003, 09:20 PM
|
#6 (permalink)
| | [code][/code] enforcer
Join Date: Mar 2003 Location: Netherlands
Posts: 1,544
| He dev.
The code tag reacts now to the italics tag.
So "SomeVar[i]" creates problems. I remember it didn't do so in the beginning.
__________________ |
| |
12-04-2003, 07:21 AM
|
#7 (permalink)
| | Registered User
Join Date: May 2003 Location: Paris, France
Posts: 31
| edit: ignore what was here, i got it working by increasing the array size from 8 to 9
ill probably be back with more problems soon as i have to add some more functions. |
| |
12-04-2003, 01:05 PM
|
#8 (permalink)
| | Registered User
Join Date: May 2003 Location: Paris, France
Posts: 31
| ok, heres another fun problem, i have completely forgotten how to do 2dimensional arrays...
what have i done wrong here? Code: int MonthHours[13][2]={{01,744},
{02,672},
{03,744},
{04,720},
{05,744},
{06,720},
{07,744},
{08,744},
{09,720},
{10,744},
{11,720},
{12,744}}; im pretty sure something about that is entirely wrong... but what is it?
here are the compile errors that come along with taht
numeric constant contains digits beyond the radix
numeric constant contains digits beyond the radix
these two errors relate to the lines with {08,744} and {09,720} |
| |
12-04-2003, 01:55 PM
|
#9 (permalink)
| | LOAD "*",8,1
Join Date: Feb 2003 Location: la.ca.us
Posts: 254
| 08 is not a valid octal number, and neither is 09.
octal values must be composed of digits between 0 and 7, inclusive. |
| |
12-04-2003, 01:56 PM
|
#10 (permalink)
| | Registered User
Join Date: May 2003 Location: Paris, France
Posts: 31
| Quote: Originally posted by joe_bruin 08 is not a valid octal number, and neither is 09.
octal values must be composed of digits between 0 and 7, inclusive. | is there any way i could use 08 and 09 in an array? i need them to stay there to indicate months.... |
| |
12-04-2003, 01:59 PM
|
#11 (permalink)
| | Registered User
Join Date: May 2003 Location: Paris, France
Posts: 31
| On a seperate note, I have trashed the array for the time being and am having a very annoying char data type problem.
I'm trying to put "Invalid Entry" into char Msg[15];
using this
{Valid[counter].Msg[15]="Invalid Record";
}
and i get this error
assignment to `char' from `const char *' lacks a cast
ive had this problem before, and had the responses to it saved but from some wonderful software issues i lost the file, so an encore explanation would be wonderful. |
| | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -8. The time now is 12:52 AM. |
Copyright © 2000-2008, Milano Interactive Web Hosting provided by Portal 360 Web Hosting |  | |