Skip to main content
deleted 3 characters in body
Source Link
Sandro4912
  • 3.2k
  • 2
  • 23
  • 50
for (;;) {
    std::string input;
    std::cin >> input;

    if (is_convertible_to_integer(input)) { // write a function to check if is convertible to unsigned int
        // convert to stringint
        break;
    }
}
for (;;) {
    std::string input;
    std::cin >> input;

    if (is_convertible_to_integer(input)) { // write a function to check if is convertible to unsigned int
        // convert to string
        break;
    }
}
for (;;) {
    std::string input;
    std::cin >> input;

    if (is_convertible_to_integer(input)) { // write a function to check if is convertible to unsigned int
        // convert to int
        break;
    }
}
added 42 characters in body
Source Link
Jerry Coffin
  • 34.1k
  • 4
  • 77
  • 145

No comments on the algorithm itself. But iI have many style improvements a interviewer probalyprobably looks for.

You should better read in as std::stringstd::string and convert the result to integer after if possible:

for (;;) { std::string input; std::cin >> input;

for (;;) {
    std::string input;
    std::cin >> input;

    if (is_convertible_to_integer(input)) { // write a function to check if is convertible to unsigned int
        // convert to string
        break;
    }
}

}

This can be probalyprobably in a function as well like unsigendunsigned int read_unsigend_int();

The next thing I wonder. Do you really need unsigendunsigned int? Often it is not worth the hasselhassle. It can intriduceintroduce hard to spot errors: https://stackoverflow.com/questions/22587451/c-c-use-of-int-or-unsigned-int

Then you create a class just for solving basically some computations. In this case iI think its overengineeringover-engineering. It could be simply solved by using free standing functions. Unlike in some other Programming languages werewhere everything is a class you can just use free standing functions.

However it is a good idea to wrap youreyour functions and classes in its own namespace to prevent name clashes. So do something like:

AnnotherAnother small thing:

Also you should not use std::endl for a newline. I even saw it in many wrong books. std::endlstd::endl gives you a newline and an expensive flushing operation of the buffer. Instead just use \n.

No comments on the algorithm itself. But i have many style improvements a interviewer probaly looks for.

You should better read in as std::string and convert the result to integer after if possible:

for (;;) { std::string input; std::cin >> input;

if (is_convertible_to_integer(input)) { // write a function to check if is convertible to unsigned int
    // convert to string
    break;
}

}

This can be probaly in a function as well like unsigend int read_unsigend_int();

The next thing I wonder. Do you really need unsigend int? Often it is not worth the hassel. It can intriduce hard to spot errors: https://stackoverflow.com/questions/22587451/c-c-use-of-int-or-unsigned-int

Then you create a class just for solving basically some computations. In this case i think its overengineering. It could be simply solved by using free standing functions. Unlike in some other Programming languages were everything is a class you can just use free standing functions.

However it is a good idea to wrap youre functions and classes in its own namespace to prevent name clashes. So do something like:

Annother small thing:

Also you should not use std::endl for a newline. I even saw it in many wrong books. std::endl gives you a newline and an expensive flushing operation of the buffer. Instead just use \n.

No comments on the algorithm itself. But I have many style improvements a interviewer probably looks for.

You should better read in as std::string and convert the result to integer after if possible:

for (;;) {
    std::string input;
    std::cin >> input;

    if (is_convertible_to_integer(input)) { // write a function to check if is convertible to unsigned int
        // convert to string
        break;
    }
}

This can be probably in a function as well like unsigned int read_unsigend_int();

The next thing I wonder. Do you really need unsigned int? Often it is not worth the hassle. It can introduce hard to spot errors: https://stackoverflow.com/questions/22587451/c-c-use-of-int-or-unsigned-int

Then you create a class just for solving basically some computations. In this case I think its over-engineering. It could be simply solved by using free standing functions. Unlike in some other Programming languages where everything is a class you can just use free standing functions.

However it is a good idea to wrap your functions and classes in its own namespace to prevent name clashes. So do something like:

Another small thing:

Also you should not use std::endl for a newline. I even saw it in many wrong books. std::endl gives you a newline and an expensive flushing operation of the buffer. Instead just use \n.

some more points
Source Link
Sandro4912
  • 3.2k
  • 2
  • 23
  • 50

Some thingsNo comments on the algorithm itself. But i think you could improve:have many style improvements a interviewer probaly looks for.

No need to use const here since you have the values by value copied anyway.

Also you should not use std::endl for a newline. I even saw it in many wrong books. std::endl gives you a newline and an expensive flushing operation of the buffer. Instead just use \n.

A opinion based thing. This line:

    cout << "[" << left << ", " << right << "] contains " << last << " and sum = " << (left + right) << endl;

I would split it into two:

    cout << "[" << left << ", " << right << "] contains " << last 
        << " and sum = " << (left + right) << endl;

why? It is easier to read and you have the option to open two source files on one screen next to each other. I personally stick usually with 80 spaces per line. But some people user 100 or 120. To see a discussion about it:

https://stackoverflow.com/questions/276022/line-width-formatting-standard

Some things i think you could improve:

No need to use const here since you have the values by value copied anyway.

No comments on the algorithm itself. But i have many style improvements a interviewer probaly looks for.

No need to use const here since you have the values by value copied anyway.

Also you should not use std::endl for a newline. I even saw it in many wrong books. std::endl gives you a newline and an expensive flushing operation of the buffer. Instead just use \n.

A opinion based thing. This line:

    cout << "[" << left << ", " << right << "] contains " << last << " and sum = " << (left + right) << endl;

I would split it into two:

    cout << "[" << left << ", " << right << "] contains " << last 
        << " and sum = " << (left + right) << endl;

why? It is easier to read and you have the option to open two source files on one screen next to each other. I personally stick usually with 80 spaces per line. But some people user 100 or 120. To see a discussion about it:

https://stackoverflow.com/questions/276022/line-width-formatting-standard

Source Link
Sandro4912
  • 3.2k
  • 2
  • 23
  • 50
Loading