Presentation error in C++ - c++

I am facing an error, that is a presentation error. Being a beginner to c++ , I am stuck on the following question:
Two cars (X and Y) leave in the same direction. The car X leaves with a constant speed of 60 km/h and the car Y leaves with a constant speed of 90 km / h.
In one hour (60 minutes) the car Y can get a distance of 30 kilometers from the X car, in other words, it can get away one kilometer for each 2 minutes.
Read the distance (in km) and calculate how long it takes (in minutes) for the car Y to take this distance in relation to the other car.
Input:
30
Output:
60 minutos (minutes in portuguese)
Now, upon submitting the code it says presentation error. Could someone help me find a solution to this error. Thank you in advance.
My code:
#include <iostream>
using namespace std;
int main(){
int Y;
cin >> Y;
cout << 2*Y << " minutos " << endl;
return 2*Y;
}

PE is a common error in OJ of ACM. you can check space , newline charactor or something you missed. for example:
#include <iostream>
using namespace std;
int main(){
int Y;
cin >> Y;
cout << 2*Y << " minutos (minutes in portuguese)" << endl;
return 0;
}
you can have a try, good luck for you.

To fix your problem try to return zero as the main function should return it, if your code is clear from errors.
Try to follow that convention to always return 0 in main.
Your code should look like this:
#include <iostream>
using namespace std;
int main()
{
int Y;
cin >> Y;
cout << 2*Y << " minutos " << endl;
return 0;
}
To conclude simply use cout instead of return.

Related

calculate x years for savings with deposition and revenue C++

I'm sitting with a challenging homework in C++ and would be really thankful for some help here!
My program need to calculate how many years it will take for an optional yearly deposition with revenue to reach a specific savings limit.
I just can't see what's wrong and have tried debugging with no help.
It doesn't really help neither that i'm totally new to C++ and MVS 2015.
I don't know if it's the math or the programming itself that is wrong.
Static typing is foreign to me since I usually use python.
Also VS don't give much information and the program just stops after asking for revenue input.
Any suggestions?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
int deposit;
int max_savings;
double revenue;
double change_factor;
double year = 0;
double geometric_sum;
cout << "Choose a yearly deposition:\n";
cin >> deposit;
cout << "Set your max saving-goal:\n";
cin >> max_savings;
cout << "set a revenue in percent:\n";
cin >> revenue;
change_factor = 1 + (revenue / 100);
geometric_sum = ((double)deposit * (pow(change_factor, year) - 1)) / (change_factor - 1);
while (geometric_sum < max_savings)
year++;
cout << "Your saving-goal will be in " << year << " years!" << endl;
cout << "Your account balance will then be " << geometric_sum << " dollars!" << endl;
return 0;
}
pow(change_factor, year) - 1
year is set to 0. Any value at the power of 0 is 1. 1 - 1 = 0. Basically you are multiplying with 0.

setprecision() not working as expected

I was doing a program which first takes 2 numbers (with float datatype) from the user and then ask the user about up-to what digit he want's to get the number divided and finally divides it up-to that number and 'cout<<' it. It compiled but din't worked up-to the mark when I calculated 22/7 which is an irrational no. up-to 100 digits it just calculated up-to 30 or 40 digits and then rest of was filled with zeros. Something like this:
3.1428570747375488281250000000000000000000000000000000000000000000000000000000000000000000000000000000
Here is my code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
system("clear");
float y;
int z;
float x;
float a;
cout << "\nHello User\n";
cout << "\nEnter first num to be divided: ";
cin >> x;
cout << "\nCool!! Now enter the 2nd number: \n";
cin >> y;
cout << "\Exelent!! Enter the place upto which u wanna caculate: ";
cin >> z;
a = x / y;
cout << fixed << showpoint;
cout << setprecision(z);
cout << "Calculating......\n" << a << endl;
return 0;
}
Floating point types have certain precision. You don't get exact results when operating on floats (or doubles). Now to get a better precision use double instead of float (See this post for more details).
You could #include <limits>, remove the step that gets the precision from input and change your code to:
std::cout << std::setprecision(std::numeric_limits<float>::max_digits10);
to display the result with maximum precision for the type you use.

C++ Beginner, first program. finding velocity and distance

Hey I'm a beginner taking an intro to C++ class, and this is my first assignment to use the formulas: d=v0*t + 1/2*g*t^2, and v= v0 + g*t. where v0 stays constant at 0 and g also stays constant at 9.807 m/s^2. I keep getting these errors and cannot seem to fix them, and im sure this code is incorrect, so can you help me figure this out?
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
const float GRAVITY = 9.807, INITIALVELOCITY = 0;
int time;
void gettime()
{
cout << "Please enter the time in seconds." << endl;
cin >> time > endl;
} //end function Time
int main(int argc, char *argv[])
{
float distance, velocity, time;
void getTime(void);
cout.setf (ios::fixed,ios::floatfield);
cin >> time;
while (time > 0) {
distance = INITIALVELOCITY * time + (0.5 * GRAVITY * pow(time, 2));
velocity = INITIALVELOCITY + (GRAVITY * time);
cout.precision (0);
cout << endl << "WHEN THE TIME IS" << time << "SECONDS THE DISTANCE"
"TRAVELED IS" << distance << "METERS THE VELOCITY IS" << velocity <<
"METERS PER SECOND.";
cout. precision(1);
cout<< time << distance << velocity << endl << endl;
}
system ("PAUSE");
return EXIT_SUCCESS;
} //end main
The name time of the global variable is used as the name of one of standard library function, so you have to give another name to the variable.
remove junk > endl after cin >> time in gettime().
At least this will make the code compilable.
Then, inputting positive value will lead to infinite loop.
UPDATE: I think removing the global variable int time; and function gettime() is good because they are causing trouble and aren't used.
In addition to the problems mentioned by MikeCAT, you are not actually calling the gettime function. The line void gettime(void); is a function declaration, not a function call. Get rid of the void's: gettime();
After that, you may see you have a line cin >> time;. This accepts user input, but it has no prompt, so it would look like the program is doing nothing.
As far as getting the time, it's better if you have gettime() return an int: int gettime() and not have time declared as a global variable.

Any Idea why this if statment isn't working? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I know it's probably a simply mistake, but I am rather new at this. Any idea why this might not be working.
#include <iostream.h>
#include <conio.h>
int main()
{
float x, y;
cout << "Please enter the miles traveled:", x;
cin >> x;
if (x <= 0)
cout << "Invalid number please try again.";
if (x > 1) {
y = ((x * .75) + 2);
cprintf("Your total amount owed is $%.2f"), y;
}
getch();
return 0;
}
First, you have some minor mistakes in your program.
In the line
cprintf("Your total amount owed is $%.2f"), y;
you should put the y inside the function call, i.e.
cprintf("Your total amount owed is $%.2f", y);
Also, the , x in the line
cout<< "Please enter the miles traveled:", x;
should be removed. Both , x and , y in the above quoted lines have no effect at all.
Then, you should add the return type int to your main() function:
int main() {
...
}
Finally, since you are using cin and cout from the namespace std, you should either write std::cin and std::cout respectively, or put a using namespace std; after your includes, or put using std::cin; and using std::cout; after the includes (the last option is recommended).
You also shouldn't #include <iostream.h> (I don't know that one, it is non-standard) but rather <iostream>, and I guess if you change that, explicitly mentioning the namespace, like in the above point, is then required.
So far, the program should "work", but I guess not in the way you want it to behave. You probably want the program to repeat after the user made an input error. For this, you should use a loop. Something like this should do the job:
cin >> x;
while (x <= 0)
{
cout << "Invalid number please try again.";
cin >> x;
}
Also, what should your program do if the user enters a number between 0 and 1? I guess instead of x > 1 you want x > 0, i.e. just the opposite of the "error case" x <= 0. This is usually expressed with an else block. But if you write a loop repeating until the user entered a positive x, you can be sure that it is really positive after the loop.
When declaring or defining a function it's required to have a type, be it void or auto while auto is, strictly-speaking, no type. A strictly-conforming main function is int main() or int main(int, char**).
This line
cout<< "Please enter the miles traveled:", x;
looks dubious. What's the x supposed to do here? Remove the , x part.
This line
cprintf("Your total amount owed is $%.2f"), y;
is bad. Pass y inside the parantheses, like
cprintf("Your total amount owed is $%.2f", y);
cout etc. are from the std namespace. Either add a std:: before all of them or put a using namespace std; before main.
This line
y=((x*.75)+2);
isn't necessarily wrong but it multiplies a float with a double, killing off precision. Use .75f instead.
float y is only used in one if-clause, so narrow down its scope to that.
<iostream.h> isn't guaranteed to exist by the C++ standard. It's <iostream>.
Remove that <conio.h> inclusion. That's wicked because non-standard! Read this instead.
Proceeding with the 8th point, don't use all that non-standard C-ish crap. C++ is not C!
int main()
you haven't mentioned return type of main()
cprintf("Your total amount owed is $%.2f", y);
remove
getch()
and
cout<< "Please enter the miles traveled: " << x;
EDIT
Also with gcc you cannot use conio.h
main, being a function, needs to return type int (void too will work in some systems). The x and the y following the cout/cprintf statements, should be removed. You may also want to enclose the content of the first if statement into a while loop, until the desired condition becomes true.
#include <iostream>
using namespace std;
int main()
{
float x, y;
cout << "Please enter the miles traveled: ";
cin >> x;
while (x <= 0.f) {
cout << "Invalid number please try again.";
cin >> x;
}
y = ((x*.75) + 2);
cout << "Your total amount owed is : " << y;
return 0;
}

c++ how to have a user input equation to be evaluated

My program is meant to ask a user to input an equation. Then find the maximum over an interval given by the user. When I compile my program, the output I get is:
Please complete the equation to be evaluated f(x)=
Please enter the first number of the interval to be checked:
Please enter the last number of the interval to be checked:
Please enter the desired initial step size:
sh: PAUSE: command not found
with the last line repeating many times.
I think the problem here has something to do with having the user input the equation to be tested. However, I'm unsure of how to fix this.
Here's my code
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
int a, b, delta, fx, x, y;
int max = 0;
cout <<"Please complete the equation to be evaluated f(x)= " << endl;
cin >> fx;
cout <<"Please enter the first number of the interval to be checked: " << endl;
cin >> a;
cout << "Please enter the last number of the interval to be checked: " << endl;
cin >> b;
cout << "Please enter the desired initial step size: " << endl;
cin >> delta;
for(x = a; x <= b; x = x+delta)
{
y = fx;
if (y > max)
{
max = y;
cout <<"The maximum over the interval from " << a <<"to " << b <<"is " << delta;
}
else
{
delta= delta/2;
}
if (delta < pow( 10, -6))
{
system ("PAUSE");
}
}
return 0;
}
F(x) shouldn't be an integer variable, it should be a string variable. That way, the user can enter operators as characters instead of the compiler thinking they should be numbers. You would then have to process the string to determine the equation; this would require some thought, and possibly a more advanced data structure such as a binary tree.
Simply don't use system("pause"); in the if statement and you'll lose that error:
"sh: PAUSE: command not found". Place it right before the end of the main.
system("pause");
return 0;
As pointed out by others, the form of f(x) could be an issue with the above code.
Consider to redesign what to achieve for your program. One possibility is to narrow down the f(x) as polynomial function so that you can avoid parsing general algebraic equation, in this case you can ask:
how many degree of the polynomial ? upon this, it is followed by input the coefficient value for each factor in the polynomial equation.
This way, you can still use integer (or double - better) in the program.