GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Jul-2005, 06:17
shelokay shelokay is offline
New Member
 
Join Date: Jul 2005
Posts: 5
shelokay is on a distinguished road
Unhappy

showing £1.00


Hey guys please help im going insane here.

I have to multiply total no of cans purchased with cost and display the cost price.

the problem i am having is it display the cost as £0.5 or £1..when i want it to display as £0.50 or £1.00

i know this is prob something real simple.. but what am i doing wrong

my variables are floats..

help please xx

from a damsel in distress.. ;-)

my program so far is as below.. i know it not neat but i have to include certain things

CPP / C++ / C Code:
include<iostream.h>
#include<conio.h>
#include<process.h>

 float canitem,bottleitem,sweetsitem,pensitem,foldersitem;
// this is so the variables can be called into the sales function.

 int sales(float,float,float,float,float);

main()
{
 const float items[5]={0.50,1.00,0.40,0.50,2.00};
  // these variables must always contain the cost price.

 float cantotal,bottletotal,sweetstotal,penstotal,folderstotal;
  // variables to be used for totalling the cost price on each item.

 float grandtotal,tendered,change,vat;
  // variables to be used for the calculation totals.

 char heading[12]="Sky College";
  // The college name can now be easily called into part of the program.

// The following is now the main page display and for the user to input
// the amount of items purchased, using a loop to display a line.

  
  clrscr(); //

  cout<<"\t\t\t"<<heading<<endl<<"\t\t"; // This will centralise the heading.

   int i; // variable for the loop.
     for(i=0;i<=17;i++)
       {
	 int show;
	 show=i%2;
	((show==1)?cout<<"--":cout<<" ");
       }

  cout<<"\n\n\n\n Please enter the number of each item purchased."<<endl;

   cout<<"\n* Cool drink can: ";
   cin>>canitem;
   cout<<"\n* Cool drink bottle: ";
   cin>>bottleitem;
   cout<<"\n* Sweets: ";
   cin>>sweetsitem;
   cout<<"\n* Pens: ";
   cin>>pensitem;
   cout<<"\n* Folders: ";
   cin>>foldersitem;

 cantotal=canitem*items[0];
 bottletotal=bottleitem*items[1];   // These are the formulas for
 sweetstotal=sweetsitem*items[2];   // adding the totals
 penstotal=pensitem*items[3];         // for display.
 folderstotal=foldersitem*items[4];


sales(cantotal,bottletotal,sweetstotal,penstotal,folderstotal);
// passing the total values into the function and calling the function.

  grandtotal=cantotal+bottletotal+sweetstotal+penstotal+folderstotal;

   cout<<"\n\n\t\t\t\t        TOTAL: œ"<<grandtotal;

  getch();
  }

// The sales function will display only items and
// total cost for items actually purchased by using the if expression to
// exclude any total which is equal to 0.

int sales(float c,float b,float s,float p,float f)
 {
  clrscr();

  if(c==0)
    {
    cout<<"";
    }
  else
    {
     cout<<"\nCool drink cans    purchased: "<<canitem<<"\t\tCost: œ"<<c;
    }
  if(b==0)
    {
     cout<<"";
    }
  else
    {
     cout<<"\nCool drink bottles purchased: "<<bottleitem<<"\t\tCost: œ"<<b;
    }
  if(s==0)
    {
     cout<<"";
    }
  else
    {
     cout<<"\nSweets             purchased: "<<sweetsitem<<"\t\tCost: œ"<<s;
    }
  if(p==0)
    {
     cout<<"";
    }
  else
    {
     cout<<"\nPens               purchased: "<<pensitem<<"\t\tCost: œ"<<p;
    }
  if(f==0)
    {
     cout<<"";
    }
  else
    {
     cout<<"\nFolders            purchased: "<<foldersitem<<"\t\tCost: œ"<<f;
    }
  return c;
  return b;
  return s;
  return p;
  return f;

  }
Last edited by LuciWiz : 06-Jul-2005 at 08:49. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 06-Jul-2005, 08:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,618
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by shelokay
Hey guys please help im going insane here.

I have to multiply total no of cans purchased with cost and display the cost price.

the problem i am having is it display the cost as £0.5 or £1..when i want it to display as £0.50 or £1.00

i know this is prob something real simple.. but what am i doing wrong

my variables are floats..



You can use formatting flags and functions like this for each item:

CPP / C++ / C Code:
     cout << "\nCool drink cans    purchased: "
          << resetiosflags(ios::fixed)
          << canitem<<"\t\tCost: œ"
          << setiosflags(ios::fixed) 
          << setprecision(2) 
          << c;

The setiosflags() , resetiosflags and setprecision() functions obtained by including the iomanip header

Question: why do you #include <process.h>?

Here's an answer to the question you didn't ask:

Didn't you get compiler messages for this stuff?

CPP / C++ / C Code:
 return c;
return b;
return s;
return p;
return f;

In fact you don't use the return value from this function (and it's a good thing you don't)

So, why not just make it a void type and delete all of the returns?

It is recommended that you tell us about all of the compiler messages you got. Sometimes it's important (but in this case no harm, no foul).

Here's a suggestion that won't necessarily make this program run better, but is a good habit to get into:

Use standard C++ header <iostream>, not <iostream.h>

So the top of your program might look like this:

CPP / C++ / C Code:
#include <iostream> // standard C++ header
#include <iomanip> // standard C++ header
#include <conio.h>  // non-standard header

using namespace std; // you need something like this now



Regards,

Dave
  #3  
Old 06-Jul-2005, 08:12
shelokay shelokay is offline
New Member
 
Join Date: Jul 2005
Posts: 5
shelokay is on a distinguished road
Thank you so much dave xxx

I will try this out..

oh and i had the process.h file cos i had tried something else and it failed and i forgot to take it back out doh!

and no i got no errors for the return values.. but i will void the function again something i was playing about with! as i am sure u can tell i still at college learning c++

thank you again xx
  #4  
Old 06-Jul-2005, 08:38
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,618
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by shelokay
and no i got no errors for the return values..


Well, some compilers warn you about "unreachable code", but I guess some don't.

The thing is that functions can return only one value (or none if its type is "void"). Furthermore, once a "return" statement is executed, program flow goes back to the calling function. (That is, all statements after the first "return" is encountered are ignored.) So, your program is not illegal or undefined behavior or anything other than bad style. (But shows a lack of understanding of program flow and use of functions.)

Quote:
Originally Posted by shelokay
as i am sure u can tell i still at college learning c++

Well, no one was born knowing this stuff. You have made a great start, I think. Good Luck!


Regards,

Dave
  #5  
Old 06-Jul-2005, 08:41
shelokay shelokay is offline
New Member
 
Join Date: Jul 2005
Posts: 5
shelokay is on a distinguished road
thanx for your words of encouragement... xx

unfortunalty (i cant spell for one!) i tried to reset flags and it didnt bring up an error but it still showed as £1 or £0.5 i ven read up on internet about this resetting flag thing.. to no aval
  #6  
Old 06-Jul-2005, 09:58
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,618
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by shelokay
i tried to reset flags and it didnt bring up an error but it still showed as £1 or £0.5 (

I showed a possible sequence for one item. Similar code goes for each and every item.

How about posting your current code? Also, what Operating System and Compiler are you using? (Sometimes it helps us to get to the bottom of things.)

Regards,

Dave
  #7  
Old 06-Jul-2005, 10:15
shelokay shelokay is offline
New Member
 
Join Date: Jul 2005
Posts: 5
shelokay is on a distinguished road
i dont it thanx to u dave! once i knew what i was looking for i could research it on the internet.

i achieved it by

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

just before display in my sales loop and it done it for all of them!

oh and incase u interested i use borland antique compiler.. cos this is what we use at college so i dont have much choice in that! even tho it dont seem to reconise try and catch!!! oh well beggers cant be users!

and can i just say i am so chuffed with myself now i have got my program working!! i try to tell my boyfriend and daughter but they just dont understand!

i am quite pleased with my end result and i hope i get a good remark from my tutor now or i think my pride will be hurt!

thanx again dave thank you so much xxx
  #8  
Old 06-Jul-2005, 10:16
shelokay shelokay is offline
New Member
 
Join Date: Jul 2005
Posts: 5
shelokay is on a distinguished road
damn sorry there such bad grammer and typo errors in my last post... guess u can tell i been working on my program for last 9 hours now!!! lol
  #9  
Old 06-Jul-2005, 10:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,618
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by shelokay
i dont it thanx to u dave! once i knew what i was looking for i could research it on the internet.

i achieved it by


The cool thing is that, once you know a way of doing things, lots of times you can find better ways (or at least ways that are more appealing to your sensibilities). Getting past that first block is the most important.

Onward and upward!

Regards,

Dave
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Showing the "current" page crystalattice Web Design Forum 5 27-Apr-2005 10:55
Problem Showing Images !!!! eRIC MySQL / PHP Forum 0 05-May-2004 22:33
ClassView not showing my classes (VC++, namespace, headers) ?!?!? djovanov CPP / C++ Forum 1 13-Jan-2004 04:54
GIDZipTest now showing compression % JdS Learning Journal by J de Silva 0 27-May-2003 22:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:46.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.