Sign in to follow this  

Help me debug my code.

This topic is 4731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Some errors, and I'm not sure whats wrong. /*Write a single if statement that examines two integer values and changes the larger to the smaller, using only one else clause.*/ #include <iostream> int main () { int x, y; cout << "Please enter two numbers.\n"; //giving error on this line cout << "First: \n"; cin >> x; //and this line cout << "Second: \n"; cin >> y; if (x > y) x = y; else y = x; cout >> "X: " >> x; return 0; System("pause"); //and here }

Share this post


Link to post
Share on other sites
#include <iostream>
using namespace std; // <- important, and most likely what the compiler wants.

using namespace std, in simplified terms, explains where cout and cin are. That is, a part of std (standard.)

[edit]

Also, cout >> "X: " >> x; has the wrong operator. You want cout << "X: " << x;

Share this post


Link to post
Share on other sites
#include <iostream>
using namespace std;
int main ()

{
int x, y;

cout << "Please enter two numbers.\n"; //giving error on this line
cout << "First: \n";
cin >> x; //and this line
cout << "Second: \n";
cin >> y;

if (x > y)
x = y;

else
y = x;

cout << "X: " << x;

cout<<"Press any key to quit";
string exit;
cin>>exit; cout<<"Thanks";

return 0;
}

Share this post


Link to post
Share on other sites
Revised code



/*Write a single if statement that examines two integer values and changes the
larger to the smaller, using only one else clause.*/

#include <iostream>
using namespace std;

int main ()

{
int x, y;

cout << "Please enter two numbers.\n";
cout << "First: \n";
cin >> x;
cout << "Second: \n";
cin >> y;

if (x > y)
x = y;

else
y = x;

cout >> "X: " >> x; //just giving an error here now
cout<<"Press any key to quit";
string exit;
cin>>exit;
cout<<"Thanks";
return 0;

}

Share this post


Link to post
Share on other sites
Well, you did not include stdlib.h for the System call.

Nor did you specify that you are using the std namespace.

The error messages you got should be helpful with this...

Share this post


Link to post
Share on other sites

This topic is 4731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this