Help me debug my code.

Started by
6 comments, last by VinnyC 19 years, 3 months ago
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 }
Advertisement
#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;
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
I posted a solution in AIM. If you have any more questions just reply.
#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;
}
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;

}

change
">>"
to
"<<"
Wow, that was a stupid mistake. Thanks both of you guys.
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...

This topic is closed to new replies.

Advertisement