-
May 10th, 2014, 11:55 AM
#1
compling error
Hi guys!
I am working on a small project which contains three files
1. MainTest.cpp
2. CashRegister.h
3. CashRegister.cpp
I have done everything that suppose to be done but still I am getting an error and can not execute the program.... can anyone points out what is wrong with my program?
Here is the code
Code:
// MainTest.cpp
#include <iomanip>
using std::setw;
using std::left;
#include <iostream>
using std::cout;
using std::endl;
using std::ios;
#include "CashRegister.h"
const char ERROR[] = "Error! Registration failed.";
int main()
{
CashRegister cash("Register.txt", 5);
cout << "This test should give five error messages below!" << endl;
cout << "------------------------------------------------" << endl;
if (!cash.registerItem( 3,"Tvål Sun", 18.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Äppelmos Bob", 14.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Coca Cola 2 lit.", 12.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Kroppkakor 4 st.", 18.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Mjölk mellan 1 lit", 6.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 2,"Skottkärran Emil", 389.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 2,"Räfsan Gunda", 110.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Socker bit ½ kg", 14.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 3,"Tandkräm Linnéa", 9.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 3,"Deodorant Axe", 37.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 4,"Aftonbladet", 8.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 4,"Mitt livs korsord", 39.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Svenssons äppelpaj", 30.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 4,"Skraplott Lyckans Ost", 25.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 5,"Räfflade dyckert 1000 st.", 89.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 5,"Hammaren Adam", 65.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Sörens Grova Limpa", 12.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Kaffe Gevalia Mörk", 29.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Glass Holland Eis", 32.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 2,"Myrgift Sudda Bort", 82.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 5,"Sandpapper blandat set", 32.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 3,"Munvatten Dentovit", 31.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 5,"Borrmaskinen Herbert", 310.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Makrillfiléer ½ kg", 38.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 6,"Golvlampan Hilda", 230.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 0,"Öl Pripps Blå 6-pack", 30.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 6,"Rakapparaten Orvar", 99.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Findus Köttfärssås 2,5 dl.", 16.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 0,"Skruv 4x22 1000 st.", 80.50 ))
cout << ERROR << endl;
if (!cash.registerItem(-1,"Blå Castello 100 gr.", 19.50 ))
cout << ERROR << endl << endl;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << setw(30) << left << "The total for this sail was:" << cash.batchTotal() << " SEK" << endl;
cout << setw(30) << left << "Expected result should be: " << "1473.00 SEK" << endl << endl;
return 0;
}
Code:
//CashRegister.cpp
class CashRegister{
public:
CashRegister(char* fileName, int nrOfCategories);// The file contains
~CashRegister(); // registered items and balances
bool registerItem(int category, char* articleName, double amount);
double batchTotal(){
double total = _batchTotal;
_batchTotal = 0; // batchTotal() is expected to reset _batchTotal;
return total;
}
private:
ofstream _file; // Here will registered items be saved
int _nrOfCategories; // products are organized in categories
double* _categorySums; // The vector will contain respective sum of each category sold during the day
double _batchTotal; // sold since the last summation
};
Code:
// CashRegister.h
#ifndef CASHREGISTER_H
#define CASHREGISTER_H
CashRegister(char* fileName, int nrOfCategories);
bool registerItem(int category, char* articleName, double amount);
double batchTotal();
#endif
any kind of help will be appreciated... thanks in advance
Sincere Regards
-
May 10th, 2014, 02:44 PM
#2
Re: compling error
What type of error? Compile, link, run time? What is the error?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 10th, 2014, 03:07 PM
#3
Re: compling error
Code:
ofstream _file; // Here will registered items be saved
where do you include fstream? (or ostream)
Your CashRegister.h file makes no sense as it doesn't contain the class declaration. The CashRegister.cpp file contains the class declaration and a definition for batchTotal() but no definiton for the construtor, destructor and registerItem(). Definitions for these need to be provided.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 15th, 2014, 10:00 AM
#4
Re: compling error
Hi guys!
Thanks for your time and reply. I have made the changes you have suggested and moved the class declaration and definition to .h file
and now the error I am getting is error: 'ofstream' does not name a type
the line it is point out is:
ofstream _file;
-
May 15th, 2014, 10:15 AM
#5
Re: compling error
Originally Posted by new_bee
... and now the error I am getting is error: 'ofstream' does not name a type
the line it is point out is:
ofstream _file;
Well, you seem to ignore the post#3 of 2kaud!
So where is the #include of the file where ofstream is declared?
Victor Nijegorodov
-
May 15th, 2014, 10:47 AM
#6
Re: compling error
yes I have included the line on the top as such
#include <ofstream>
and yes it seems like it has solved the problem
Thanks
Last edited by new_bee; May 15th, 2014 at 10:53 AM.
-
May 15th, 2014, 10:55 AM
#7
Re: compling error
Then use
Code:
std::ofstream _file;
or add
Code:
using namespace std;
before your
Victor Nijegorodov
-
May 18th, 2014, 04:07 PM
#8
Re: compling error
where do i need to add class declaration? in the .cpp file or .h file?
-
May 18th, 2014, 10:48 PM
#9
Re: compling error
A small clarification may be in order. This is a class definition that defines a class named X:
Code:
class X
{
// ...
};
A class definition is also a class declaration, but here is a declaration of X that is not its definition:
Generally, unless it is implementation detail or if your program is so trivial such that it has no header files, a class would be defined in a header file that is then included wherever the definition of that class is needed, e.g., to define an object of the class type. The class might be forward declared in other places where the class name is required but the class definition is not needed, e.g., to declare a pointer to an object of the class type without creating an object of the class at that point.
In your case, this means that the definition of CashRegister should be moved to CashRegister.h. You would remove the attempted declaration of CashRegister's construction (which was invalid anyway), and the declarations of registerItem and batchTotal (as non-member functions) from that header. Instead, your class definition would contain the valid member function declarations (which can also be member function definitions, if you want to define them inline for some reason).
-
May 20th, 2014, 11:45 AM
#10
Re: compling error
Hi! I have done a lot of work to complete the application as i wanted but it did not work I am a Java developer and this part is required to be in c++ so i do not know how to handle things in headers
can anyone help me to out the save the results in the Register.txt as such
>>>> Register Opened: 2014-05-20 Tuesday at 17:45
17:45 : Cat. 3 : 18.00 Kr. [Tvål Sun ]
17:45 : Cat. 1 : 14.50 Kr. [Äppelmos Bob ]
17:45 : Cat. 1 : 12.00 Kr. [Coca Cola 2 lit. ]
17:45 : cat. 1 : 18.00 Kr. [Kroppkakor 4 St. ]
17:45 : cat. 1 : 6.50 Kr. [Mjölk mellan 1 lit ]
17:45 : cat. 2 : 389.50 Kr. [Skottkärran Emil ]
.
.
.
.
.
17:45 : Cat. 1 : 38.50 Kr. [Makrillfileer 1/2 Kg]
17:45 : cat. 1 : 16.00 Kr. [Findus Köttfärssås 2,5 dl. ]
>>>> Register closed: 2014-05-20 Tuesday at 17:45
Category totals for last opening period:
----------------------------------------
Category 1: 224.50 Kr.
category 2: 582.00 Kr.
Category 3: 96.00 Kr.
Category 4: 73.00 Kr.
Category 5: 497.50 Kr.
Total sales within all categories: 1473.00 Kr.
I have done everything just little more work to be done?
I will appreciate any help
-
May 20th, 2014, 11:50 AM
#11
Re: compling error
Originally Posted by new_bee
Hi! I have done a lot of work to complete the application as i wanted but it did not work I am a Java developer and this part is required to be in c++ so i do not know how to handle things in headers
Well, now you programs not only compiles but also runs.
What problem do you have with it or its results?
Did you try to debug it?
Victor Nijegorodov
-
May 20th, 2014, 12:10 PM
#12
Re: compling error
it is not giving the results what i want... rather it does not produce anything
here is my progress so far
MainTest.cpp
Code:
// MainTest.cpp
#include <iomanip>
using std::setw;
using std::left;
#include <iostream>
using std::cout;
using std::endl;
using std::ios;
#include "CashRegister.h"
const char ERROR[] = "Error! Registration failed.";
int main()
{
CashRegister cash("Register.txt", 5);
cout << "This test should give five error messages below!" << endl;
cout << "------------------------------------------------" << endl;
if (!cash.registerItem( 3,"Tvål Sun", 18.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Äppelmos Bob", 14.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Coca Cola 2 lit.", 12.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Kroppkakor 4 st.", 18.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Mjölk mellan 1 lit", 6.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 2,"Skottkärran Emil", 389.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 2,"Räfsan Gunda", 110.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Socker bit ½ kg", 14.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 3,"Tandkräm Linnéa", 9.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 3,"Deodorant Axe", 37.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 4,"Aftonbladet", 8.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 4,"Mitt livs korsord", 39.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Svenssons äppelpaj", 30.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 4,"Skraplott Lyckans Ost", 25.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 5,"Räfflade dyckert 1000 st.", 89.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 5,"Hammaren Adam", 65.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Sörens Grova Limpa", 12.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Kaffe Gevalia Mörk", 29.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Glass Holland Eis", 32.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 2,"Myrgift Sudda Bort", 82.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 5,"Sandpapper blandat set", 32.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 3,"Munvatten Dentovit", 31.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 5,"Borrmaskinen Herbert", 310.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Makrillfiléer ½ kg", 38.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 6,"Golvlampan Hilda", 230.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 0,"Öl Pripps Blå 6-pack", 30.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 6,"Rakapparaten Orvar", 99.50 ))
cout << ERROR << endl;
if (!cash.registerItem( 1,"Findus Köttfärssås 2,5 dl.", 16.00 ))
cout << ERROR << endl;
if (!cash.registerItem( 0,"Skruv 4x22 1000 st.", 80.50 ))
cout << ERROR << endl;
if (!cash.registerItem(-1,"Blå Castello 100 gr.", 19.50 ))
cout << ERROR << endl << endl;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << setw(30) << left << "The total for this sail was:" << cash.batchTotal() << " SEK" << endl;
cout << setw(30) << left << "Expected result should be: " << "1473.00 SEK" << endl << endl;
return 0;
}
CashRegister.h
Code:
//CashRegister.h
#ifndef CASHREGISTER_H
#define CASHREGISTER_H
#include <fstream>
class CashRegister{
public:
CashRegister(char* fileName, int nrOfCategories);// The file contains registered items and balances
~CashRegister();
bool registerItem(int category, char* articleName, double amount);
double batchTotal(){
double total = _batchTotal;
_batchTotal = 0; // batchTotal() is expected to reset _batchTotal;
return total;
}
private:
std::ofstream _file; // Here will registered items be saved
int _nrOfCategories; // products are organized in categories
double* _categorySums; // The vector will contain respective sum of each category sold during the day
double _batchTotal; // sold since the last summation
};
#endif
CashRegister.cpp
Code:
//CashRegister.cpp
#include "CashRegister.h"
#include <fstream>
CashRegister::CashRegister(char* fileName, int nrOfCategories){
_file.open(fileName, std::ofstream::in | std::ofstream::out | std::ofstream::app);
_nrOfCategories = nrOfCategories;
}
bool registerItem(int category, char* articleName, double amount){
}
my CashRegister.cpp is not finished yet.. it requires some coding
-
May 20th, 2014, 12:19 PM
#13
Re: compling error
Originally Posted by new_bee
it is not giving the results what i want... rather it does not produce anything
The you have to debug your code to find the problem that it "does not produce anything".
Originally Posted by new_bee
...
my CashRegister.cpp is not finished yet.. it requires some coding
Then finish it!
Victor Nijegorodov
-
May 20th, 2014, 01:10 PM
#14
Re: compling error
Just giving you code a quick eyeball and not trying to really figure it out, I couldn't help but notice this function does nothing.
Code:
bool registerItem(int category, char* articleName, double amount){
}
It really shouldn't even compile.
Your batchTotal function doesn't make any kind of sense. I don't see where you're totaling anything.
-
May 20th, 2014, 02:13 PM
#15
Re: compling error
Code:
bool registerItem(int category, char* articleName, double amount){
}
as a minimum this should be
Code:
bool CashRegister::registerItem(int category, char* articleName, double amount){
return false;
}
registerItem is part of the class CashRegister so needs to be defined as part of that class rather than as a non-class function. Also as the function is defined to return a bool then as it minimum it should at least return a valid bool value.
rather it does not produce anything
If it doesn't produce the output from the first two cout statements then you have serious problems.
You do not check anywhere that the file has been successfully opened in the class constructor. IMO I would avoid calling any function in a constructor that might fail as success/failure cannot be easily be reported.
Why are you using char arrays rather than the c++ string class?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|