Hello, I'm new to the site here. I say this because there seems to be a fair bit of etiquette here that I could step on so if I do, that's why.

Anyhow, I had to take C for my software certificate and since I wanted to take C++ and couldn't, I'm trying to self teach it to myself. I'm doing that by essentially going through my old C book and writing the homework problems in C++ instead so that explains the simplicity of this first question.

1. Ok, I hope I do this right... I have a C problem that asks for you to take in a character from the keyboard. It then wants me to print it twice, once as a character (%c) and then again as an integer (%d) and I can do that without batting an eye. I am not, however so familiar with the cout and cin commands. I was able to reproduce the results by using a cast in the cout statement but I need to know if there is an equivalent of the type specifier (%d) or if I should just use the cast.

2. The second one is more theoretical. I've read a fair bit about C/C++ and other languages that a MAIN function. In every tutorial or book there comes a point where the book will say something like "When you write larger programs, Main will consist only of function calls." My question is, how literal is that statement supposed to be taken? Does that mean that there should be no variables defined in Main? Does it mean that if there are four lines of code that they should all be function calls.

I'm sorry if these are odd questions or worse yet.. repeated questions but I have looked around and wherever I've looked it is assumed that this is known in the same way it is assumed that I breathe and walk upright.

Thanks in advance.
Geometroid

Recommended Answers

All 2 Replies

Hello, I'm new to the site here. I say this because there seems to be a fair bit of etiquette here that I could step on so if I do, that's why.

Welcome to DaniWeb! :) I reccomend reading the threads at the top of the forum marked Announcement and Read Me if you haven't done so already

Anyhow, I had to take C for my software certificate and since I wanted to take C++ and couldn't, I'm trying to self teach it to myself. I'm doing that by essentially going through my old C book and writing the homework problems in C++ instead so that explains the simplicity of this first question.

C and C++ are actually very different languages, as I'm sure you're finding. If you're serious about C++, then find yourself a good introductory C++ book, which will have excercises that encourage you to solve problems in a C++ way.

1. Ok, I hope I do this right... I have a C problem that asks for you to take in a character from the keyboard. It then wants me to print it twice, once as a character (%c) and then again as an integer (%d) and I can do that without batting an eye. I am not, however so familiar with the cout and cin commands. I was able to reproduce the results by using a cast in the cout statement but I need to know if there is an equivalent of the type specifier (%d) or if I should just use the cast.

I think this is one example of a C-like excercise which doesn't really apply to learning C++ (Unless its going to teach you how to cast, or how to assign a char to an int).

Incidentally, the reccomended way to cast between int and char in C++ is using a C++ static_cast, which looks like this

#include <iostream>

int main()
{
    char ch = 'A';
    std::cout << static_cast<int>( ch );
}

C++ has different kinds of casts for different situations (The 'C' syntax is still used but frowned upon). the reason being that C++ casts are designed to help you notice when you're doing something that may cause runtime bugs. static_cast's are usually quite safe, and will throw a compiler error if you attempt to cast between completely unrelated types (such as a std::string to an int). When dealing with more complicated casts (such as dynamic_cast or reinterpret_cast), the "ugly" C++ cast syntax is a big neon flashing sign to the programmer that he/she has to be careful what they're doing.

2. The second one is more theoretical. I've read a fair bit about C/C++ and other languages that a MAIN function. In every tutorial or book there comes a point where the book will say something like "When you write larger programs, Main will consist only of function calls." My question is, how literal is that statement supposed to be taken? Does that mean that there should be no variables defined in Main? Does it mean that if there are four lines of code that they should all be function calls.

If you use the form of main which takes commandline arguments, then that statement is already false, since you have 2 variables (Usually known as argv and argc) which you need to juggle about to determine what the user typed after the program name. (although you could pass this responsibility to a function or object)

In a more general sense, the main function is usually a very high level view of the basic flow of the program, and it wouldn't be out of place for the main function to simply consist of 3 or 4 lines when dealing with a huge program spanning dozens of .CPP files. What those 3 or 4 lines are, depends on the approach you use.

One approach is to use main as a "dummy" entry point, and redirect the main program to another function (Where you could pass the commandline input as a std::vector<string> instead of raw arrays) .. or you may have a single function call in main surrounded by a try-catch block, for easy exception handling. Also, if you use non-standard extensions (such as those provided by MS Visual studio), your entry point may end up being something completely different (MSVC++ uses a function called WinMain as its entry point for Win32 apps)

I think its a 'big picture' issue that you shouldn't worry about too much for the moment. For small/toy programs while learning the language, or testing out a feature of the language, it really doesn't matter IMHO.

I think its a 'big picture' issue that you shouldn't worry about too much for the moment. For small/toy programs while learning the language, or testing out a feature of the language, it really doesn't matter IMHO.

I concur, you will find that after writing a few hundred programs you will probably develop your own style. This is an example of how I use main in a windows application

int APIENTRY WinMain (HINSTANCE Instance, HINSTANCE PrevInst, char *CmdLine, int ShowValue)
    {
    MSG Msg;

    hInst = Instance;                                                    // Global copy of instance handle.

    // Create the main window and if everything was successful then fall into applications main
    // message pump
    if (CreateMainWindow (ShowValue))
        {
        Words.Create ("Glenda's Medical Words");

        while (GetMessage (&Msg, NULL, 0, 0))
            {
            TranslateMessage (&Msg);
            DispatchMessage (&Msg);
            }
        return Msg.wParam;
        }

    return 0;
    }

Some of my applications are more elaborate depending on what's required for startup, but this does not mean I am any more or less correct than other methods. As Bench has said, I use the four variables passed by preamble and one of my own, so if you wanted to do your entire application in the main () block your free to do so.

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.