Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,234 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,706 people online right now. Registration is fast and FREE... Join Now!




Using Classes

 
Reply to this topicStart new topic

Using Classes

oat
15 Mar, 2007 - 04:03 PM
Post #1

New D.I.C Head
*

Joined: 30 Jan, 2007
Posts: 20


My Contributions
I am totally lost on where to go, or even if I have done the assignment right... I really need some help on this one, any help would be greatly appreciated, the assignment asks you to:

QUOTE
Define a class named CoffeeOrder. Declare a private static field that holds the price of a cup of coffee as $1.25. Include private integer fields that you set to a flag value of 1 or 0 to indicate whether the order should have any of the following: cream, milk, sugar, or artificial sweetener. Include a public function that takes a user's order from the keyboard and sets the values of the four fields in response to four prompts. If the user indicated both milk and cream, turn off the milk flag to allow only cream. If the user indicated both sugar and artificial sweetener, turn off the artificial sweetener flag, allowing only sugar. Include another function that displays the user's completed order. Write a main() function that continues to ask a user for an order in a loop until the user indicated the order is complete or 10 orders have been placed, whichever comes first. After the user indicates that ordering is complete, display a recap of all the coffee orders, including the cream, milk, sugar, and sweetener status of each, as well as a count of the number of coffees ordered and the total price.


Here is my code, I don't guarantee that anything I put in this code actually makes sense

CODE
// Coffee2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class CoffeeOrder
{
private:
    static int cupOfCoffee;

    int cream;
    int milk;
    int sugar;
    int artSweet;

public:
    void displayValues();
    void takeOrder();
};

CoffeeOrder::displayValues()
{
    cout<<"Your Coffee Includes: "<<cream<<<<<<endl;

}
CoffeeOrder::takeOrder(int cream, int milk, int sugar, int artSweet)
{
cout<<"Would You Like Cream In Your Coffee? ";
    cin>>cream;
    if(cream == 0)
cout<<"Would You Like Milk In Your Coffee? ";
    cin>>milk;    
    else
cout<<"Would You Like Sugar In Your Coffee? ";
    cin>>sugar;
    if(sugar == 0)
cout<<"Would You Like Artificial Sweetner In Your Coffee? ";
    cin>>artSweet;



int CoffeeOrder::cupOfCoffer = 1.25;
}

int main()
{
CoffeeOrder aCoffee;
int cream, milk, sugar, artSweet;

aCoffee.displayValues();
}

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Using Classes
15 Mar, 2007 - 06:46 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
well you seem to be off to a good start. is there a particular problem you are having?

the line cout<<"Your Coffee Includes: "<<cream<<<<<<endl; does not make much sence to me, but I think once you put the other values and a prompt or so in there that should work...

We don't ussually ask people to enter 1's and 0's as answeres to questions, and it really is better to not allow the user to directly assign the private data perhapse something like this would be better:
CODE

char cInput;
cout << "would you like cream in your coffee? ";
cin >> cInput
if (cInput == 'y' || cInput == 'Y') { cream = 1; }

maybe with a check to loop though until the user gives valid input.

also note that the lines:
CODE
if (cream==0)
cout<<"Would You Like Milk In Your Coffee? ";
    cin>>milk;
    else
cout<<"Would You Like Sugar In Your Coffee? ";
    cin>>sugar;
are going to cause you trouble, that should look like:
CODE
if (cream==0)
{
cout<<"Would You Like Milk In Your Coffee? ";
cin>>milk;
} else
{
cout<<"Would You Like Sugar In Your Coffee? ";
    cin>>sugar;
}
as the if statement only executed the next line so the cout and the cin there will get seperated. Also look at the logic there... not sure this is what you want to happen.


This post has been edited by NickDMax: 15 Mar, 2007 - 06:47 PM
User is offlineProfile CardPM
+Quote Post

oat
RE: Using Classes
15 Mar, 2007 - 09:38 PM
Post #3

New D.I.C Head
*

Joined: 30 Jan, 2007
Posts: 20


My Contributions
I put those recommendations in and I still get these errors, I'm sure 1 mishap caused many of these errors, please could you guys take a look.


  • error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  • error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  • error C2556: 'int CoffeeOrder::displayValues(void)' : overloaded function differs only by return type from 'void CoffeeOrder::displayValues(void)'
  • see declaration of 'CoffeeOrder::displayValues'
  • error C2371: 'CoffeeOrder::displayValues' : redefinition; different basic types
  • see declaration of 'CoffeeOrder::displayValues'
  • error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  • error C2556: 'int CoffeeOrder::takeOrder(void)' : overloaded function differs only by return type from 'void
  • CoffeeOrder::takeOrder(void)'
  • see declaration of 'CoffeeOrder::takeOrder'
  • error C2371: 'CoffeeOrder::takeOrder' : redefinition; different basic types
  • see declaration of 'CoffeeOrder::takeOrder'
  • error C2181: illegal else without matching if
  • error C2655: 'CoffeeOrder::cupOfCoffee' : definition or redeclaration illegal in current scope
  • see declaration of 'CoffeeOrder::cupOfCoffee'
  • error C2086: 'double CoffeeOrder::cupOfCoffee' : redefinition
  • see declaration of 'cupOfCoffee'
  • error C2264: 'CoffeeOrder::displayValues' : error in function definition or declaration; function not called

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Using Classes
16 Mar, 2007 - 03:54 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
Well seeing the code that made those errors would be nice....
User is offlineProfile CardPM
+Quote Post

oat
RE: Using Classes
16 Mar, 2007 - 06:56 AM
Post #5

New D.I.C Head
*

Joined: 30 Jan, 2007
Posts: 20


My Contributions
CODE
// Coffee2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class CoffeeOrder
{
private:
    static double cupOfCoffee;

    int cream;
    int milk;
    int sugar;
    int artSweet;

public:
    void displayValues();
    void takeOrder();
};

CoffeeOrder::displayValues()
{
    cout<<"Your Coffee Includes: "<<cream<<endl;

}
CoffeeOrder::takeOrder()
{
char cInput;
char sInput;
cout << "Would You Like Cream In Your Coffee? ";
cin >> cInput;
if (cInput == 'y' || cInput == 'Y') { cream = 1; }
{
cout<<"Would You Like Milk In Your Coffee? ";
cin>>milk;
}
else
{
cout<<"Would You Like Sugar In Your Coffee? ";
    cin>>sugar;
}
    if (sInput == 'y' || sInput == 'Y') { cream = 1; }
    {
cout<<"Would You Like Artificial Sweetner In Your Coffee? ";
    cin>>artSweet;
    }

double CoffeeOrder::cupOfCoffee = 1.25;
}

int main()
{
    {
CoffeeOrder aCoffee;
aCoffee.displayValues();
    }
    system("Pause");
    return 0;
}

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Using Classes
17 Mar, 2007 - 10:18 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
owch... you did mean you, "put those recommendations in" line by line, word for word.

Well most of your errores can by fixed by adding the word "void" to the begining of your definition lines:
void CoffeeOrder::displayValues()
void CoffeeOrder::takeOrder()

The definition of your function must look like the declaration.

Once that is done you still get some errors because you copied my code and pasted it into your code without thinking.

CODE
cin >> cInput
if (cInput == 'y' || cInput == 'Y') { cream = 1; }

this was an example of how to get an input that allows the user to press 'y' or 'Y' rather than '1' or '0'... You can't use it exactly as written in each case.

Because you did not think when you pasted this into your code you made the construction:
CODE
    if (cInput == 'y' || cInput == 'Y') { cream = 1; }
    {
        cout<<"Would You Like Milk In Your Coffee? ";
        cin>>milk;
    }     else
    {
        cout<<"Would You Like Sugar In Your Coffee? ";
        cin>>sugar;
    }
Here the 'else' causes an error because it is not paired with the 'if' above it. The if statement has { cream = 1; } and that is what it executes... the next lines are not part of the if statement. Thus seperating the 'if' from the 'else'. Even if they DID work that way... your logic there would make NO sence.

When you program you have to run the program in your head line by line. THINK about what each line does.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:51PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month