0

I'm currently learning how to program in C++ and one of the practical examples is to write a program showing the data types numeric limit in a table. Currently writing in repl.it before pasting to .txt and compiling using makefile. There are no resources or similar examples I could find explaining how to do this, nor have I fully grasped enough to ID the correct keywords to use.

Question

Attempt:

#include <limits>

void main()
{
number = sizeor(int)
unsign = sizeor(unsigned int)
long = sizeor(long)
longlong = sizeor(long_long)
unsignedchar = sizeor(unsigned char)
float = sizeor(float)
double = sizeor(double)
char = sizeor(char)

cout<<numeric_limits<int>::min();
cout<<numeric_limits<int>::max();
cout<<numeric_limits<int>::epsilon();

cout<<numeric_limits<double>::min();
cout<<numeric_limits<double>::max();
cout<<numeric_limits<double>::epsilon();

cout<<numeric_limits<unsign>::min();
cout<<numeric_limits<unsign>::max();
cout<<numeric_limits<unsign>::epsilon();

cout<<numeric_limits<long>::min();
cout<<numeric_limits<long>::max();
cout<<numeric_limits<long>::epsilon();

cout<<numeric_limits<longlong>::min();
cout<<numeric_limits<longlong>::max();
cout<<numeric_limits<longlong>::epsilon();

cout<<numeric_limits<unsignedchar>::min();
cout<<numeric_limits<unsignedchar>::max();
cout<<numeric_limits<unsignedchar>::epsilon();

cout<<numeric_limits<float>::min();
cout<<numeric_limits<float>::max();
cout<<numeric_limits<float>::epsilon();

cout<<numeric_limits<char>::min();
cout<<numeric_limits<char>::max();
cout<<numeric_limits<char>::epsilon();
}

Except this prints

main.cpp:5:2: error: expected function body after function declarator
    number = sizeor(int)
    ^
main.cpp:15:2: error: unknown type name 'cout'
    cout<<numeric_limits<int>::max();
    ^
main.cpp:15:6: error: expected unqualified-id
    cout<<numeric_limits<int>::max();
        ^
main.cpp:16:2: error: unknown type name 'cout'
    cout<<numeric_limits<int>::epsilon();
    ^
main.cpp:16:6: error: expected unqualified-id
    cout<<numeric_limits<int>::epsilon();
        ^
main.cpp:18:2: error: unknown type name 'cout'
    cout<<numeric_limits<double>::min();
    ^
main.cpp:18:6: error: expected unqualified-id
    cout<<numeric_limits<double>::min();
        ^
main.cpp:19:2: error: unknown type name 'cout'
    cout<<numeric_limits<double>::max();
    ^
main.cpp:19:6: error: expected unqualified-id
    cout<<numeric_limits<double>::max();
        ^
main.cpp:20:2: error: unknown type name 'cout'
    cout<<numeric_limits<double>::epsilon();
    ^
main.cpp:20:6: error: expected unqualified-id
    cout<<numeric_limits<double>::epsilon();
        ^
main.cpp:22:2: error: unknown type name 'cout'
    cout<<numeric_limits<unsign>::min();
    ^
main.cpp:22:6: error: expected unqualified-id
    cout<<numeric_limits<unsign>::min();
        ^
main.cpp:23:2: error: unknown type name 'cout'
    cout<<numeric_limits<unsign>::max();
    ^
main.cpp:23:6: error: expected unqualified-id
    cout<<numeric_limits<unsign>::max();
        ^
main.cpp:24:2: error: unknown type name 'cout'
    cout<<numeric_limits<unsign>::epsilon();
    ^
main.cpp:24:6: error: expected unqualified-id
    cout<<numeric_limits<unsign>::epsilon();
        ^
main.cpp:26:2: error: unknown type name 'cout'
    cout<<numeric_limits<long>::min();
    ^
main.cpp:26:6: error: expected unqualified-id
    cout<<numeric_limits<long>::min();

What is the correct method to acquire the desired result and where/what are the deficiencies in my attempt that I need to correct?

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 2
    Pay *extremely* close attention to every character. `sizeor` and `sizeof` might look similar, but they're not the same. – tadman May 24 '21 at 00:18
  • 2
    Hint: `cout` isn't a thing. `std::cout` is. – tadman May 24 '21 at 00:19
  • 6
    Tip: Instead of typing all of this code in and then mashing the compile button, start with the most minimal program, *ensure it compiles*, and build incrementally from there. Compile *frequently*. Stop adding more code the instant you have a compile problem, fix that issue before making things worse. – tadman May 24 '21 at 00:20
  • 3
    `number` etc are not declared and missing `;` `long = sizeor(long)` tries to use a keyword are a variable name. Please refer to the [list of books](https://stackoverflow.com/a/388282) – Richard Critten May 24 '21 at 00:20
  • Why have you got all of those 'sizeor' statements at the start of your code anyway? You don't use them. Also, some of the types in your 'numeric_limits' statements are incorrect, so you should review those. – Paul Sanders May 24 '21 at 01:00
  • 1
    `sizeof` gives you a number of bytes. The number of _bits_ is 8 times larger. – Nathan Pierson May 24 '21 at 01:07
  • 1
    Building on what tadman wrote, your first error is on the first line in your `main` function. Try reducing your program to `#include void main() { number = sizeor(int) }` and focusing on what is preventing that much from compiling. (A [mre] is a powerful debugging tool; it doesn't matter that there is no useful functionality yet.) – JaMiT May 24 '21 at 01:42

2 Answers2

2

There're quite a lot of errors/typos within your code.

  1. void main() will return a '::main' must return 'int' error, the correct syntax is int main().

  2. number = (IMO is Pythonic syntax) should be int number = , as in C++, the correct format for declaring variables is type variable_name = value;. More info here.

  3. Variables declarations should all end with ;, or the program will return a error: expected ',' or ';' before 'int'.

  4. A keyword such as int or char mustn't be used as a variable name.

  5. sizeor() is not a valid operator, it's sizeof() as specified clearly in your question.

  6. cout and numeric_limits<int>::min() is invalid, it should be std::cout and std::numeric_limits<int>::min(), or you can use using namespace std; instead. Also, when using cout, #include <iostream> must be used.

  7. Some of the type you used is invalid, such as long_long should be long long, unsign should be unsigned int and unsignedchar should be unsigned char.

I include a little snippet of your code which have been modified here (you can build and fill in the rest from this):

#include <limits>
#include <iostream>
int main()
{
    int number = sizeof(int);
    int unsign = sizeof(unsigned int);
    int long_ = sizeof(long);
    int longlong = sizeof(long long);
    int unsignedchar = sizeof(unsigned char);
    int float_ = sizeof(float);
    int double_ = sizeof(double);
    int char_ = sizeof(char);

    std::cout<<std::numeric_limits<int>::min() << "\n";
    std::cout<<std::numeric_limits<unsigned char>::min() << "\n";
    std::cout<<std::numeric_limits<long long>::min() << "\n";
    std::cout<<std::numeric_limits<float>::min() << "\n";
}

Result:

-2147483648
 
-9223372036854775808
1.17549e-38
  • It should also be noticed that sizeof() actually "Yields the size in bytes of the object representation of type", not bits. As @Nathan Pierson mentioned, the number of bits is 8 times more than the number of bytes.

P.S: As @tadman mentioned above:

Instead of typing all of this code in and then mashing the compile button, start with the most minimal program, ensure it compiles, and build incrementally from there. Compile frequently. Stop adding more code the instant you have a compile problem, fix that issue before making things worse.

And @JaMiT :

Building on what tadman wrote, your first error is on the first line in your main function. Try reducing your program to #include <limits> void main() { number = sizeor(int) } and focusing on what is preventing that much from compiling. (A minimal reproducible example is a powerful debugging tool; it doesn't matter that there is no useful functionality yet.)

IMO, you're not really familiar with C++ syntaxes, maybe you should try some simpler program first to get the hang of it. The list of books @Richard Critten provided is very useful for beginners.

More info:

std::numeric_limits : https://en.cppreference.com/w/cpp/types/numeric_limits

sizeof() : https://en.cppreference.com/w/cpp/language/sizeof

std::cout : https://en.cppreference.com/w/cpp/io/cout

0

sizeof is an operator and keyword in c++, and it calculates the bytes of data type or variables during the complying time. remember that sizeof is an operator, not a function.

Mehdi Mostafavi
  • 880
  • 1
  • 12
  • 25
PatZ
  • 1