here is the code for my program....
It's very basic to you gurus but about the best I can manage for now :)
I posted a question regarding error trapping earlier but didn't put in my
code.
I need a pointer towards how I can ensure that a number is inut into the
RoomLength, RoomWidth, RoomHeight etc etc
I have tried playing with isalpha and isdigit (and the ! versions) but can't
think how to test for number, re-do section if false, go on to next if true.
Just a pointer please folks,
Many thanks
// Decorator Asssignment.cpp : Defines the entry point for the console
application.
// Software Constructs and Tools
// Programming Assignment
// Arnie Owen Jan 2002
#include "stdafx.h"
#include <stdlib.h>
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char* argv[])
{
//This section declares all variables
char CustName [23], WallChoice, Quit []="*";//escape character
int
RollsReqd,NoDoors,DoorArea,DoorPaint,StripsReqd,StripsReqd1,PatternRepeat;
int
StripsReqd2,StripsReqd3,StripsReqd4,StripsPerRoll,ExtraRolls,WidthInt,Length
Int;
float NoWindows,StripLength, WindowCost, WoodworkCost, Subtotal,InvoiceVAT;
float CeilingArea,WallArea,CeilingCost, WallPaperCost, RollCost,RoomHeight;
float
RoomLength,RoomWidth,WindowArea,WindowPaint,WallPaintUsed,WallPaintCost,Door
Cost, Total;
int TrimTop=3,TrimBottom=3;//Paper Trim Amounts
const float VAT=17.5;//VAT @ current rate
const float GlossLitre=4.00;//Price of Gloss Paint per litre
const float EmulsLitre=3.50;//Price of Emulsion per litre
int StripWidthcm=52;//Width of Paper in cm
int RollLength=10;//Paper length in mr
//Displays an initial Menu Screen and loops until exit
do
{
//This sections initialises all declared variables.
WallPaperCost=0,WallPaintCost=0,WallPaintUsed=0;
DoorPaint=0,WindowPaint=0,RollCost=0;
RoomLength=0,RoomWidth=0,RoomHeight=0;
CeilingArea=0,WallArea=0,NoDoors=0;
RollsReqd=0,NoWindows=0,InvoiceVAT=0,ExtraRolls=0;
StripsReqd=0,StripsReqd1=0,StripsReqd2=0,StripsReqd3=0,StripsReqd4=0;
cout<<"Joe Blake Painting & Decorating\t\t\tTel 01522 522522"<<endl;
cout<<"\nPlease enter the customers name.(Max 22 characters)\n";
cout<<"or press * to quit the program.\n\n"<<endl;
cin.getline(CustName,sizeof(CustName));
if (strcmp(CustName,Quit))//compare input with the quit sequence
{
cout<<"\n\nPlease enter the room length (xx.x metres)\t";
cin>>RoomLength;
cin.ignore();
cout<<"\n\nPlease enter the room width (xx.x metres)\t";
cin>>RoomWidth;
cout<<"\n\nPlease enter the room height (xx.x metres)\t";
cin>>RoomHeight;
cout<<"\n\nPlease enter the number of doors.\t\t";
cin>>NoDoors;
cout<<"\n\nPlease enter the number of windows in the room.\t";
cin>>NoWindows;
//This section calculates the area of the different room items
WindowArea=NoWindows*2;
DoorArea=NoDoors*2;
CeilingArea=(RoomWidth*RoomLength);
WallArea=(((RoomLength+RoomWidth)*2)*RoomHeight)-(WindowArea+DoorArea);
//This section determines if the walls are to be papered or painted
//and calculates that decorating costs
cout<<endl<<"\nDo you want the room wallpapered ( Y / N )\t";
cin>>(WallChoice);
//Loop to ensure valid selection
while
((WallChoice!='Y')&&(WallChoice!='y')&&(WallChoice!='N')&&(WallChoice!='n'))
{
cout<<"\n\aPlease select Y for Wallpaper or N for paint !!\t";
cin>>WallChoice;
}
if ((WallChoice=='Y')||(WallChoice=='y'))
{
cout<<"\nHow large is the Pattern Repeat for the paper selected ? (xx
cm)\t";
cin>>PatternRepeat;
cout<<"\nHow much is the paper per roll ?\t\t";
cin>>RollCost;
LengthInt=RoomLength*100;//convert room length from float mr to int cm
for modulus calc
StripsReqd1=LengthInt/StripWidthcm;//gives the integer value of strips
reqd
if ((LengthInt%StripWidthcm)>0)//perform modulus calculation and
{
StripsReqd2=StripsReqd2+1;//if there is a remainder add 1 to strips
reqd
}
WidthInt=RoomWidth*100;//convert room width from float mr to int cm for
modulus calc
StripsReqd3=(WidthInt/StripWidthcm);
if ((WidthInt%StripWidthcm)>0)//perform modulus calculation and
{
StripsReqd4=StripsReqd4+1;//if there is a remainder add 1 to strips
reqd
}
StripsReqd=(((StripsReqd1+StripsReqd2)*2)+((StripsReqd3+StripsReqd4)*2));
//calc total amount of strips reqd
StripLength=((PatternRepeat*.5)+(RoomHeight*100)+TrimTop+TrimBottom)/100;
//calc length of each strip
StripsPerRoll=RollLength/StripLength;
//calc how many whole strips per roll
if (StripsReqd%StripsPerRoll>0)
{
ExtraRolls=ExtraRolls+1;
}
RollsReqd=(StripsReqd/StripsPerRoll)+ExtraRolls;
WallPaperCost=RollsReqd*RollCost;
}
if ((WallChoice=='N')||(WallChoice=='n'))
{
WallPaintUsed=(WallArea/8);
WallPaintCost=(WallPaintUsed)*EmulsLitre;
}
//This segment performs the calculations for the invoice.
CeilingCost=(CeilingArea/8)*EmulsLitre;
DoorPaint=NoDoors;
DoorCost=DoorPaint*GlossLitre;
WindowPaint=NoWindows*.5;
WindowCost=WindowPaint*GlossLitre;
WoodworkCost=DoorCost+WindowCost;
Subtotal=CeilingCost+WoodworkCost+WallPaintCost+WallPaperCost;
InvoiceVAT=Subtotal*(VAT/100);
Total=Subtotal+InvoiceVAT;
//This section produces the estimate as per the assignment spec. The
//cout statements are required to format the text and numbers.
system("CLS");
cout<<" Joe Blake Painting & Decorating\t\tTel : 01522 522522\n\n";
cout<<" Estimate of Costs for ........"<<CustName<<endl;
cout<<"\nMATERIALS\n\n";
cout<<setprecision (1)<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
cout<<" Ceiling : "<<setw(4)<<CeilingArea/8<<" litres\t @ \x9c ";
cout<<setprecision (2)<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
cout<<setw(4)<<EmulsLitre<<" per litre\t\x9c
"<<setw(6)<<CeilingCost<<endl;
cout<<setprecision (1)<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
cout<<" Walls : "<<setw(4)<<WallPaintUsed<<" litres\t @ \x9c ";
cout<<setprecision (2)<<setiosflags(ios::right)
<<setiosflags(ios::showpoint);
cout<<setw(4)<<EmulsLitre<<" per litre\t\x9c
"<<setw(6)<<WallPaintCost<<endl;
cout<<setprecision (0)<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
cout<<setw(15)<<RollsReqd<<" rolls of paper\t @ \x9c ";
cout<<setprecision (2)<<setiosflags(ios::right)
<<setiosflags(ios::showpoint);
cout<<setw(5)<<RollCost<<" per roll\t\x9c "<<setw(6)<<WallPaperCost<<endl;
cout<<setprecision (1)<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
cout<<" Woodwork: "<<setw(4)<<DoorPaint+WindowPaint<<" litres\t @ \x9c
";
cout<<setprecision (2)<<setiosflags(ios::right)
<<setiosflags(ios::showpoint);
cout<<setw(4)<<GlossLitre<<" per litre\t\x9c
"<<setw(6)<<WoodworkCost<<endl;
cout<<"\t\t\t\t\t\t\t========"<<endl;
cout<<"\nSUBTOTAL\t\t\t\t\t\t\x9c "<<setw(6)<<Subtotal<<endl;
cout<<setprecision (1)<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
cout<<"\nVAT @ "<<setw(6)<<VAT;
cout<<setprecision (2)<<setiosflags(ios::right)
<<setiosflags(ios::showpoint);
cout<<" %\t\t\t\t\t\t\x9c "<<setw(6)<<InvoiceVAT<<endl;
cout<<"\t\t\t\t\t\t\t========"<<endl;
cout<<"\nFINAL TOTAL\t\t\t\t\t\t\x9c "<<setw(6)<<Total<<endl;
cout<<"\n\n";
cin.ignore();
system("pause");
system("CLS");
}
} while(strcmp(CustName,Quit));
cout<<"\n\nThank you using the estimatation program \n";
cout<<"devised by Arnie Owen. Jan 2002\n\n";
return 0;
}
> I need a pointer towards how I can ensure that a number is inut into the
> RoomLength, RoomWidth, RoomHeight etc etc
> I have tried playing with isalpha and isdigit (and the ! versions) but
> can't think how to test for number, re-do section if false, go on to next
> if true.
> Just a pointer please folks,
I'm also going to give just a few style tips.
> // Decorator Asssignment.cpp : Defines the entry point for the console
> application.
> // Software Constructs and Tools
> // Programming Assignment
> // Arnie Owen Jan 2002
>
> #include "stdafx.h"
> #include <stdlib.h>
> #include <iostream.h>
> #include <iomanip.h>
Read the book /Accelerated C++/, then prefer the versions of these that
don't have a .h on the end.
> #include <conio.h>
> #include <stdio.h>
> #include <string.h>
> #include <ctype.h>
>
> int main(int argc, char* argv[])
> {
> //This section declares all variables
> char CustName [23], WallChoice, Quit []="*";//escape character
Use 'std::string', and put each declare on a different line.
> int
> RollsReqd,NoDoors,DoorArea,DoorPaint,StripsReqd,StripsReqd1,PatternRepeat;
> int
>
StripsReqd2,StripsReqd3,StripsReqd4,StripsPerRoll,ExtraRolls,WidthInt,Length
> Int;
> float NoWindows,StripLength, WindowCost, WoodworkCost,
> Subtotal,InvoiceVAT; float CeilingArea,WallArea,CeilingCost,
> WallPaperCost, RollCost,RoomHeight; float
>
RoomLength,RoomWidth,WindowArea,WindowPaint,WallPaintUsed,WallPaintCost,Door
> Cost, Total;
> int TrimTop=3,TrimBottom=3;//Paper Trim Amounts
> const float VAT=17.5;//VAT @ current rate
> const float GlossLitre=4.00;//Price of Gloss Paint per litre
> const float EmulsLitre=3.50;//Price of Emulsion per litre
> int StripWidthcm=52;//Width of Paper in cm
> int RollLength=10;//Paper length in mr
Woah. You have a latent "object" here - long term consider putting every
one of these into a class, and give the class methods.
> //Displays an initial Menu Screen and loops until exit
This comment could be a method on that object instead. Then it would
self-document.
> do
> {
> //This sections initialises all declared variables.
Many of your comments add no value. We can see variables initializing.
> WallPaperCost=0,WallPaintCost=0,WallPaintUsed=0;
> DoorPaint=0,WindowPaint=0,RollCost=0;
> RoomLength=0,RoomWidth=0,RoomHeight=0;
> CeilingArea=0,WallArea=0,NoDoors=0;
> RollsReqd=0,NoWindows=0,InvoiceVAT=0,ExtraRolls=0;
> StripsReqd=0,StripsReqd1=0,StripsReqd2=0,StripsReqd3=0,StripsReqd4=0;
If those variables (so long as they are not a class) are never used outside
this do loop, then move the declares themselves in here.
> cout<<"Joe Blake Painting & Decorating\t\t\tTel 01522 522522"<<endl;
> cout<<"\nPlease enter the customers name.(Max 22 characters)\n";
> cout<<"or press * to quit the program.\n\n"<<endl;
> cin.getline(CustName,sizeof(CustName));
To end a line with a linefeed, prefer...
getline(cin, CustName);
...if CustName were a 'std::string'.
> if (strcmp(CustName,Quit))//compare input with the quit sequence
If CustName were a 'std::string' that could have been:
if (CustName != Quit)
> {
> cout<<"\n\nPlease enter the room length (xx.x metres)\t";
> cin>>RoomLength;
> cin.ignore();
> cout<<"\n\nPlease enter the room width (xx.x metres)\t";
> cin>>RoomWidth;
> cout<<"\n\nPlease enter the room height (xx.x metres)\t";
> cin>>RoomHeight;
> cout<<"\n\nPlease enter the number of doors.\t\t";
> cin>>NoDoors;
> cout<<"\n\nPlease enter the number of windows in the room.\t";
> cin>>NoWindows;
I think you can say:
if (!(cin >> RoomWidth))
cout << "error ...";
--
Phlip
thanks for the reply, I'm going to sound really dense but....you lost me on
most of what you said :)
I've got to put comments in 'cos it's a very elementary level of programming
we're doing at the moment and I think the tutors want us to show we know
what the code we have written does ;)
we have not come across 'std::string', or objects, classes, methods...
hopefully I'll be able to appreciate the help you've tried to provide when I
know a bit more :)
Somebody hinted at cin.clear so I looked up examples and came up with
examples that I then amended to suit my need, needed to put them into a
'while' statement for each of the menu inputs.....
cout<<"\n\nPlease enter the room length (xx.x metres)\t";
while(!(cin >> RoomLength))
{
cin.clear();
cin >> badChars;
cout << "\nPlease enter the room length (xx.x metres)\t";
}
cout<<"\n\nPlease enter the room width (xx.x metres)\t";
etc etc etc
Can I confirm that the cin>>badChars flushes whatever was left in cin pipe ?
also..translation of
while(!(cin >> RoomLength)) - I read this as 'while the value of roomlength
is not 0'
now I'm confused because I've set it to 0 when I initialize it but how does
it know that I don't want characters ?
Thanks very much for the reply......every bit helps.....
Well, your tutor has a point, but it needs some additional illustration.
The idea of the comments is more WHY than WHAT. In the case of assigning
some values to variables, one should point out only non-trivial
assigments and operations.
Consider these two pieces of code:
for(z=0;z!=MAGIC;z+=7){ // loop from 0 to MAGIC with step 7
doSomethingCool(z);
doSomethingElseToo(z+2);
}
for(z=0;z!=MAGIC;z+=7){ // each field has length of 7
doSomethingCool(z);
doSomethingElseToo(z+2);
}
Apparently, the first comment is useless. Any semi-competent programmer
recognizes instantly what the loop does - technically. The comment
doesn't tell WHY the step is 7 or whether it is important to be 7.
Now, the latter comment tells the reader that some field has length of
seven units, so we are stepping through individual fileds or the like.
Another one:
int magic=6; // declare an int 'magic' and assign it to six
int magic=6; // the sixth bit is the one to watch for
Once again, the first comment doesn't add any value to the reader. If
the reader needs this kind of comments, (s)he shouldn't be reading the
code at all.
> we have not come across 'std::string', or objects, classes, methods...
They are soon to come, I hope. Surely your tutor is not using char
arrays for strings?
> while(!(cin >> RoomLength)) - I read this as 'while the value of roomlength
> is not 0'
No.
The loop goes on as long as the input the cin is trying to store to
RoomLength is invalid. This happens if you type, say, a string when cin
is expecting a number. After getting a decent input, the loop will fail.
-P
Thou shalt not follow the Null Pointer,
for at its end Madness and Chaos lie.
> we have not come across 'std::string', or objects, classes, methods...
Your teacher is teaching things in the wrong order.
That's okay - the point of school is to suffer.
But you'l be better off beyond school if you get a handful of Addison
Wesley Longman books about C++ write now - begining with /Accelerated C++/,
and read read read.
--
Phlip