The Mother Board

Motherboards.org forums. Free tech support, motherboard ID, and more.
It is currently Mon Sep 24, 2018 3:41 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Beginner C++ help
PostPosted: Wed Oct 17, 2007 3:40 am 
Offline
Pilgrim
Pilgrim

Joined: Sat Jul 28, 2007 9:43 am
Posts: 14
#include <iostream>
#include <string>
using namespace std;

int main ()
{
int i, f, m;
string n;
double a = 2.60;
double b = 3.36;
double c = 2.79;
double t;

cout << "Initial meter reading:";
cin >> i;
cin.ignore(100000, '\n');

cout << "Final meter reading:";
cin >> f;
cin.ignore(100000, '\n');

int hcf = f - i;

cout << "Customer name:";
cin >> n;
cin.ignore(100000, '\n');

cout << "Month number (1=Jan, 2=Feb, etc.):";
cin >> m;
cin.ignore(100000, '\n');

if (m == 6 || 7 || 8 || 9 || 10)
{
if (hcf <= 46)
{
t = hcf * a;
}
else if (hcf > 46)
{
t = ((hcf - 46)*b) + 119.60;
}
}

else
{
if (hcf <= 32)
{
t = hcf * a;
}
else if (hcf > 32)
{
t = ((hcf - 32)*c) + 83.20;
}
}

cout << "---\nThe bill for " << n << " is $" << t << "\n";
}

The problem is in regards to the bolded part. I have the condition only if m is a number 6-10, but any number I input, it still uses the bold code block instead of going to the italicized else block. What am I doing wrong? And, how do I make it that in the underlined output, the number is always shown as a nonnegative number with at least one digit to the left and exactly two digits to the right of the decimal point?

Also, I'm not understanding too well why I don't get an error for using double, but I do for float. I thought float could be used for decimals too.

Please point out any mistakes in the code or anything that is not needed yet included in there. Thanks in advance.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Oct 17, 2007 7:59 am 
Offline
Black Belt 2nd Degree
Black Belt 2nd Degree

Joined: Mon Aug 09, 2004 7:33 pm
Posts: 2741
Location: Milwaukee - Wisconsin
why have all those ORs?

why not just use if( conditionA && conditionB ) like
Code:
if( m > 5 && m < 11)


as for the formatting portion - it's actually pretty simple, but you'll need to discover that on your own. this is a really simple example of what you're looking for - you should have a book or reference for which to find the same info:

http://www.arachnoid.com/cpptutor/student3.html

what error are you getting when using float?

-Roach

_________________
Lian Li Lancool First Knight Series PC-K59W
Intel Core i7-960 Bloomfield 3.2GHz
EVGA X58 FTW3 132-GT-E768-TR
EVGA GeForce GTX 1070 FTW GAMING ACX 3.0
G.SKILL Ripjaws Series 12GB (3 x 4GB)
Crucial M4 128gb SATAIII SSD x2
Crucial M4 256gb SATAIII SSD
OCZ Saber 1000 960GB Enterprise SSD
Corsair Professional Series HX850 PSU
Dell UltraSharp U2713HM 27" w/drop ceiling mount
Logitech G930 7.1 Headset
DBPOWER RGB LED Keyboard
Logitech G402 Hyperion Fury


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Oct 17, 2007 1:05 pm 
Offline
Pilgrim
Pilgrim

Joined: Sat Jul 28, 2007 9:43 am
Posts: 14
Thank you, it worked nicely. And slash the part where I said double gave errors, it doesn't anymore.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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 post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group