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;
}
Related
I am instructed to write a program where the user inputs two values, can be with decimals. Then create a void function that changes the precision to 4 digits. I created a program and it runs the first value (x) just fine but for some reason, it's giving me an error saying the second variable is uninitialized (h) any advice would be appreciated! I think that I've been looking at it for too long and just can't spot it!
Below is the code:
#include <iostream>
#include<cmath>
using namespace std;
void display_it(double x, double h);
int main(void) // getting input from user and displaying output
{
double x, h;
cout << "Please enter 2 values to be displayed with precision.\n";
cin >> x, h;
cout << x << endl;
cout << h << endl;
return 0;
}
void display_it(double x,double h) //does the precision change of x and h
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(4);
}
This line:
cin >> x, h;
is not reading in 2 values from cin. It's actually only reading in 1 value, after which the expression h that's after the , is evaluated (which does nothing). So the warning/error about h being uninitialized is correct.
The correct way to read 2 values is:
cin >> x >> h;
I'm trying to understand something. I'm still a beginner to c++ and I just created this little program where you input a value and it tells you whether it's even or odd. To do this, I made an integer called "result" which takes value, and then does % 2 operation.
However, my first mistake was that I put int result above "cin >> value" so for some reason that messed up the program and the number would always be even no matter what. Then when I put int result below "cin >> value" the program worked like it should. Why is it doing this?
Any help would be appreciated thank you. I apologize if this is a duplicate but I don't know what to search for.
#include <iostream>
#include <string>
#include "Human.h"
#include <ctime>
using namespace std;
int main() {
int value = 0; // where I input
cin >> value;
// if you put int result above cin program changes.
int result = value % 2;
if (result == 0) {
cout << "Even number." << endl;
}
else {
cout << "Odd number." << endl;
}
return 0;
}
Any code whichever programming language you use runs from top to bottom.
You need to first declare the variable, give it a value and then check for being even or odd.
When you used cin after setting the value of result = value%2; the compiler used the originally initialized value for value which is 0 to compute the value of result which will be 0%2.
That's why you need to use cin>>value; before setting result = value%2;.
C++ read the code top to bottom , line by line. So you will have to int your variable first.I made a much more simpler version of the program if you want to read it:
#include <iostream>
using namespace std;
int main() {
int a;
cout << "a=";
cin >> a ;
if(a%2==0)
{cout<<"a is even";}
else
{cout<<"a is uneven";}
}
When you put int result = value % 2; before cin >> value;, your program will calculate the result before you put a value inside int value via your input.
So your program does calculate int result = 0 % 2;
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.
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.
I have made a simple program in C++ and this is the code:
#include <iostream>
using namespace std;
int main()
{
int number;
int square;
number = 5;
square = number * number;
cout << "The square is ";
cout << square;
return 0;
}
what it does is basically taking the integer "5" and get the square value on the screen and so on...
my question is:
how can I make the program take any value from the user instead of storing a value in the memory?
than Q.
Your code makes use of cout to print. C++ makes cin available for input from the console:
int x;
cin >> x;
"An example is worth a thousand words..."
Well cout takes some var. from memory and prints it out on the screen, right?
Well, cin does the exact opposite, it takes in some value from the keyboard and puts it in your memory..
You have to take in the value with the help of cin command, like this:
int a; //lets say you have a variable
cout << "Enter a value here: "; //prompts the user to enter some number
cin >> a; //this line will allow the user to enter some value with the keyboard into this var.
int square = a * a;
cout << "The square is: " << square;
Hope it helps...
Just replace:
number = 5;
with:
cout << "What's the number? ";
cin >> number;
You already know how to use cout to generate output, this simply uses cin to retrieve input.
Keep in mind that, while this may be okay for small test programs or learning, data input in real programs tends to be a little more robust (such as if you enter the string xyzzy when it's trying to input an int variable).