Ok, I'm probly going to sound like a bit of an idiot, but I'm new to the programming world.

Whenever I attempt to run this program, I get a long list of error messages.

//DATATYPE.CPP
//Examples of variable declaration and
//initialization.

#include <iostream>

main ()
{
     //declare a constant for the square root of two
     const double SQUARE_ROOT_OF_TWO = 1.414214;

     int i;                 //declare i as an integer
     long j;                //j as a long integer
     unsigned long k        //k as an unsigned long integer
     float n;               //n as a floating point number

     i = 3;                 //initialize i to 3
     j = -2048111;          //j to -2048111
     k = 4000000001;        //k to 4000000001
     n = 1.887;             //n to 1.887

     //output constant and variables to screen
     cout << SQUARE_ROOT_OF_TWO << '\n';
     cout << i << '\n';
     cout << j << '\n';
     cout << k << '\n';
     cout << n << '\n';
     return 0;
     }

Whenever I try to run this, I get these error messages.

In function `int main()': 
expected primary-expression before "unsigned" 
`k' undeclared (first use this function) 
[Warning] this decimal constant is unsigned only in ISO C90 
`n' undeclared (first use this function) 
`cout' undeclared (first use this function)

Recommended Answers

All 5 Replies

Missing a semicolon here:
unsigned long k //k as an unsigned long integer

also, either change cout to std::cout or put a 'using namespace std;' at the top after the include.

>>Whenever I attempt to run this program,

Since you are new -- you did not run this program, but you compiled this program". If you are going to learn programming then you need to also learn programming jargon. "run a program" means the program is already compiled and you can execute it, just like you execute most other programs on your computer.

And for future reference when you post questions and code it is very helpful to everyone if you would also indicate the compiler you used, its version (if known) and the operating system you are using.

main() should be int main()
there is no semicolon after unsigned long k

Just my own personal thing ... use endl in c++ and \n in c :)

Member Avatar for iamthwee

main() should be int main()
there is no semicolon after unsigned long k

Just my own personal thing ... use endl in c++ and \n in c :)

Yes good advice, using endl in c++ also flushes the buffer however, it would also be acceptable to use '\n' as well.

Hey Dani did you get my PM? :cry:

Ok, I'm probly going to sound like a bit of an idiot, but I'm new to the programming world.

Whenever I attempt to run this program, I get a long list of error messages.

//DATATYPE.CPP
//Examples of variable declaration and
//initialization.

#include <iostream>

main ()
{
     //declare a constant for the square root of two
     const double SQUARE_ROOT_OF_TWO = 1.414214;

     int i;                 //declare i as an integer
     long j;                //j as a long integer
     unsigned long k        //k as an unsigned long integer
     float n;               //n as a floating point number

     i = 3;                 //initialize i to 3
     j = -2048111;          //j to -2048111
     k = 4000000001;        //k to 4000000001
     n = 1.887;             //n to 1.887

     //output constant and variables to screen
     cout << SQUARE_ROOT_OF_TWO << '\n';
     cout << i << '\n';
     cout << j << '\n';
     cout << k << '\n';
     cout << n << '\n';
     return 0;
     }

Whenever I try to run this, I get these error messages.

In function `int main()': 
expected primary-expression before "unsigned" 
`k' undeclared (first use this function) 
[Warning] this decimal constant is unsigned only in ISO C90 
`n' undeclared (first use this function) 
`cout' undeclared (first use this function)

end quote.

The biggest problem with the program is this line

[Warning] this decimal constant is unsigned only in ISO C90
k= 4000000001 it will not accept if I remove one digit the program will run.

Can anyone shed some light on the subject.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.