Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Beginner C++ help


  • Please log in to reply

#1
finalcloud13

finalcloud13

    New Member

  • Member
  • Pip
  • 6 posts
#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.

Edited by finalcloud13, 17 October 2007 - 04:23 AM.

  • 0

Advertisements


#2
jpshortstuff

jpshortstuff

    Member

  • Member
  • PipPipPip
  • 119 posts
Hi,

if (m == 6 || 7 || 8 || 9 || 10)


change this to:

if ( (m == 6) || (m == 7) || (m == 8) || (m == 9) || (m == 10) )

(added brackets for clarity)

Because each item between each || is treated as condition. So your original was checking if m==6, then just seeing if 7 was true (not m==7). You need each condition to be written out in full.

Another way to write it would be this:

if ( (m >= 6) && (m <= 10) )

Many will say this is neater and easier to read.


That solves your first problem, not sure about your second I'm not big on C++ but I'll have a little look at some methods of formatting if no-one else replies.

Hope that helps,

jpshortstuff

Edited by jpshortstuff, 17 October 2007 - 01:26 PM.

  • 0

#3
finalcloud13

finalcloud13

    New Member

  • Topic Starter
  • Member
  • Pip
  • 6 posts
Thank you very much for your help :)
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP