LITTLEBLACKDOG.COM Forum Index LITTLEBLACKDOG.COM

 
LWD LWD   FAQ FAQ   Memberlist Memberlist   Usergroups Usergroups   Active Topics Active Topics   Register Register  
  Profile Profile   Log in to check your private messages Log in to check your private messages   Log in Log in  
  Who is Online Who is Online   Image Gallery Image Gallery   Chat Chat   Search Search  
  LWDGear       LBDGear  

View next topic
View previous topic
Post new topic     Reply to topic   LITTLEBLACKDOG.COM Forum Index » Code Warriors
Author Message
micaela
Stray Dog


Joined: 18 Mar 2003
Posts: 16
Location: South Carolina

Post Posted: Sun Apr 27, 2003 8:10 pm   Post subject: c++ help!! Reply with quote Back to top  

Okay, I have compiled this program and executed it but it just acts weird when I desk check it.................please take a look...... Smile .....

#include <iostream>
#include <cmath>
#include <string>
#include <ctype.h>
#include <conio.h>
#include <fstream.h>
#include <iomanip.h>

//function prototypes
char print_menu();
char get_shirt_type();
float shirt_price(char);
char get_cookie_type();
char get_cup_size();
float cup_price(char);

void main()
{
long lemonadeVol=0;
double sales=0.0;
float price;
int transactions=0,
oatmealCt=0,
cChipCt=0,
snickCt=0,
autoCt=0,
nonAutoCt=0;
char sel, size, cookie_type, shirt_type;

cout << " This program will help you track your lemonade, cookie, and \n"
<< "T-shirt sales.\n"
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint)
<<setprecision(2);

do
{
sel = print_menu();
}
while (!((sel == 'L') || (sel == 'C') || (sel == 'T') || (sel == 'Q')));

while (sel!='Q')
{
if (sel=='L')
{
size = get_cup_size();
price = cup_price(size);
cout << "\nYou have purchased a "<<size<<" lemonade for $"<< price <<".";
if (size == 'S')
{
lemonadeVol+=8;
}
else if (size=='M')
{
lemonadeVol+=12;
}
else
{
lemonadeVol+=16;
}
}
else if (sel=='C')
{
price=0.75;
cookie_type=get_cookie_type();
cout<<"\nYou have purchased a";
if (cookie_type=='S')
{
cout << " snickerdoodle ";
snickCt ++ ;
}
else if (cookie_type=='C')
{
cout << " chocolate chip cookie ";
cChipCt ++;
}
else
{
cout << "n oatmeal cookie ";
oatmealCt ++;
}
cout << " for $" << price << ".";
}
else if (sel=='T')
{
shirt_type = get_shirt_type();
price = shirt_price(shirt_type);
cout << "\nYou have purchased a ";
if (shirt_type=='A')
{
cout << "n autographed ";
autoCt ++;
}
else
{
cout << " non-autographed ";
nonAutoCt ++;
}
cout << "\"This Old House\"T-shirt for $" << price << ".\n";
}


transactions ++;
sales += price;
sel = print_menu();

}//end of while loop

cout << "\nTotal number of customers : " << transactions << " ."
<< "\nTotal volume of lemonade : " << lemonadeVol << " ounces. "
<< "\nNumber of oatmeal cookies sold : " << oatmealCt << " ."
<< "\nNumber of chocolate chip cookies sold: " << cChipCt << ". "
<< "\nNumber of snickerdoodles sold : " << snickCt << ". "
<< "\nNumber of autographed TOH shirts sokd: " << autoCt << " ."
<< "\nNumber of plain TOH shirts sold : " << nonAutoCt << " ."
<< "\nTotal sales : " << sales << " .\n";
};//end of main function

/*********************************************
* PURPOSE : Menu selection (L, C, T, or Q) *
* INPUT : USERS *
* OUTPUT : NONE *
**********************************************/
char print_menu()
{
char menu_choice;

cout<< "\n\nPress L for lemonade,\n"
<< " C for cookies, \n"
<< " T for T-shirts, or\n"
<< " Q to quit and view your sales data.\n\n";
menu_choice = getch();
menu_choice = toupper(menu_choice);
return menu_choice;

}//end function

/********************************************************
* PURPOSE : For users to enter their choice *
* of T-shirts--Autographed or non *
* autographed. *
* INPUT : Select "A" for autographed or *
* "N" for nonAutographed. *
* OUTPUT : "A" or "N" *
*********************************************************/
char get_shirt_type()
{
char shirtType;

do
{
cout <<"\nPress A for an autographed \"This Old House\" T-shirt "
<< "for $10\nPress N for a non-autographed \"This Old House\" "
<< "T-shirt for $5\n";
shirtType = getch();
shirtType = toupper(shirtType);
}
while (!((shirtType =='A') || (shirtType =='N')));
return shirtType;
}

/*****************************************************************
* PURPOSE : To assign a price to the T-shirt based on type *
* INPUT : shirt type (A or N) will be passed to this function*
* OUTPUT : Shirt price, $10 or $5 *
******************************************************************/

float shirt_price(char shirt_type)
{
float shirtPrice;
if(shirt_type=='A')
{
shirtPrice=10.00;
}
else if(shirt_type=='N')
{
shirtPrice = 5.00;
}
return shirtPrice;
}//end of function

/****************************************************************
* PURPOSE : To select a cookie type *
* INPUT : User will enter C, O, or S for chocolate chip, oat*
* meal, or snickerdoodle. *
* OUTPUT : C, O, or S *
*****************************************************************/
char get_cookie_type()
{
int cChipCt=0, oatmealCt=0, snickCt=0;
char cookieType;

do
{

cout << "\nAll cookies are $.75\n\n\n"
<<"Press C for chocolate chip\n"
<<"Press O for oatmeal\n"
<<"Press S for snickerdoodle\n";

cookieType=getch();
cookieType=toupper(cookieType);
}
while (!((cookieType == 'C') || (cookieType == 'O') || (cookieType == 'S')));
if(cookieType=='C')
{
cChipCt++;
}
else if(cookieType=='O')
{
oatmealCt++;
}
else
{
snickCt++;
}
return cookieType;
}

/****************************************************************
* PURPOSE : To choose a cup size. *
* INPUT : User will input S, M, or L *
* OUTPUT : S, M, or L *
*****************************************************************/
char get_cup_size()
{
char cupSize=' ';
do
{
cout << "\nLemonade is $.50 for 8 ounces, $.75 for 12 ounces\n"
<<"and $1.00 for 16 ounces.\n\n"
<<"Press S for a small (8 ounce) lemonade\n"
<<"Press M for a medium (12 ounce) lemonade\n"
<<"Press L for a large (16 ounce) lemonade : ";
cupSize=getch();
cupSize=toupper(cupSize);
}//end do
while (!((cupSize == 'S') || (cupSize == 'M') || (cupSize == 'L')));
return cupSize;
}

/****************************************************************
* PURPOSE : To assign a price for the lemonade based on size. *
* INPUT : cupSize from get_cup_size, not user input. *
* OUTPUT : $.50 if 8 ounce, $.75 if 12 ounce, or $1.00 if 16 *
* ounces. *
*****************************************************************/
float cup_price(char size)
{
float cupPrice;
if (size=='S')
{
cupPrice=.50;
}
else if (size=='M')
{
cupPrice=.75;
}
else if (size=='L')
{
cupPrice=1.00;
}
return cupPrice;
}//end of function


Help please!!! Sad

Micaela
View user's profile Send private message
brotherhobbes
Butt Sniffer
Butt Sniffer


Joined: 06 May 2002
Posts: 1579

Post Posted: Mon Apr 28, 2003 7:56 am   Post subject: Reply with quote Back to top  

just a quick look over it i noticed you don't have

Code: Select all

using namespace std;


i think iostream needs that.

EDIT: quick note, would you please put your code in the [ code] [ /code] (remove the spaces, i put them there so they would show up). it will keep the formatting you used.


Last edited by brotherhobbes on Mon Apr 28, 2003 8:46 am; edited 1 time in total
View user's profile Send private message Send e-mail
brotherhobbes
Butt Sniffer
Butt Sniffer


Joined: 06 May 2002
Posts: 1579

Post Posted: Mon Apr 28, 2003 8:44 am   Post subject: Reply with quote Back to top  

i'm not sure what a "desk check" is, but i compiled this and got a blinking cursor when i ran it.

ok, i think by "acting weird" you mean nothing is showing up? that problem is in your first call to print_menu() in the first do while loop.

about namespace std, you do need that when using iostream, but not iomanip. and you don't need both of those. use one or the other or you will get ambigious errors about cout.

i'd change your includes to be like this:
Code: Select all

#include <iostream>
#include <cmath>
//#include <iomanip.h>
#include <fstream.h>
using namespace std;
 
#include <ctype.h>
#include <conio.h> 
#include <string>

(actually i don't see you using anything to do with strings in this, just chars, so i'd get rid of the string include also).

and then the section where you use the iomanip stuff:
Code: Select all

    cout << " This program will help you track your lemonade, cookie, and \n"
    << "T-shirt sales.\n";
    <<setiosflags(ios::fixed)
    <<setiosflags(ios::showpoint)
    <<setprecision(2);


use something like this instead:
Code: Select all

    cout.setf(ios::fixed);
    cout.precision(2);

i don't think you need showpoint, because setting the precision to 2 will give you two decimal places for showing currency, yeh?
View user's profile Send private message Send e-mail
micaela
Stray Dog


Joined: 18 Mar 2003
Posts: 16
Location: South Carolina

Post Posted: Mon Apr 28, 2003 2:55 pm   Post subject: Reply with quote Back to top  

What I meant by acting weird is when I execute the program yeah it gave me a blinking cursor but if you press enter twice, the choices will come up and over and over again...... Confused When this is compiled without "using namespace std;", there's no errors...but with "using namespace std;", it gives out these errors:

Compiling...
prog6-ma.cpp
E:\Cpp\originalprogram#6\prog6-ma.cpp(3Cool : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(39) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(40) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(57) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(75) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(7Cool : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(83) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(8Cool : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(91) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(97) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(100) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(105) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(108) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(118) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(135) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(154) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(161) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(198) : error C2872: 'cout' : ambiguous symbol
E:\Cpp\originalprogram#6\prog6-ma.cpp(232) : error C2872: 'cout' : ambiguous symbol
Error executing cl.exe.

prog6-ma.obj - 19 error(s), 0 warning(s)


I'm gonna keep working on it though, thanks Brotherhobbes...... Laughing

Micaela
View user's profile Send private message
Lycander
Lead Dog
Lead Dog


Joined: 24 May 2002
Age: 25
Posts: 12198
Location: The Constitution State

Post Posted: Mon Apr 28, 2003 3:33 pm   Post subject: Reply with quote Back to top  

Code: Select all

char print_menu()
{
   char menu_choice;

   cout<< "\n\nPress L for lemonade,\n"
   << " C for cookies, \n"
   << " T for T-shirts, or\n"
   << " Q to quit and view your sales data.\n\n";
   menu_choice = getch();
   menu_choice = toupper(menu_choice);
   return menu_choice;
}//end function


Try

Code: Select all
menu_choice = cin.get()


instead of getch(). Or even
Code: Select all
cin >> menu_choice;

_________________
To the top of hunger mountain
I found my solitary ways
Where I could live on nuts and honey
And take my shelter in a cave
View user's profile Send private message
brotherhobbes
Butt Sniffer
Butt Sniffer


Joined: 06 May 2002
Posts: 1579

Post Posted: Mon Apr 28, 2003 5:49 pm   Post subject: Reply with quote Back to top  

is this for a class? or just for you? i'm a bit leary about giving you tons of code incase your professor asks you where you got it...


micaela wrote:
What I meant by acting weird is when I execute the program yeah it gave me a blinking cursor but if you press enter twice, the choices will come up and over and over again...... Confused When this is compiled without "using namespace std;", there's no errors...but with "using namespace std;", it gives out these errors:
...
prog6-ma.obj - 19 error(s), 0 warning(s)[/color]

I'm gonna keep working on it though, thanks Brotherhobbes...... Laughing

Micaela


change your includes and bypass including iomanip directly like i mentioned in my 2nd post. i'm pretty sure it should look like this:

Code: Select all

#include <iostream>
#include <cmath>
//#include <iomanip.h>
#include <fstream.h>
using namespace std;

#include <ctype.h>
#include <conio.h> 
#include <string>


i don't think you need to include string or cmath. i don't see anything that needs either of those. actually, i don't see what you need fstream for either.

going with lycander's suggestion, i'd use cin. cin waits for a newline (return/enter key) before continuing, if i remember right. it helps to write some input functions to deal with such things:

Code: Select all

// Gets a single character.
//
// Out: n   the character
//
// Return: true if a character was inputted.
bool get_char(char &n)
{
    if (cin >> n)
    {
        while (cin.get() != '\n') { }
        return true;
    }
    cin.clear();
    while (cin.get() != '\n') { }
    return false;
}

// Gets a number
//
// Out: n    the number.
//
// Return:   true if a number was inputed.
bool get_num(int &n)
{
    if (cin >> n)
   {
        while (cin.get() != '\n') { }
        return true;
    }
    cin.clear();
    while (cin.get() != '\n') { }
    return false;
}


i normally template the get_num, but i don't want to confuse you, so let's just go with that.

then you can do your input loops like:

Code: Select all

// includes
#include <iostream>
using namespace std;
#include <cctype>

// main
char choice;
do
{
    cout << "Would you like to enter a (C)ar, (S)hip, or (Q)uit? _\b";
    get_char(choice);
    choice = tolower(choice);
}
while (!strchr("csq", choice));


something else. you are declaring things like cChipCt in both main and in the get_cookie_type() function. you do realize that the cChipCt value won't carry back and forth unless you make it global (bad!), static in main, or pass from get_cookie_type(), right?
View user's profile Send private message Send e-mail
micaela
Stray Dog


Joined: 18 Mar 2003
Posts: 16
Location: South Carolina

Post Posted: Thu May 01, 2003 7:29 pm   Post subject: Reply with quote Back to top  

Laughing I finally got the program to work by using "cin >> ...". Also, had to stop looking at that program for a while to review for Finals. Thanks for the suggestions.

Micaela
View user's profile Send private message
brotherhobbes
Butt Sniffer
Butt Sniffer


Joined: 06 May 2002
Posts: 1579

Post Posted: Fri May 02, 2003 4:11 am   Post subject: Reply with quote Back to top  

glad it worked out
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic     Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group
phpBB SEO
All times are GMT - 8 Hours

Help us keep advertisements off this site. Donate today!