Condition FOR not number characters
Dec 1, 2019 at 4:41pm UTC
the if else statements works for numbers only
When the option is only number , if the user type any not number The program must Give a notice Invalid input but instead IT STARTED to act weird
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
#include <iostream>
#include <cstdlib>
using namespace std;
double nos,mv;
void sale()
{
cout<<"\n\t\t\t\t>----SALE----<" <<endl;
cout<<"Enter the Net of Sale: " ;
cin>>nos;
cout<<endl<<"Enter the Market Value: " ;
cin>>mv;
if (mv>=nos)
{
cout<<endl<<"\t\t\t CGT...............PHP " <<mv*.06<<endl;
cout<<"\t\t\t Doc Stamp.........PHP " <<(mv/1000)*15<<endl;
cout<<"\t\t\t Transfer Tax......PHP " <<nos*.02<<endl;
cout<<"\t\t\t Gross Tax.........PHP " <<(mv*.06)+((mv/1000)*15)+(nos*.02);
}
if (nos>mv)
{
cout<<endl<<"\t\t\t CGT...............PHP " <<nos*.06<<endl;
cout<<"\t\t\t Doc Stamp.........PHP " <<(nos/1000)*15<<endl;
cout<<"\t\t\t Transfer Tax......PHP " <<nos*.02<<endl;
cout<<"\t\t\t Gross Tax.........PHP " <<(nos*.06)+((nos/1000)*15)+(nos*.02);
}
}
void donation()
{
cout<<"\n\t\t\t\t>----DONATION----<" <<endl;
cout<<endl<<"Enter the Market Value: " ;
cin>>mv;
cout<<endl<<"\t\t\t CGT...............PHP " <<mv*.06<<endl;
cout<<"\t\t\t Doc Stamp.........PHP " <<(mv/1000)*15<<endl;
cout<<"\t\t\t DNT...............PHP " <<(mv*.12)<<endl;
cout<<"\t\t\t Transfer Tax......PHP " <<mv*.07<<endl;
cout<<"\t\t\t Gross Tax.........PHP " <<(mv*.06)+((mv/1000)*15)+(mv*.12)+(mv*.07);
}
double mv1=0,mv2=0,mv3=0,tmv;
void inheritance()
{
cout<<"\n\t\t\t\t>----INHERITANCE----<" <<endl;
cout<<endl<<"Enter the Market Value (PROPERTY 1): " ;
cin>>mv1;
cout<<"Enter the Market Value (PROPERTY 2): " ;
cin>>mv2;
cout<<"Enter the Market Value (PROPERTY 2): " ;
cin>>mv3;
tmv=mv1+mv2+mv3;
cout<<endl<<"\t\t\t Total Market Value = PHP " <<tmv<<endl;
cout<<endl<<"\t\t\t CGT...............PHP " <<tmv*.06<<endl;
cout<<"\t\t\t Doc Stamp.........PHP " <<(tmv/1000)*15<<endl;
cout<<"\t\t\t Estate Tax........PHP " <<tmv*.20<<endl;
cout<<"\t\t\t Transfer Tax......PHP " <<tmv*.07<<endl;
cout<<"\t\t\t Gross Tax.........PHP " <<(tmv*.06)+((tmv/1000)*15)+(tmv*.20)+(tmv*.07);
}
int main()
{
int month,day,year;
cout<<endl<<"\tSelect Date (00/00/0000)" <<endl;
cout<<endl<<"Enter Month: " ;
cin>>month;
cout<<"Enter Day: " ;
cin>>day;
cout<<"Enter Year: " ;
cin>>year;
char choice,choicea,choiceb,choicec;
menu:
system("cls" );
cout<<endl<<"\t >-----------------------<" ;
cout <<"\n\t\tComputerized Taxation \n\t\t\tFor \n\t\t Land Title Transfer" <<endl;
cout<<"\t >-----------------------<" <<endl;
cout<<"\t\t " <<month<<"/" <<day<<"/" <<year<<endl;
cout<<endl<<"\t\t Main Menu:" <<endl;
cout<<endl<<"\t\t [A]. Sale\n\t\t [B]. Donation\n\t\t [C]. Inheritance" <<endl;
cout<<"\n\tChoose a Letter:" ;
cin>>choice;
if (choice=='A' ||choice=='a' )
{
amenu:
system("cls" );
sale();
cout<<endl<<endl<<"Would you like to?" <<endl;
cout<<"[A].Exit Program" <<endl;
cout<<"[B].Back To Main Menu" <<endl;
cin>>choicea;
if (choicea=='A' ||choicea=='a' )
{
system("cls" );
cout<<"\n\n\t\tThank You!" <<endl<<endl;
system("pause" );
return 0;
}else
if (choicea=='B' ||choicea=='b' )
{
system("cls" );
cout<<endl<<endl<<"\t\t Back to Menu . . ." <<endl<<endl;
system("pause" );
goto menu;
}
else
{
system("cls" );
cout<<"\n\n\t\t\t\t\tInvalid Input!" <<endl<<endl;
system("pause" );
goto amenu;
}
}
else if (choice=='B' ||choice=='b' )
{
bmenu:
system("cls" );
donation();
cout<<endl<<endl<<"Would you like to?" <<endl;
cout<<"[A].Exit Program" <<endl;
cout<<"[B].Back To Main Menu" <<endl;
cin>>choiceb;
if (choiceb=='A' ||choiceb=='a' )
{
system("cls" );
cout<<"\n\n\t\tThank You!" <<endl<<endl;
system("pause" );
return 0;
}else
if (choiceb=='B' ||choiceb=='b' )
{
system("cls" );
cout<<endl<<endl<<"\t\t Back to Menu . . ." <<endl<<endl;
system("pause" );
goto menu;
}
else
{
system("cls" );
cout<<"\n\n\t\t\t\t\tInvalid Input!" <<endl<<endl;
system("pause" );
goto bmenu;
}
}
else if (choice=='C' ||choice=='c' )
{
cmenu:
system("cls" );
inheritance();
cout<<endl<<endl<<"Would you like to?" <<endl;
cout<<"[A].Exit Program" <<endl;
cout<<"[B].Back To Main Menu" <<endl;
cin>>choicec;
if (choicec=='A' ||choicec=='a' )
{
system("cls" );
cout<<"\n\n\t\tThank You!" <<endl<<endl;
system("pause" );
return 0;
}else
if (choicec=='B' ||choicec=='b' )
{
system("cls" );
cout<<endl<<endl<<"\t\t Back to Menu . . ." <<endl<<endl;
system("pause" );
goto menu;
}
else
{
system("cls" );
cout<<"\n\n\t\t\t\t\tInvalid Input!" <<endl<<endl;
system("pause" );
goto cmenu;
}
}
else
{
system("cls" );
cout<<"\n\n\t\t\t\t\tInvalid Input!" <<endl<<endl;
system("pause" );
goto menu;
}
return 0;
}
Dec 1, 2019 at 8:25pm UTC
Hello CoolAvocado,
You could try something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
cout << "Enter the Net of Sale: " ;
cin >> nos;
while (!std::cin)
{
std::cout << "\n Invalid Input!! Try again. " ;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' ); // <--- Requires header file <limits>.
cout << "Enter the Net of Sale: " ;
cin >> nos;
}
cout << endl << "Enter the Market Value: " ;
cin >> mv;
while (!std::cin)
{
std::cout << "\n Invalid Input!! Try again. " ;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' ); // <--- Requires header file <limits>.
cout << "Enter the Market Value: " ;
cin >> mv;
}
I just used the first function as an example, but the while loop can be used after any formatted input like
cin >> mv;
. Where "cin" expects a number.
Hope that helps,
Andy
Dec 2, 2019 at 1:25pm UTC
Andy what does std::meanssss????
Dec 2, 2019 at 1:46pm UTC
Dec 2, 2019 at 8:40pm UTC
Hello CoolAvocado,
What I usually put is:
It takes a little time, but better to learn to qualify what is needed with "std::" a little bit at a time then all at once.
The chances are that you paid a lot of money to be taught the easy way to write code because the classes are structured to make you wait. At least that is the opinion I have formed.
After a week or so I just type it without thinking.
Andy
Dec 3, 2019 at 1:03am UTC
Dec 3, 2019 at 11:16pm UTC
If you got rid of those ugly gotos, your code, especially your main(), would become way easier to read:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
#include <iostream>
#include <string>
struct MonthDayYear {
int month {};
int day {};
int year {};
// Debetable...
void askForMonthDayYear();
};
void MonthDayYear::askForMonthDayYear()
{
std::cout << "\n\tSelect Date (00/00/0000)\n\nEnter Month: " ;
std::cin >> month;
std::cout << "Enter Day: " ;
std::cin >> day;
std::cout << "Enter Year: " ;
std::cin >> year;
}
void mainMenu(const MonthDayYear& mdy);
char askForChar(const std::string& text, const std::string& options);
void displaySpacedString(const std::string& s);
bool exitOrAgain();
void sale();
void donation();
void inheritance();
int main()
{
MonthDayYear mdy;
mdy.askForMonthDayYear();
for (bool again { true }; again; /**/ ) {
mainMenu(mdy);
char choice {
askForChar( "\n\t\t [A]. Sale\n\t\t [B]. Donation\n\t\t "
"[C]. Inheritance\n\n\t>>> " ,
"abcABC" )
};
switch ( choice )
{
case 'a' : case 'A' : sale(); break ;
case 'b' : case 'B' : donation(); break ;
case 'C' : case 'c' : inheritance(); break ;
// default: break;
}
again = exitOrAgain();
}
return 0;
}
void mainMenu(const MonthDayYear& mdy)
{
std::cout << "\n\t >-----------------------<"
"\n\t\tComputerized Taxation\n\t\t\tFor\n\t\t Land Title Transfer\n"
"\t >-----------------------<\n"
"\t\t " << mdy.month << '/' << mdy.day << '/' << mdy.year
<< "\n\n\t\t Main Menu:\n" ;
}
char askForChar(const std::string& text, const std::string& options)
{
char ans {};
for (;;) { // forever; rely on break
std::cout << text;
std::cin >> ans;
if ( std::string::npos == options.find(ans) ) {
std::cout << "Unknown choice '" << ans << "'\nValid choices are: " ;
displaySpacedString(options);
std::cout << "Please, try again.\n\n" ;
} else {
break ;
}
}
return ans;
}
void displaySpacedString(const std::string& s)
{
for (auto c : s) {
std::cout << c << ' ' ;
}
}
bool exitOrAgain()
{
std::cout << "\n\nWould you like to?\n" ;
char choice {
askForChar( "[A].Exit Program\n[B].Back To Main Menu\n>>> " , "abAB" )
};
if (choice == 'a' || choice == 'A' ) {
std::cout << "\n\n\t\tThank You!\n\n" ;
return false ;
}
std::cout << "\n\n\t\t Back to Menu . . .\n\n" ;
return true ;
}
void sale()
{
std::cout << "\n\t\t\t\t>----SALE----<\n" ;
std::cout << "Enter the Net of Sale: " ;
double nos;
std::cin >> nos;
std::cout << "\nEnter the Market Value: " ;
double mv;
std::cin >> mv;
if (mv >= nos)
{
std::cout << "\n\t\t\t CGT...............PHP " << mv * .06
<< "\n\t\t\t Doc Stamp.........PHP " << (mv / 1000) * 15
<< "\n\t\t\t Transfer Tax......PHP " << nos * .02
<< "\n\t\t\t Gross Tax.........PHP "
<< (mv * .06) + ( (mv / 1000) * 15 ) + (nos * .02) ;
return ;
}
std::cout << "\n\t\t\t CGT...............PHP " << nos * .06
<< "\n\t\t\t Doc Stamp.........PHP " << (nos / 1000) * 15
<< "\n\t\t\t Transfer Tax......PHP " << nos * .02
<< "\n\t\t\t Gross Tax.........PHP "
<< (nos * .06) + ( (nos / 1000) * 15 ) + (nos * .02);
}
void donation()
{
std::cout << "\n\t\t\t\t>----DONATION----<\n" ;
std::cout << "\nEnter the Market Value: " ;
double mv;
std::cin >> mv;
std::cout << "\n\t\t\t CGT...............PHP " << mv * .06
<< "\n\t\t\t Doc Stamp.........PHP " << (mv / 1000) * 15
<< "\n\t\t\t DNT...............PHP " << (mv * .12)
<< "\n\t\t\t Transfer Tax......PHP " << mv * .07
<< "\n\t\t\t Gross Tax.........PHP "
<< (mv * .06) + ( (mv / 1000) * 15 ) + (mv * .12) + (mv * .07);
}
void inheritance()
{
std::cout << "\n\t\t\t\t>----INHERITANCE----<\n" ;
std::cout << "\nEnter the Market Value (PROPERTY 1): " ;
double mv1;
std::cin >> mv1;
std::cout << "Enter the Market Value (PROPERTY 2): " ;
double mv2;
std::cin >> mv2;
std::cout << "Enter the Market Value (PROPERTY 3): " ;
double mv3 = 0;
std::cin >> mv3;
double tmv = mv1 + mv2 + mv3;
std::cout << "\n\t\t\t Total Market Value = PHP " << tmv
<< "\n\t\t\t CGT...............PHP " << tmv * .06
<< "\n\t\t\t Doc Stamp.........PHP " << (tmv / 1000) * 15
<< "\n\t\t\t Estate Tax........PHP " << tmv * .20
<< "\n\t\t\t Transfer Tax......PHP " << tmv * .07
<< "\n\t\t\t Gross Tax.........PHP "
<< (tmv * .06) + ( (tmv / 1000) * 15 ) + (tmv * .20) + (tmv * .07);
}
Topic archived. No new replies allowed.