tax calculator pause program - c++

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;

Related

Why does it display the sum if numbers aren't between the 2 values?

I want it to not display the result of the sum if the numbers are lower or equal to 1 or 1000. I don't know if using if is the best way, but that's what I tried using, and I don't really understand why it doesn't work. I also tried writing conditions with || and &&, but those don't work either.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int sum;
int a, b, c;
int main() {
cin >> a;
cin >> b;
cin >> c;
sum = a + b + c;
if ( 1 <= a, b, c <= 1000) { //also tried ( 1 <= a || b || c <= 100) and ( a, b, c >= 1 && a, b, c <= 1000)
cout<< sum;
}
else {
cout<< "can't calculate";
}
return 0;
}
The expression in this if statement
if ( 1 <= a, b, c <= 1000)
is an expression with the comma operator. It is equivalent to
if ( ( 1 <= a ), ( b ), ( c <= 1000 ) )
and the value of the expression is the value of its last operand. That is this if statement is equivalent to
if ( ( c <= 1000 ) )
It seems you mean
if ( 1 <= a && a <= 1000 && 1 <= b && b <= 1000 && 1 <= c && c <= 1000 )
{
std::cout << a + b + c << '\n';
}
Pay attention to that there is no sense to calculate the sum
sum = a + b + c;
before the checking the values of the variables a, b and c in the if statement.
Why do you have so much includes?
Your code just need iostream. Anything else could be removed.
Your if-condition doesn't have the right syntax. You can't write "a and b and c should be under 1000", you must do it for every var. Try:
a >= 1 && a <= 1000 &&
b >= 1 && b <= 1000 &&
c >= 1 && c <= 1000
You probably want this:
if ( a >= 1 && b >= 1 && c >= 1 &&
a <= 1000 && b <= 1000 && c <= 1000 )
{
std::cout << a + b + c;
}
Your code is not correct. You have to use the correct syntax.
Note: see Why is "using namespace std;" considered bad practice?

Infinite loop in do while

I'm new in C++.
I've got a problem with Yi function in my code.
My do while loop in Yi function is never breaking.
I don't know where is the problem. I guess, It's in the bool value, but not sure.
111111111111111111111111111111111111111111111111111111111111222222222222222222233333333333333333333333333333333444444444444444444444444444444444444444444444
there are no more details.
#include "pch.h"
#include <iostream>
#include <iomanip>
#include <limits>
#include <cmath>
using namespace std;
double Yi(double&, double&, double&, int&, int&);
double Yi(double &f, double &a, double &b, int &i, int &n) {
float amin, amax, bmin, bmax, da, db;
bool z = true;
do { cout << "input amin, amax, da" << endl;
cout << "input bmin, bmax, db" << endl;
cout << "Input n" << endl;
while (!(cin >> amin) || !(cin >> amax) || !(cin >> da) || !(cin >> bmin) || !(cin >> bmax) || !(cin >> db) || !(cin >> n)) {
cout << "You have entered wrong input. Input values again: " << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
i = 1;
if ((amax > amin && da > 0) && (bmax > bmin && db > 0) && (n>=i))
{ for (a = amin; a < amax; a += da);
for (b = bmin; b < bmax; b += db);
for (i; i < n; i++);
}
float f1, f2;
if (a == 0)
{
cout << "***********************************************" << endl;
z;
}
if (a > 0)
{
f1 = (a * i + 2 * b) * (a * i + 2 * b) + pow(-1, i)*i;
f2 = sqrt(i*i + i);
f = f1 / f2;
z = false;
}
if (a < 0)
{
f1 = (a * i + 2 * b) * (a * i + 2 * b) + i;
f2 = sqrt(i*i - i + 1);
f = f1 / f2;
z = false;
}
} while (z);
return f;
}
What should I change for working code?
Thank you in advance. :)
I'm not even sure what your Yi function does, however, the boolean value in z is only changed in case a becomes different than zero. Therefore, we may assume a never becomes less neither greater than 0, so your code never enters the block provided by this if if ((amax > amin && da > 0) && (bmax > bmin && db > 0) && (n>=i)) or if it does, this block of code for (a = amin; a < amax; a += da); doesn't execute so a is not less than amax or a is increased until it reaches 0. In case the block headed by the following if if ((amax > amin && da > 0) && (bmax > bmin && db > 0) && (n>=i))
is never executed, a already is set to 0.
Usually one shouldn't copy and paste a code and ask people to fix it, however, since you are new to the forum and also new to c++ programming,I have tried to help you anyway. Also note, it is a good idea to post sample input and output to make it easier to identify the problem. Cheers.

Renaming filenames in two directories IF certain characters between them match - vector subscript out of range

My first job as an intern was to write a program to compare certain characters in the filenames of two different directories, and if they match, rename them. I wrote a custom code to match the characters. The initial few files get renamed in both directories, but it breaks after a point, giving a vector subscript out of range error.
I have an idea of how to fix such a vector range error from all the other posts, but nothing seemed to work. Any input would be appreciated!
PS: I am not a coder and this is my third official program. I understand the code is a bit messy.
Here is the code:
#include<dirent.h>
#include<vector>
#include<sstream>
int main()
{
cout << "Comparer - Renamer v.0.1.beta\n\n";
string dr1, dr2;
int x, y;
DIR *d1;
struct dirent *dir1;
vector<string> a;
a.reserve(25000);
int i = 0;
cout << "Enter the first directory (format : log_2017...) : ";
cin >> dr1;
d1 = opendir(dr1.c_str());
if (d1){
while ((dir1 = readdir(d1)) != NULL){
i++;
a.push_back(dir1->d_name);
}
closedir(d1);
}
x = a.size();
cout << "\nEnter the second directory (format : 2017.12...) : ";
cin >> dr2;
DIR *d2;
struct dirent *dir2;
vector<string> b;
b.reserve(25000);
int j = 0;
d2 = opendir(dr2.c_str());
if (d2){
while ((dir2 = readdir(d2)) != NULL){
j++;
b.push_back(dir2->d_name);
}
closedir(d2);
}
y = b.size();
ostringstream osa, nsa, osb, nsb;
string oldname_a, newname_a, oldname_b, newname_b;
int u, v, w;
for (int l = 2; l < x; l++){
for (int k = l; k < y; k++){
int c = a[l][20] * 10 + a[l][21];
int d = b[k][14] * 10 + b[k][15];
int e = a[l][17] * 10 + a[l][18];
int f = b[k][11] * 10 + b[k][12];
if (a[l][4] == b[k][0] && a[l][5] == b[k][1] && a[l][6] == b[k][2] && a[l][7] == b[k][3] && a[l][9] == b[k][5] && a[l][10] == b[k][6] && a[l][12] == b[k][8] && a[l][13] == b[k][9]){
u = 0;
}
else{
u = 1;
}
if ((e - f) == 0 && abs(c - d) < 12){
v = 0;
}
else{
v = 1;
}
if ((e - f) == 1 && ((c == 58) || (c == 59) || (c == 0) || (c == 1) || (c == 2))){
w = 0;
}
else{
w = 1;
}
if (u == 0 && (v == 0 || w == 0)){
osa.str(string());
osa << dr1 << "\\" << a[l];
nsa.str(string());
nsa << dr1 << "\\" << l - 1 << ". " << a[l];
oldname_a = osa.str();
newname_a = nsa.str();
osb.str(string());
osb << dr2 << "\\" << b[k];
nsb.str(string());
nsb << dr2 << "\\" << l - 1 << ". " << b[k];
oldname_b = osb.str();
newname_b = nsb.str();
rename(oldname_a.c_str(), newname_a.c_str())
rename(oldname_b.c_str(), newname_b.c_str())
break;
}
}
}
return 0;
}
Presently the code is set such that it shows me how the comparison between the filenames is made.
It turns out I was not debugging properly, and the problem was in this part of the code:
int c = a[l][20] * 10 + a[l][21];
int d = b[k][14] * 10 + b[k][15];
int e = a[l][17] * 10 + a[l][18];
int f = b[k][11] * 10 + b[k][12];
I did not know that I couldn't assign an integer from a string/char directly to an int. I converted the char to int (which would give me the ASCII value of the char) and then subtracted it by 48 to convert it to decimal (I do not know if there is an easier way to do this, but this seemed to have worked for me!) The modified part looks like this:
c = ((int)a[l][20] - 48) * 10 + ((int)a[l][21] - 48);
d = ((int)b[k][14] - 48) * 10 + ((int)b[k][15] - 48);
e = ((int)a[l][17] - 48) * 10 + ((int)a[l][18] - 48):
f = ((int)b[k][11] - 48) * 10 + ((int)b[k][12] - 48);
There was also a small manual error in the conditions, which I also rectified.

Why doesn't this output "Please try again" when loop runs again?

Hello I am trying to learn c++ and I wanted to give a little practice with a program. However I'm having trouble using cout within the loop.
This is the loop I'm trying to output text from. When the user enters a number that isn't valid it is supposed to say "Sorry try again!"
while (datecheck)
{
bool check(false);
if (check)
std::cout<<"Sorry try again!"<<std::endl;
std::cin>>c;
if (c >= 1)
{
if (b == 2 && c <= 28)
datecheck = false;
if (b == 2 && a % 4 == 0 && c <= 29)
datecheck = false;
if (b == 4 || b == 6 || b == 9 || b == 11 && c <= 30)
datecheck = false;
if (c <= 31)
datecheck = false;
}
check = true;
}
When it outputs and I purposely keep myself in the loop it doesn't output anything
Year: -20
-20
-20
You declare a fresh new variable check at every iteration. And you initialize that variable to false every time. So move that declaration before the while loop.
Change this:
while (datecheck)
{
bool check(false);
...
check = true;
}
to this:
bool check(false);
while (datecheck)
{
...
check = true;
}
The problem is with declaration of bool check(false);. This keeps on re-assigning value to false at beginning of each iteration.
A simple fix could be to get-rid of use of check variable and use only datecheck.
bool datecheck(true);
while (true)
{
std::cin>>c;
if (c >= 1)
{
if (b == 2 && c <= 28)
datecheck = false;
if (b == 2 && a % 4 == 0 && c <= 29)
datecheck = false;
if (b == 4 || b == 6 || b == 9 || b == 11 && c <= 30)
datecheck = false;
if (c <= 31)
datecheck = false;
}
if (datecheck)
{
std::cout<<"Sorry try again!"<<std::endl;
}
else
{
break;
}
}

C++ Multiple Logical Operator

I'm rather new to C/C++. I have a segment of my application which doesn't seem to work as I'd want but I cannot understand why.
What I'm looking to do is when the 4 key is in the status of down, I'd like it to carry out the 'idle' function. I'd like the idle function to have 2 outcomes.
If the Up OR Down OR Left OR Right OR LMouse AND RButton then carry out the 'movement rotation operation' code else just carry out the standard idle function.
However within my code, it'll loop this while it's down but the moving() will only ever return 0
I've been messing with it for some time and trying to look on google for answers but I cannot understand why.
Here's my segment of code:
int moving()
{
int u = GetAsyncKeyState(VK_UP);
int d = GetAsyncKeyState(VK_DOWN);
int l = GetAsyncKeyState(VK_LEFT);
int r = GetAsyncKeyState(VK_RIGHT);
int mr = GetAsyncKeyState(VK_RBUTTON);
int ml = GetAsyncKeyState(VK_LBUTTON);
if(u == 1 || d == 1 || l == 1 || r == 1 || mr == 1 && ml == 1)
{
return 1;
}
}
void idle()
{
cout << "moving = " << moving() << endl;
if(moving() == 1)
{
cout << "Movement rotation operating." << endl;
}
else
{
cout << "This is the idle statement" << endl;
}
}
int main()
{
while(1)
{
if(GetAsyncKeyState('4'))
{
cout << "4 Pressed" << endl;
idle();
}
}
}
Thank you in advance.
Your logic to determine the button combination needs an extra set of parentheses.
if(u == 1 || d == 1 || l == 1 || r == 1 || (mr == 1 && ml == 1))
Also, 1 will evaluate to true so you can say
if(u || d || l || r || (mr && ml))
You could also make the function return a bool since that is really what you're after.
bool moving()
{
// ...
// code for getting button states
// ...
return (u || d || l || r || (mr && ml))
}