Ignoring character data types.

Hello everyone. I was, for a while, solving this problem our teacher had given us. But unfortunately, I keep failing. Our teacher had said that our program is not supposed to accept character data types whenever entered. But it keeps accepting. I would really be grateful if you could help me. Its a code that accepts building types(blg) then accepts kwh present(kwhpres) and previous(kwhprev) and compute for the respective payment(am). Here is my code:

#include<iostream.h>
void main()
{
double kwhpres, kwhprev, am, k, blg;


cout<<" Building Code"<<endl;
cout<<"----------------"<<endl;
cout<<"(1)-Residential"<<endl;
cout<<"(2)-Commercial"<<endl;
cout<<"(3)-Industrial"<<endl;
cout<<"Enter your choice: ";
cin>>blg;

if (blg==1)
{{
cout<<"Enter present reading ";
cin>>kwhpres;
cout<<"Enter previous reading ";
cin>>kwhprev;
k=kwhpres-kwhprev;
}
{if (k<=50&&k>=0)
{
am=27;
cout<<"Amount due: "<<am<<endl;
}

else if (k>50&&k<=100)
{
am=27+((k-50)*0.75);
cout<<"Amount due: "<<am<<endl;
}
else if (k>100&&k<=150)
{
am=27+((k-100)*3.50);
cout<<"Amount due: "<<am<<endl;
}
else if (k>150)
{
am=27+((k-150)*5.50);
cout<<"Amount due: "<<am<<endl;
}
else
{
cout<<"Invalid"<<endl;
}}}

else if (blg==2)
{ {
cout<<"Enter present reading ";
cin>>kwhpres;
cout<<"Enter previous reading ";
cin>>kwhprev;
k=kwhpres-kwhprev;
}
{if (k<=50&&k>=0)
{
am=35;
cout<<"Amount due: "<<am<<endl;
}

else if (k>50&&k<=100)
{
am=35+((k-50)*0.75);
cout<<"Amount due: "<<am<<endl;
}
else if (k>100&&k<=150)
{
am=35+((k-100)*3.50);
cout<<"Amount due: "<<am<<endl;
}
else if (k>150)
{
am=35+((k-150)*5.50);
cout<<"Amount due: "<<am<<endl;
}
else
{
cout<<"Invalid"<<endl;
}}}
else if (blg==3)
{ {
cout<<"Enter present reading ";
cin>>kwhpres;
cout<<"Enter previous reading ";
cin>>kwhprev;
k=kwhpres-kwhprev;
}
{if (k<=50&&k>=0)
{
am=52;
cout<<"Amount due: "<<am<<endl;
}
else if (k>50&&k<=100)
{
am=52+((k-50)*0.75);
cout<<"Amount due: "<<am<<endl;
}
else if (k>100&&k<=150)
{
am=52+((k-100)*3.50);
cout<<"Amount due: "<<am<<endl;
}
else if (k>150)
{
am=52+((k-150)*5.50);
cout<<"Amount due: "<<am<<endl;
}
else
{
cout<<"Invalid"<<endl;
}
}}}
You'll need some sort of while loop in there to validate the input and stop the program from progressing until a number has been entered.

Something like:
1
2
3
4
5
6
7
8
9
10
int my_int;

cout << "Please enter an integer number: ";

while(!(cin >> my_int))
{
    cout << "Invalid input, try again: ";
    cin.clear();
    cin.ignore(1000, '\n');
}


You'll need to adapt it for your code. If you need extra conditions (say, you don't want a negative number to be entered), just add them as or conditions for that while statement.
closed account (o3hC5Di1)
Hi there,

Would you please be so kind to wrap that code in code-tags ([code ]//code here[ /code]) and apply aome indentation for better readability?

As to your issue, there are some ideas on how to achieve this here:
http://stackoverflow.com/questions/5655142/how-to-check-if-input-is-numeric-in-c

All the best,
NwN
Last edited on
Thank you very much. But where specifically in my program should I put the loop part. Im sorry, Im just a bigginer in this proglang. Thanks.
closed account (o3hC5Di1)
Hi there,

Using iHutch's example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double kwhpres, kwhprev, am, k, blg;


cout<<" Building Code"<<endl;
cout<<"----------------"<<endl;
cout<<"(1)-Residential"<<endl;
cout<<"(2)-Commercial"<<endl;
cout<<"(3)-Industrial"<<endl;
cout<<"Enter your choice: ";

while(!(cin >> big))
{
    cout << "Invalid input, try again: ";
    cin.clear();
    cin.ignore(1000, '\n');
}


All the best,
NwN
So Its before the if else statements. Thank you very much.
closed account (o3hC5Di1)
Indeed, because you need to have the big value first before you can evaluate it in the if/else statements.

All the best,
NwN
Topic archived. No new replies allowed.