cout problem
Sep 7, 2011 at 3:18pm UTC
im extremely new to c++ programming, started today actually and ive come across a small problem.
I get an error when trying to do a simple sizeof program using cout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <limits.h>
int main(void )
{
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
cout << "int är " << sizeof (int ) << " bytes.\n" ;
cout << "short är " << sizeof (short ) << " bytes.\n" ;
cout << "long är " << sizeof (long ) << " bytes.\n" ;
cout << "Maxvärden:\n" ;
cout << "int: " << n_int << "\n" ;
cout << "short: " << n_short << "\n" ;
cout << "long: " << n_long << "\n" ;
cout << "Minsta intvärde = " << INT_MIN << "\n" ;
return 0;
}
Have i done anything wrong? And if so, what do i need to do to fix it?
Im using Dev C++ 4.9.9.2.
Sep 7, 2011 at 3:33pm UTC
I think that you haven't included the right header #include<iostream> or #include<iostream.h> for the cout I/O flux. Try to include those headers and see if it works!
Sep 7, 2011 at 4:03pm UTC
I was able to compile it this way in MS visual C++ 2008 Express. Not sure about Dev C++. Try leaving out the
if it doesn't want to compile.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// Sizeof.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <limits.h>
int main(void )
{
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
std::cout << "int är " << sizeof (n_int) << " bytes.\n" ;
std::cout << "short är " << sizeof (n_short) << " bytes.\n" ;
std::cout << "long är " << sizeof (n_long) << " bytes.\n\n" ;
std::cout << "Maxvärden:\n" ;
std::cout << "int: " << n_int << "\n" ;
std::cout << "short: " << n_short << "\n" ;
std::cout << "long: " << n_long << "\n\n" ;
std::cout << "Minsta intvärde = " << INT_MIN << "\n" ;
std::cout << "Maxsta intvärde = " << INT_MAX << "\n" ; // Not sure of wording. Just showing INT_MAX
return 0;
}
Sep 7, 2011 at 4:03pm UTC
You need to add iostream not limits.h. Further you write "using namespace::std;" (without quotes) after including the specified directive. This will solve your problems.
for more help:
http://recurseit.blogspot.com
Last edited on Sep 7, 2011 at 4:04pm UTC
Sep 7, 2011 at 4:21pm UTC
I love you guys, thanks! the iostream.h worked :)
Sep 7, 2011 at 4:53pm UTC
You welcome!
Topic archived. No new replies allowed.