My header file:
// Definition of class SunshineWeb that counts Product 1, Product 2
// and Product 3 retail prices. Refer to Project 2.cpp for member
// functions.
using namespace std;
#include <string> // program uses C++ standard string class
// SunshineWeb class definition
class SunshineWeb
{
public:
SunshineWeb( string ); // constructor initialises customer name
void setCustomerName( string ); // function to get customer name
string getCustomerName; // function to retrieve customer name
void displayMessage(); // displays a welcome message
void inputProducts(); // inputs a number of products
void displayProductTotal(); // total number of what products
private:
string CustomerName; // customer's name
int Product1Count;
int Product2Count;
int Product3Count;
}; // end of class SunshineWeb
Member-function definitions, switch counter and all that:
// Project 2.cpp : Defines the entry point for the console application.
// Member-function definitions for class SunshineWeb that uses a switch
// statement to count Products, then calculate and display Product total.
#include "stdafx.h"
#include <iostream>
#include "SunshineWeb.h"
#include <iomanip>
using namespace std;
//constructor initialises CustomerName with string supplied as arguement;
//initialises counter data member to 0
SunshineWeb::SunshineWeb( string name )
{
setCustomerName( name ); // validates and store CustomerName
Product1Count = 0; // initialises count of Product 1 to 0
Product2Count = 0; // initialises count of Product 2 to 0
Product3Count = 0; // initialises count of Product 3 to 0
} // end SunshineWeb constructor
// function to set the customer's name; limits to 25 or fewer characters
void SunshineWeb::setCustomerName( string name )
{
if ( name.length() <= 25 ) // if the name has 25 or fewer characters
CustomerName = name;
else // if name is longer than 25 characters
{ // sets CustomerName to the first 25 characters of parameter name
CustomerName = name.substr( 0, 25 ); // selects first 25 characters
cout << "Name \"" << name << "\" exceeds maximum length (25). \n"
<< "Limiting CustomerName to first 25 characters.\n" << endl;
} // end if... else statement
} // end function setCustomerName
// function to retrieve customer's name
string SunshineWeb::getCustomerName()
{
return CustomerName;
}
// displays a welcome message to the user
void SunshineWeb::displayMessage()
{
cout << "Welcome to Sunshine Web's store application!" << endl;
} // end function displayMessage
void SunshineWeb::inputProducts()
{
int products; // products entered by user
cout << "Enter 1 for Product 1, 2 for Product 2, " << endl
<< "3 for Product 3, and EOF character to end input." << endl;
// loops until user inputs end-of-file key sequence
while( ( products = cin.get() ) != EOF )
{
// determine which product was entered
switch (products) // switch statement nested in while
{
case '1': // The character 1 was inputted
++Product1Count; // increment Product1Count
break; // necessary to exit the switch
case '2': // The character 2 was inputted
++Product2Count; // increment Product2Count
break; // necessary to exit the switch
case '3': // The character 3 was inputted
++Product3Count; // increment Product3Count
break; // necessary to exit the switch
case '\n': // ignores new lines,
case '\t': // tabs,
case ' ' : // and spaces inbetween input
break; // exit switch
default: // catch all other characters
cout << "Incorrect character entered."
<< "Please enter 1, 2, 3 or EOF key." << endl;
break; // exit switch
} // end switch
} // end while
} // end function inputProducts
// displays the quantity of the product and retail total
void SunshineWeb::displayProductTotal()
{
// output summary of user orders
cout << "Quantity of each Product ordererd by the user:"
<< "\nProduct 1, at $22.98 per unit: " << Product1Count // displays number of Product 1
<< "\nProduct 2, at $34.50 per unit: " << Product2Count // displays number of Product 2
<< "\nProduct 3, at $99.98 per unit: " << Product3Count // displays number of Product 3
<< endl;
// algorithim used to calculate total price
int total = static_cast <double> (Product1Count*22.98) + static_cast <double>(Product2Count*34.50) + static_cast<double>(Product3Count*99.98);
cout << "The total price of your order is: " << fixed << setprecision(2) << total << endl;
} // end function displayProductTotal
Problem begins at string SunshineWeb::getCustomerName()
, where it underlines getCustomerName()
and states the following:
Error:declaration is incompatible with "std::string SunshineWeb::getCustomerName"
(declared at line 12 of "c:\users\user\documents\visual studio 2010\projects\project 2\project 2\SunshineWeb.h")
And this is the main file / executor thingy:
// creates SunshineWeb object, inputs products and displays
// total quantity and retail price of products inputted.
#include "SunshineWeb.h" // includes definition of class SunshineWeb
int main()
{
// creates SunshineWeb object
SunshineWeb mySunshineWeb("Sunshine Web Store");
mySunshineWeb.displayMessage();
mySunshineWeb.inputProducts();
mySunshineWeb.displayProductTotal();
} // end main
My second problem is with SunshineWeb mySunshineWeb("Sunshine Web Store");
, where it underlines Sunshine Web Store
and states:
Error: no suitable constructor exists to convert from "const char[19]" to "SunshineWeb"
Just beginning to learn C++ object orientation programming, and this is a program I wrote for class - what gives? I googled and went through my notes, but it doesn't help / solve this problem at all. What really bugs me most is the no suitable constructor part, as I can simply throw all Customer Name related functions out of the window, as I haven't even implemented it yet, seeing as I can't get it working ; but maybe there's something I'm missing. Either way... I need help! Please save my baby!
Edit : Thanks for the help, I've noticed my error and tried tweaking it like suggested, so my header file now looks like :
// Definition of class SunshineWeb that counts Product 1, Product 2
// and Product 3 retail prices. Refer to Project 2.cpp for member
// functions.
#include <string> // program uses C++ standard string class
// SunshineWeb class definition
class SunshineWeb
{
public:
SunshineWeb( std::string ); // constructor initialises customer name
void setCustomerName( std::string ); // function to get customer name
std::string getCustomerName(); // function to retrieve customer name
void displayMessage(); // displays a welcome message
void inputProducts(); // inputs a number of products
void displayProductTotal(); // total number of what products
private:
std::string CustomerName; // customer's name
int Product1Count;
int Product2Count;
int Product3Count;
}; // end of class SunshineWeb
This fixed the incompatible declaration error (a simple typo mistake) from earlier, and I'm not sure if it fixed the error with no suitable constructor, because while the nasty red underline is gone, I still can't run the program : I opened up the error list from Visual Studio 2010, I see a couple of errors:
Error 5 error C2065: 'mySunshineWeb' : undeclared identifier c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 9
Error 7 error C2065: 'mySunshineWeb' : undeclared identifier c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 10
Error 9 error C2065: 'mySunshineWeb' : undeclared identifier c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 11
Error 2 error C2065: 'SunshineWeb' : undeclared identifier c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 8
Error 3 error C2146: syntax error : missing ';' before identifier 'mySunshineWeb' c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 8
Error 6 error C2228: left of '.displayMessage' must have class/struct/union c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 9
Error 10 error C2228: left of '.displayProductTotal' must have class/struct/union c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 11
Error 8 error C2228: left of '.inputProducts' must have class/struct/union c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 10
Error 4 error C3861: 'mySunshineWeb': identifier not found c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 8
Warning 1 warning C4627: '#include "SunshineWeb.h"': skipped when looking for precompiled header use c:\users\user\documents\visual studio 2010\projects\project 2\project 2\sunshineweb.cpp 3
Yes, I am a complete amateur making (mostly) silly mistakes, but for the love of my life I can't seem to find a solution to my seemingly-simple problems. These problems makes no sense whatsoever, and I'm quadruple checking everything. Thanks for the help (and patience displayed), I really appreciate it.