Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Final project has begun! So have the errors! (http://www.programmingforums.org/showthread.php?t=12087)

codylee270 Dec 2nd, 2006 7:20 PM

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;
}

#endif


customerFactory 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


Arevos Dec 2nd, 2006 7:25 PM

Quote:

Originally Posted by codylee270 (Post 120390)
I get 4 errors that reference the built-in assert.h header. What's going on here?

Ah, I remember when I fell for this bug back when I was still in school. It can be problematic for the unwary, because a syntax error in a header file can have results in the header file below it.

You're missing the semicolon at the end of your class definition in your customerFactory header.

codylee270 Dec 2nd, 2006 7:32 PM

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.

codylee270 Dec 2nd, 2006 8:49 PM

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?

DaWei Dec 2nd, 2006 9:30 PM

"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).

grumpy Dec 2nd, 2006 9:59 PM

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();
        }
}

For this to work, your Customer class will need to have a default constructor and a working assignment operator.

codylee270 Dec 2nd, 2006 11:06 PM

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...

DaWei Dec 2nd, 2006 11:16 PM

The first one sounds like a missing type or brace or something. As for the array,
:

                Customer c[i] = factory.newRandomCustomer();
That's trying to declare an array (c) 1000 times, with an invalid dimension. What's up with that? Declare it once, above, and then fill it.

codylee270 Dec 2nd, 2006 11:31 PM

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;
};

#endif


Do 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?

DaWei Dec 3rd, 2006 12:22 AM

At this point,
:

        Customer newRandomCustomer();  // <----------- ERROR C2146
how does the compiler know what a Customer is? I may be blind, but I don't see it defined at that point, and I'm pooty dam' sure it isn't a standard C++ type.


All times are GMT -5. The time now is 1:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC