![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Final project has begun! So have the errors!
Alright, so I'm finally getting this event simulation of a grocery store underway. My first goal is to get the customers created. To start this, first and last names (which will be assigned at random) need to be read in from a file and stored in 2 different string arrays, which I've done. Next, I'm in the process of creating 2 classes, one for Customer objects, and another called customerFactory which will produce all the random data and assign it to each new customer, as required by the programming project.
Before I try to code every piece of these 2 classes, I wanted to at least get the names from the files read in. I can accomplish this from main() with no problem. I decided it would be better to do it from within the constructor of the customerFactory, since an instance of this class will be creating all the customers using these names. Upon creating this customerFactory class, copying the code from main to read in the names, and making a few changes to use the data members of the factory class for storing the string arrays, when trying to create a "factory" instance inside main(), I get 4 errors that reference the built-in assert.h header. What's going on here? errors: c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\assert.h(47): error C2143: syntax error : missing ';' before 'string' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\assert.h(47): error C2143: syntax error : missing ';' before 'string' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\assert.h(47): fatal error C1004: unexpected end of file found c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\assert.h(47): fatal error C1004: unexpected end of file found Below is the current code for my driver, customerFactory header and implementation, and the contents of the 2 .txt files. driver: //CECS 302 - CODY S-----
#include "customerFactory.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cassert>
#include <string>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
// --------------------------- Begin Customer Creation ----------------------------------------
customerFactory factory = customerFactory();
system("PAUSE");
return 0;
}customerFactory Header: //CECS 302 - CODY S-----
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cassert>
#include <string>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#ifndef CUSTOMER_FACTORY_H
#define CUSTOMER_FACTORY_H
class customerFactory
{
public:
customerFactory();
//void newRandomCustomer(int);
private:
string *firstNames;
string *lastNames;
}
#endifcustomerFactory Implementation: //CECS 302 - CODY S-----
#include "customerFactory.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cassert>
#include <string>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
customerFactory::customerFactory(string[] first, string[] last, int size_first, int size_last)
{
// --------------------------- Read in first and last names for random new customers -----------------------------------
ifstream infile, infile2;
char *first_file = "first.txt";
char *last_file = "last.txt";
int size_first, size_last;
infile.open(first_file);
assert(infile);
infile >> size_first; //gets size of first_name string array
firstNames = new string[size_first];
//Allocate space for new array based on input from file
string val;
infile.ignore();
// Write data from first name file into first name array
for(int i=0; i < size_first; i++)
{
getline (infile, val);
firstNames[i] = val;
}
infile.close(); // Closes the opened file
// ----------------------------------
infile2.open(last_file);
assert(infile2);
infile2 >> size_last; //gets size of first_name string array
lastNames = new string[size_last];
//Allocate space for new array based on input from file
infile2.ignore();
// Write data from first name file into last name array
for(int j=0; j < size_last; j++)
{
getline (infile2, val);
lastNames[j] = val;
}
infile2.close(); // Closes the opened file
cout << firstNames[0] << ", " << lastNames[0] << endl;
return;
}first names text file: 50 al fred john tim terry ryan dave chad greg scott jimbo cody chris carl eric matt max sam sherry jessica tina tracy trina emily joyce theresa tammy pam linda lisa joni gayle mary marianne lindy lucy sharon samantha taylor christine carol caitlin maegan nicole bob jim jerry kyle martin finch Last Names text file: 50 sledge shutt harper stemen goodlet hides coe johnson roberts richmond milliken marks priddy price maxfield neubaugher bogner traynor mesa boogie schechter bin links best last most host roast rouchka wagner maron kimmer komp lewis phillips samsung apple mac sack flack flask boon coon downy swingline fine line shine mine few knew |
|
|
|
|
|
#2 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
You're missing the semicolon at the end of your class definition in your customerFactory header. |
|
|
|
|
|
|
#3 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
HAHA... OMG! and I thought class ending semi-colons was well instilled in my programming brain... It would be something like that to partially make my evening bad... THANK YOU AREVOS... That would have driven me nuts all night.. and thanks for the quick response, that will certainly help me get much more done tonight (and post plenty more problems, i'm sure of if it [HA])! Seriously, check back tonight, very likely to be more posts.
|
|
|
|
|
|
#4 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
OK, everything for the customerFactory class and the relevant constructor for the customer class has been written. Now, I'm going to have to run a for loop 1000 times to create 1000 random customers.
I'm having some trouble with this part, b/c I want to create a standard c[#] instance for each customer, but it's saying no. Here's the code I have based on your suggestions: The function within the customerFactory class that passes the random data to the customer constructor: Customer customerFactory::newRandomCustomer()
{
Customer c = Customer(firstNames[randFirst()], lastNames[randLast()], randomPayType(), deliVisitor(), sweepsVisitor(), randomNumGen(), randArrival());
return c;
//Calls all member functions to pass random data to newly created customer based on for loop in main. Call to randomNumGen() used for numItems
}The Customer Constructor: Customer::Customer(string first, string last, int pay, bool deli, bool sweeps, int items, int arrival)
{
firstName = first;
lastName = last;
payMethod = pay;
visitsDeli = deli;
visitsSweeps = sweeps;
numItems = items;
initialArrival = arrival;
}and the driver: int main()
{
// --------------------------- Begin Customer Creation ----------------------------------------
customerFactory factory = customerFactory();
for(int i = 0; i<1000; i++)
{
Customer c[i] = factory.newRandomCustomer();
}
system("PAUSE");
return 0;
}I was trying to return a customer object from the customerFactory and set it equal to c[i], but I'm guessing there is an issee using the same name for instance inside the factory. Does anyone know how to make a similar method work? Last edited by codylee270; Dec 2nd, 2006 at 9:47 PM. |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"using namespace std" in header files is not recommended. Better to expend the effort to write "std::string", etc. Also, you put your header guards below all the includes, which means you can get a lot of shit shoveled in there if the file happens to be included, somehow, twice (which is what the header guards are for, anyway).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Create an array of customers, and then enter the loop.
int main()
{
customerFactory factory = customerFactory();
Customer c[1000];
for(int i = 0; i<1000; i++)
{
c[i] = factory.newRandomCustomer();
}
} |
|
|
|
|
|
#7 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Hello all,
What do you mean by working assignment operator. Do you mean I need to overload the =? As for the #ifndef, etc, I moved those up. And I know that I shouldn't use namespace std; throughout, but I won't be using any other namespace here, and to be honest I'm not even totally sure how to create other namespaces. I'm a little disappointed that my classes have yet to cover the topic. And, my main now looks like: int main()
{
// --------------------------- Begin Customer Creation ----------------------------------------
customerFactory factory = customerFactory();
Customer c[1000];
for(int i = 0; i<1000; i++)
{
Customer c[i] = factory.newRandomCustomer();
}
system("PAUSE");
return 0;
}But I'm still getting all of these fun errors: error C2146: syntax error : missing ';' before identifier 'newRandomCustomer' error C2057: expected constant expression error C2075: 'c' : array initialization needs curly braces error C2466: cannot allocate an array of constant size 0 error C2146: syntax error : missing ';' before identifier 'newRandomCustomer' error C2146: syntax error : missing ';' before identifier 'newRandomCustomer' error C2501: 'customerFactory::Customer' : missing storage-class or type specifiers error C2501: 'customerFactory::Customer' : missing storage-class or type specifiers error C2501: 'customerFactory::Customer' : missing storage-class or type specifiers warning C4183: 'newRandomCustomer': missing return type; assumed to be a member function returning 'int' warning C4183: 'newRandomCustomer': missing return type; assumed to be a member function returning 'int' warning C4183: 'newRandomCustomer': missing return type; assumed to be a member function returning 'int' error C2371: 'customerFactory::newRandomCustomer' : redefinition; different basic types error C2556: 'Customer customerFactory::newRandomCustomer(void)' : overloaded function differs only by return type from 'int customerFactory::newRandomCustomer(void)' And I'm postive they all stem from the creation of the customers via the loop. What now? Thanks for the help guys... |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The first one sounds like a missing type or brace or something. As for the array,
Customer c[i] = factory.newRandomCustomer();
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
The one error, the syntax error, says I have an issue in my customerFactory class at my newRandomCustomer definition: I've commented the location:
//CECS 302 - CODY S-----
#ifndef CUSTOMER_FACTORY_H
#define CUSTOMER_FACTORY_H
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cassert>
#include <string>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <queue>
#include <ctime>
using namespace std;
class customerFactory
{
public:
customerFactory(); // constructor
//Postcondtion: Reads in first and last names from external files into 2 string arrays
int randomPayType();
//Precondition: none
//Postcondtion: Returns 1 for check, 2 for cash, 3 for credit
int randomNumGen();
//Precondition: none
//Postcondtion: Returns a value b/w 1 and 100 for a percentage comparison
bool sweepsVisitor();
//Precondition: none
//Postcondtion: Return true if customer will visit the sweeps, else returns false
bool deliVisitor();
//Precondition: none
//Postcondtion: Return true if customer will visit the deli, else returns false
int randFirst();
//Precondition: none
//Postcondtion: Returns random index value to assign a first name from the firstNames string array
int randLast();
//Precondition: none
//Postcondtion: Returns random index value to assign a last name from the lastNames string array
int randArrival();
//Precondition: none
//Postcondtion: Returns value b/w 1 and 1441 (* 60 for minute to second conversion)
Customer newRandomCustomer(); // <----------- ERROR C2146
//Precondition:
//Postcondtion: Uses all functions above to create random first and last name, determines whether customer will visit the sweeps and/or deli, arrival time into the store,
// the number of items the customer will purchase, and creates a customer instance by using the customer constructor.
private:
string *firstNames;
string *lastNames;
int size_first, size_last;
};
#endifDo you see the problem there? All errors posted below go with this line except the first one, and the last 2. main now looks like: int main()
{
// --------------------------- Begin Customer Creation ----------------------------------------
customerFactory factory = customerFactory();
Customer c[1000];
for(int i = 0; i<1000; i++)
{
c[i] = factory.newRandomCustomer();
}
system("PAUSE");
return 0;
}Here are the current errors: error C2146: syntax error : missing ';' before identifier 'newRandomCustomer' error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) error C2146: syntax error : missing ';' before identifier 'newRandomCustomer' error C2146: syntax error : missing ';' before identifier 'newRandomCustomer' error C2501: 'customerFactory::Customer' : missing storage-class or type specifiers error C2501: 'customerFactory::Customer' : missing storage-class or type specifiers error C2501: 'customerFactory::Customer' : missing storage-class or type specifiers warning C4183: 'newRandomCustomer': missing return type; assumed to be a member function returning 'int' warning C4183: 'newRandomCustomer': missing return type; assumed to be a member function returning 'int' warning C4183: 'newRandomCustomer': missing return type; assumed to be a member function returning 'int' rror C2371: 'customerFactory::newRandomCustomer' : redefinition; different basic types error C2556: 'Customer customerFactory::newRandomCustomer(void)' : overloaded function differs only by return type from 'int customerFactory::newRandomCustomer(void)' So is it saying I'm needing an oveloaded operator. If so, for what. I'm trying to assign one object to another w/ both the same type? |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
At this point,
Customer newRandomCustomer(); // <----------- ERROR C2146
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Instant Messaging System Final Year Project | bondito | Java | 20 | Oct 29th, 2007 12:08 AM |
| Need help making a game plan for final programming project! | codylee270 | C++ | 27 | Dec 1st, 2006 8:29 PM |
| help with timers for final class project | Rath72 | Visual Basic | 8 | Jun 14th, 2006 7:55 AM |
| Final Year Project | smakazmi | Other Programming Languages | 8 | Oct 7th, 2005 3:09 AM |
| University Final Year Project | nez | Project Ideas | 3 | May 23rd, 2005 9:59 AM |