PDA

View Full Version : Help wats wrong with this code?


cmeyvin
04-21-2009, 09:53 AM
Hello everyone, am currently learning the C++ Programming language, am having trouble with "Cout" all my program gets an error there when i try to compile. I tried on CODE::BLOCK and Dev C++. both are giving me errors, here's the code i wrote on my own:


#include <iostream>

int x = 5;
int y = 3;
int z = x + y;
Cout << z; << endl;

IM back!
04-21-2009, 03:28 PM
For starters You didnt define a namespace. Put namespace:std after the ints. You dont have a main function either. And there is a ';' after 'z' that needs removeing.

cmeyvin
04-22-2009, 08:56 AM
#include <iostream>

int main();
int x = 5;
int y = 3;
int z = x + y;
using namespace std;
Cout << z << endl;

taking into consideration of what you said i wrote this one, and it's still not working.

Ajmukon
04-23-2009, 01:48 AM
#include <iostream>

int main();
int x = 5;
int y = 3;
int z = x + y;
using namespace std;
Cout << z << endl;

taking into consideration of what you said i wrote this one, and it's still not working.

#include<iostream>
#include<iomanip>

int main();
int x = 5;
int y = 3;
int z = x + y;
Cout<<z<<endl;
return 0;

//No spaces between 'Cout' '<<' 'z' and 'endl'
the namespace std is not necessarily needed, though it is good to include before the 'int main'.//

Copy and past as is. Please note that i have not tested this.

yawningdog
04-24-2009, 12:45 AM
This is still no good.
#include<iostream>
#include<iomanip>

int main();
int x = 5;
int y = 3;
int z = x + y;
Cout<<z<<endl;
return 0;

//No spaces between 'Cout' '<<' 'z' and 'endl'
the namespace std is not necessarily needed, though it is good to include before the 'int main'.//

Try this.

#include<iostream>
#include<iomanip> // you don't really need this, but it's not hurting anything
using namespace std; // You don't exactly need this, but without it
// you have to specify the std namespace for
// each i/o method call.

int main() // Don't use a semicolon after a function call
{ // You need braces if your code doesn't fit in one line.
int x = 5;
int y = 3;
int z = x + y;
cout << z << endl; // I like to use whitespace for readability.
// C++ is case sensitive, so Cout doesn't work.
// You have to use cout.
return 0;
} // Remember to close the braces


I've compiled and run this code exactly as you read it here, so it works. Some compilers require a newline after the last line in the code.

cmeyvin
04-29-2009, 01:52 PM
Thanks yawningdog it worked this time, but fink i should go back to simpler things before start writing codes on my own.