How do I code this in VB6. I almost got it already but I don't know how to put it all together. I am doing this in VB6.
If Incometxt >= 20962 Then StatusLbl = "Low-income class"
ElseIf Incometxt <= 20963 Then StatusLbl = "Lower middle-income class"
ElseIf Incometxt <= 41925 Then StatusLbl = "Middle middle-income class"
ElseIf Incometxt <= 73368 Then StatusLbl = "Upper middle-income class"
ElseIf Incometxt <= 125773 && = 2096200 Then StatusLbl = "Upper-income class"
Else Incometxt > 2096201 Then StatusLbl = "Rich"
I think the mistake happened on line 5 and 6 of the code I gave above.
Try this:
If Incometxt <= 20962 Then
StatusLbl = "Low-income class"
ElseIf Incometxt >= 20963 And Incometxt <= 41924 Then
StatusLbl = "Lower middle-income class"
ElseIf Incometxt >= 41925 And Incometxt <= 73367 Then
StatusLbl = "Middle middle-income class"
ElseIf Incometxt >= 73368 And Incometxt <= 125772 Then
StatusLbl = "Upper middle-income class"
ElseIf Incometxt >= 125773 And Incometxt <= 2096200 Then
StatusLbl = "Upper-income class"
Else
StatusLbl = "Rich"
End If
Related
I'm doing a tax calculator in C ++ but I have an error
It is supposed to enter several data: Type of vehicle, date and model.
But the program asks me only one before pause and does not go ahead.
It is assumed that the operation is as follows:
You enter a type of vehicle, then the date of issuance, and finally its price.
Given these data, the program is supposed to calculate the percentage of tax that must be included.
But as I said, the program is paused before asking the second data, the year.
This is the code
#include <iomanip>
#include <iostream>
using namespace std;
int main(){
std:string a;//tipo
int b;//año
double c;//precio
char d;//resultado
cout << "Ingrese el tipo:";
cin >> a;
cout << "Ingrese el año:";
cin >> b;
cout << "Ingrese el precio:";
cin >> c;
if (a = "automovil" && b <= 1980){
d = c*100/3.3;
}else if ( a == "automovil" && b <= 1990){
d = c*100/5.5;
}else if ( a == "automovil" && b <= 2000){
d = c*100/7;
}else if ( a == "automovil" && b <= 2010){
d = c*100/10;
}else if ( a == "camioneta" && b <= 1980){
d = c*100/3.3;
}else if ( a == "camioneta" && b <= 1990){
d = c*100/5.5;
}else if ( a == "camioneta" && b <= 2000){
d = c*100/7;
}else if ( a == "camioneta" && b <= 2010){
d = c*100/10;
}else if ( a == "camion" && b <= 1980){
d = c*100/3.3;
}else if ( a == "camion" && b <= 1990){
d = c*100/5.5;
}else if ( a == "camion" && b <= 2000){
d = c*100/7;
}else if ( a == "camion" && b <= 2010){
d = c*100/10;
}else if ( a == "volqueta" && b <= 1980){
d = c*100/3.3;
}else if ( a == "volqueta" && b <= 1990){
d = c*100/5.5;
}else if ( a == "volqueta" && b <= 2000){
d = c*100/7;
}else if ( a == "volqueta" && b <= 2010){
d = c*100/10;
}
cout << d;
return 0;
}
any suggestions?
Some issues with your code: to test for equality use the == operator (not the assignment = operator).
E.g.
if (a = "automovil" && b <= 1980)
...
else if ( a = "automovil" && b <= 1990){
Should be
if (a == "automovil" && b <= 1980)
...
else if ( a == "automovil" && b <= 1990){
And your last line expects you to write something to standard input. As was in the comments I think if you really wanted to test your calculated d value you should print it to standard output like so:
cout << d << endl;
Because what is currently happening is you are overwriting the d value that you calculated once you type something into standard input.
there are several error in your code, forst of all it seems unlikely that you should declare "a" as a char, from your code it seems like it should be instead a std::string. you should also note that the following code is wrong
if (a = "automovil" && b <= 1980){
you should use
if (a == "automovil" && b <= 1980){
There is a bunch of mistakes in your code:
First, you need to declare a and b as std:string and not char.
Second, you need to use == to compare two variables and not = which is the assignment operator.
Third, you need to cout << d; at the end of your program and not cin >> d;
Well, I want to make a function which have encoding and decoding function.
So, I studied about "rot-13 encoding" and solved it like this:
char* szTemp = "Hello World";
for (int i = 0; i < strlen(szTemp); i++)
{
if (szTemp[i] >= 'a' && szTemp[i] <= 'm') szTemp[i] += 13;
else if (szTemp[i] >= 'A' && szTemp[i] <= 'M') szTemp[i] += 13;
else if (szTemp[i] >= 'n' && szTemp[i] <= 'z') szTemp[i] -= 13;
else if (szTemp[i] >= 'N' && szTemp[i] <= 'Z') szTemp[i] -= 13;
}
MessageBox(szTemp);
But it have some error. What is it? Anyone help me!
In MFC, it's all about the CString...
CString sTemp = "Hello World";
CString sResult = "";
int nLength = sTemp.GetLength();
char c;
for ( int i = 0 ; i < nLength ; ++i )
{
c = sTemp[i];
if (c>= 'a' && c<= 'm') c+= 13;
else if (c>= 'A' && c<= 'M') c+= 13;
else if (c>= 'n' && c<= 'z') c-= 13;
else if (c>= 'N' && c<= 'Z') c-= 13;
sResult += c;
}
AfxMessageBox( sResult );
It can also be done by accessing the buffer directly, in which case, you can use almost all of your original code. It looks something like this:
CString sTemp = "Hello World";
int nLength = sTemp.GetLength();
// Limit scope of szTemp since it is not usable after
// the call to ReleaseBuffer
{
char* szTemp = sTemp.GetBuffer();
for (int i = 0; i < nLength; i++)
{
if (szTemp[i] >= 'a' && szTemp[i] <= 'm') szTemp[i] += 13;
else if (szTemp[i] >= 'A' && szTemp[i] <= 'M') szTemp[i] += 13;
else if (szTemp[i] >= 'n' && szTemp[i] <= 'z') szTemp[i] -= 13;
else if (szTemp[i] >= 'N' && szTemp[i] <= 'Z') szTemp[i] -= 13;
}
sTemp.ReleaseBuffer();
}
AfxMessageBox( sTemp );
Hope that helps,
D*
Can anyone please explain me how this wrapping of chars between a-to-z and A-to-Z happening in Caesar shift code?
k %= 26;
for(int i = 0; i < n; i++){
int c = s[i];
if(c >= 'a' && c <= 'z'){
c += k;
if( c > 'z'){
c = 96 + (c % 122); // wrapping from z to a?
}
}
else if(c >= 'A' && c <= 'Z'){
c += k;
if(c > 'Z'){
c = 64 + (c % 90);
}
}
cout << (char)c;
}
K is amount of shift and c is a char of string s.
Is there any better way to do the same?
Lets make a couple changes to the code and it is easier to see what is going on
for(int i = 0; i < n; i++){
int c = s[i];
if(c >= 'a' && c <= 'z'){
c += k;
if( c > 'z'){
c = 'a' + (c % 'z') - 1; // wrapping from z to a?
}
}
else if(c >= 'A' && c <= 'Z'){
c += k;
if(c > 'Z'){
c = 'A' + (c % 'Z') - 1;
}
}
cout << (char)c;
}
So in c = 'a' + (c % 'z') - 1; if c is larger than z then we mod c by z(122) to get how many characters from a we need to go. The same thing is going on with the upper case letters. I am subtracting one here as we are starting at a instead of the character before a like you original code does.
I am relatively new to programming; I am in an intro to c++ course in college.
How would I go about returning a single char from a function back to the main? I haven't seen any posts that answer this question simply enough for me to understand.
Thanks in advance!
For those interested, this is the code that I wrote that caused me to ask this question.
(NOTE: My prof tells us to include cstdlib and iostream in all programs, and to put using namespace std before the main. I already saw several posts that say not to use this, but this is how my prof has us write our programs.)
char evaluate(int score){
/*receives an int score out of 100 from main and determines what letter grade to return */
if (score >= 90 && score <= 100)
return "A";
else if (score >= 80 && score <= 89)
return "B";
else if (score >= 70 && score <= 79)
return "C";
else if (score >= 65 && score <= 65)
return "D";
else
return "F";
}
When I do this, I get "error: invalid conversion from 'const char*' to 'char' [-fpermissive]"
Any ideas why?
"A" is a string literal. What you meant is 'A'.
return "A";
will return type const char*
You should rather use :-
return 'A'
to return a character.
Alternatively, you can eliminate multiple return statements by doing this:
char evaluate(int score)
{
char grade;
if (score >= 90 && score <= 100)
grade = 'A';
else if (score >= 80 && score <= 89)
grade = 'B';
else if (score >= 70 && score <= 79)
grade = 'C';
else if (score >= 65 && score <= 65)
grade = 'D';
else
grade = 'F';
return grade;
}
As others have mentioned, you should use single quotes. E.g. 'A' instead of double quotes. Use double for strings. Single quotes for char.
you can do
char evaluate(int score){
char returnValue = 'F';
/*receives an int score out of 100 from main and determines what letter grade to return */
if (score >= 90 && score <= 100)
returnValue = 'A';
else if (score >= 80 && score <= 89)
returnValue = 'B';
else if (score >= 70 && score <= 79)
returnValue = 'C';
else if (score >= 65 && score <= 65)
returnValue = 'D';
return returnValue;
}
if(location[0] <= 'H' || location[0] >= 'A'
&& location[1] <= 8 || location[1] >= 1)
I am checking to see if the first indice is between 'A' and 'H' and the second indice is between 1 - 8.
so the argument would = 1 if the user entered { A, 1 }
-1 if the user entered { J, 1 }
And (&&) has higher precedence than or (||), so to express what you want you need:
if ((location[0] <= 'H' || location[0] >= 'A') && (location[1] <= 8 || location[1] >= 1))
Otherwise what's happening is that it does the equivalent of this:
if (location[0] <= 'H' || (location[0] >= 'A' && location[1] <= 8) || location[1] >= 1)
Which is clearly not what you intended.
You can put as many expressions in the condition as you want, but you must remember the precedence rules; I suggest always using extra parentheses for clarity.
As Jeremiah has pointed out in the comments, the condition still wouldn't be doing what you wanted -- you are asking if something is in a given range by checking if it's either less than the upper bound or greater than the lower bound (this covers all input), when you intended to check whether the input was both larger than the lower bound and smaller than the upper:
if (location[0] <= 'H' && location[0] >= 'A' && location[1] <= 8 && location[1] >= 1)
In this case, since the operators are all &&, no extra parentheses are needed, though you can add them for extra clarity.
You need &&s not ||s. For example: 'Z' >= 'A' || 'Z' <= 'H' is true.
if(location[0] <= 'H' && location[0] >= 'A' && location[1] <= 8 && location[1] >= 1)
While not necessary in this case, you should group your logic together with parenthesis (and if they were ||s you would have to for it to work as expected):
if((location[0] <= 'H' && location[0] >= 'A') && (location[1] <= 8 && location[1] >= 1))
Add parenthesis to group the conditions:
if( (location[0] <= 'H' || location[0] >= 'A') && (location[1] <= 8 || location[1] >= 1))
If location[0] was equal to 'J' (definitely NOT between 'A' and 'H'), your if-clause would be TRUE because even though location[0] <= 'H' is FALSE, location[0] >= 'A' is TRUE and you are using || (OR). The problem is that you are using || (OR) instead of && (AND). You need to use all && (AND) in the if-clause.
&& has higher precedence than ||. So, you may want to write it as
if((location[0] <= 'H' || location[0] >= 'A') && (location[1] <= 8 || location[1] >= 1))
If you already tried this, please tell what error you got
Sorry guys, new here. I should have posted my entire method. Thank you for explaining the precedence of the operators, but my issue was that I was comparing a char with an int. All I needed to do was put ' ' around my numbers.
void GameBoard::enterShips()
{
char location[2];
int ships = 0;
int count = 1;
while( ships < NUM_SHIPS )
{
cout << "Enter a location for Ship " << count << ": ";
cin >> location[0];
cin >> location[1];
cout << endl;
if((location[0] <= 'H' && location[0] >= 'A') && (location[1] <= '8' && location[1] >= '1'))
{
location[0] = location[0] - 64;
location[1]--;
Grid[location[0]][location[1]] = SHIP;
ships++;
count++;
}
else
{
cout << "Wrong coordinates entered. Try again." << endl << endl;
}
}
}
As you can see in this operator precedence table, && is #13. It's higher up than ||, which is #14.
Therefore:
if(location[0] <= 'H' || location[0] >= 'A'
&& location[1] <= 8 || location[1] >= 1)
Is equivalent to:
if(location[0] <= 'H' || (location[0] >= 'A'
&& location[1] <= 8) || location[1] >= 1)
You want:
if((location[0] <= 'H' || location[0] >= 'A') &&
(location[1] <= 8 || location[1] >= 1))
Or even better:
if(((location[0] <= 'H') || (location[0] >= 'A')) &&
((location[1] <= 8) || (location[1] >= 1)))
I always put brackets around everything except when */ and +- are concerned. Even if you memorize the table, you shouldn't assume others do.
It helps code readability and helps prevent bugs... even compiler bugs! Triple bonus. :)
EDIT: Ah, you want ALL of them to be true. Just use all &&s, then! This works:
if(((location[0] <= 'H') && (location[0] >= 'A')) &&
((location[1] <= 8) && (location[1] >= 1)))