dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 43 of 43

Thread: Namespace and stdafx.h

  1. #31
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Namespace and stdafx.h

    Quote Originally Posted by stober
    That is exactly what I said -- Comdeau is not a compiler, just a preprocessor. You have to have some other compiler installed on your computer.
    Not according to them:
    Note closely that this is not preprocessing, but compiling.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  2. #32
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Namespace and stdafx.h

    Comeau says that he is a compiler:
    Comeau C/C++ is a command line driven C and C++ compiler that generates platform specific and C compiler specific C as its object code (the generated C code won't work on another platform, as it is CPU, OS and C compiler specific, and furthermore, it is not standalone). It then transparently invokes a particular C compiler that we've specifically ported it to (again, it won't work with one we haven't ported it to -- such porting must be done by Comeau since particulars must be coded into the compiler and library itself). Note closely that this is not preprocessing, but compiling. The Comeau C/C++ front-end first does full "C-style" preprocessing (#include, #define, etc.), as usual. That results in a normal translation unit, upon which Comeau C/C++ then does normal and full syntax, semantic and error checking. Next, the input C++ program (or C program in C mode) is translated into a machine independent intermediate language (IL), as is usually done by compilers. So, in effect, the original C++ (or C in C mode) source code is effectively gone at this point.
    Comeau is a C++ compiler which compiles to an intermediate language which is very close to C, but contains a few modifications (i suppose that there must be some modifications to allow an efficient usage of exceptions for example).
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  3. #33
    Join Date
    Jun 2002
    Posts
    1,417

    Re: Namespace and stdafx.h

    It can claim whatever it likes, but because it invokes some other compiler makes Comeau nothing more than a preprocessor. "particular C compiler that we've specifically ported it to" means that it generates code that is compatible with the compiler.

    If you want to use Comeau you still have to have another compiler installed on the computer.
    Quote Originally Posted by Comeau
    Comeau C/C++ must work with specific C compilers in order to generate native object code. Therefore, we have tailored our generally available ports of Comeau C/C++ for use with various C compilers. The list of specific C compilers immediately follows here. The C compilers discussed below must be from the normal distribution for the platform and target that same respective platform.

  4. #34
    Xeon's Avatar
    Xeon is offline Want me to ban you?! Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Re: Namespace and stdafx.h

    Ok, back to the original question about cin.sync().
    Just for reference, the original code was :
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {   
        double dCelsius = 0.0, dFah = 0.0;
        
        cout << "Enter the celsius temperature to be converted to Fahrenheit :\n";
        
        cin >> dCelsius;
        
        dFah = (dCelsius * 1.8) + 32;
        
        cout << "The Fahrenheit equivalent of " << dCelsius << " celsius is " << dFah;
    
        return 0;
    }
    The reply was :
    From SuperKoko:
    The synchronisation is necessary because std::cin does not read the newline character after the number input. And cin.get returns that newline character without needing to get input from the user.
    Hmmm.....was there any newline character after the number input(cin >> dCelsius;) in the code above?

    I'm still very confused about cin.sync() and how it can actually work together with cin.get() to fix the flashing window problem.
    Is there any clearer explanation, like an analogy or fun examples?

    And I read the MSDN explanation, but it's explanation was talking about some buffer stuff and it's worse than doing any explanation at all.

    Good day and thanks all!
    Xeon.
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  5. #35
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Namespace and stdafx.h

    Well... you need the explanation with a buffer.

    To simplify, consider for now, only reading a stream character by character.
    the input system works like that for std::cin:
    • If there are some characters "ready" (in the input buffer), the get() function returns the character at the front of the buffer and remove it from the buffer.
    • If there are no characters "ready", that is, the input buffer is empty, the get() method request some input from the user.
      The user can type a whole line of text, and edit it (with backspace, del, etc.)
      The user confirms his input by pressing [return].
      At this date, the get() function is still hanged on the user input.
      When the user presses [return], the std::cin stream reads the whole user input and put it all in the input buffer. A newline character ('\n') is appended to the user input.
      Then the get() function returns the front character of that buffer, and remove it from the input buffer.


    Initially, when the program is launched, the input buffer is empty (there are no characters ready for input).

    To simplify, we can consider that any input function, indirectly calls the get() function (that is not exact, but i simplify a bit the problem).
    The std::istream::getline function, for example, reads all the characters one by one, calling get() to get each character, until it reaches a newline character. When that character is reached, the function stops and returns all the characters it has found, except the newline character. But the newline character is removed from the input buffer, so a new call to std::istream::getline on the same stream, will not return an empty string.
    The std::istream& std::istream:perator>>(double&) function (the function you use to input the double), is different:
    It reads all the characters (with a mechanism similar to successives call to get()) until it reaches a whitespace character or any non-digit character
    Note : whitespace characters include : space, tab, newline, return, formfeed and maybe vertical tab (' ', '\t', '\n','\r',...).
    When this whitespace or non-digit character is reached, the function pushes back that character into the input buffer, so it is not really removed from the buffer, and than return the double number which corresponds to the digits read.

    So, when you do std::cin>>dCelsius:
    it tries to read one character (like if get() was called)
    The input buffer at this data is empty, so it request for characters for the user
    The user enter "42" for example, and then type [return].
    "42\n" is stored in the input buffer.
    and '4' is returned by the character-reading function and the input buffer is set to "2\n"

    A second call to the character-reading function is performed and '2' is read. The input buffer now contains only "\n"
    A third call to the chracter-reading function is performed and '\n' is returned. The input buffer is now empty. But the input function sees that this character is not a digit character, and push back it in the input buffer, which is now "\n", and returns the 42 floating point value.
    Now, if you call directly get(), it will return that '\n' character without requesting for the user to input!
    But, if you call std::cin::sync, the input buffer will be emptied, and the user will be requested for input at the next call of get().
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  6. #36
    Xeon's Avatar
    Xeon is offline Want me to ban you?! Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Smile Re: Namespace and stdafx.h

    Thanks a lot, SuperKoko! Spent 25 mins reading the explanation and try to understand it and refering to other sources as well.
    Now, below is the correct code Padex gave earlier. I'm going to give my own explanation of what's going on at crucial steps of the code, and correct me if my explanation is wrong.

    #include <iostream>
    using namespace std;

    int main()
    {
    double dCelsius = 0.0, dFah = 0.0;
    cout << "Enter the celsius temperature to be converted to Fahrenheit :\n";

    cin >> dCelsius;

    /*if we enter 42 as the input in the above line, dCelsius will be just 42 and not 42\n, because the >> operator reads only non-special characters and pushes any special characters back into the buffer. at this stage, the input buffer contains \n.*/

    dFah = (dCelsius * 1.8) + 32;
    cout << "The Fahrenheit equivalent of " << dCelsius << " celsius is " << dFah;

    cin.sync();

    cin.get(); //call get() to ask for user input; as long as the user doesn't input anything, the program won't close, thus giving him a chance to see the conversion results.

    /*everytime it's called, get() will check the input buffer to see if it's empty. since the input buffer previously already has \n in it, it's not empty.
    since it's not empty, get() won't bother asking the user for input and just directly return what's currently in the buffer, which is \n.

    now that \n is being returned, the program assumes that the user has pressed the Return(Enter) key and terminates automatically without giving us a chance to look at the result, even though we didn't want that to happen.

    thus, we call sync() to empty everything in the buffer so that the program won't see \n in the buffer when get() is called. this is because once the program sees \n being called, it assumes the Return key has been pressed and there's nothing left to do anymore, and closes.*/


    return 0;
    }


    Correct me if I'm wrong in my explanation, guys.

    Another thing : I never had to deal with all these complexities in Visual C++ 6.0. Is it because VC++ hid a lot of details for me?

    Thanks!
    Xeon.
    Last edited by Xeon; November 4th, 2005 at 09:36 AM.
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  7. #37
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Thumbs up Re: Namespace and stdafx.h

    Quote Originally Posted by Xeon
    Correct me if I'm wrong in my explanation, guys.
    Yeah! You got it! You understood!
    Congratulations.

    Quote Originally Posted by Xeon
    Another thing : I never had to deal with all these complexities in Visual C++ 6.0. Is it because VC++ hid a lot of details for me?
    I suppose that you were lucky to never encounter any problem with VC++.

    All that complex stuff is useful, because it allows to use uniformily any type of stream : file stream, string stream, network TCP stream, and console streams.

    That input may also be good for the user.
    For example:
    Code:
    std::cin>>someVariable;
    std::cin>>anotherVariable;
    The user can input the two values on a single line!
    Code:
    42 76
    If there was no buffering system, and that each character typed was immediately sent to the program, the user would not be able to correct his input.

    Note : i tried that code:
    Code:
    #include <iostream.h>
    
    int main()
    {
    char c;
    cin>>c;
    cin.get();
    return 0;
    }
    With turbo C++ 1.0.1, and it stops immediately after the [return] key was pressed after the character was entered.
    That is the expected behavior.

    I hope that VC++ 6.0 has that very standard behavior.
    I fear the possibility that VC++ 6.0 eat the whitespace character encountered without pushing it back to the buffer).
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  8. #38
    Xeon's Avatar
    Xeon is offline Want me to ban you?! Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Re: Namespace and stdafx.h

    From S. Koko:
    Yeah! You got it! You understood. Congratulations.
    2 girls for you and 2 girls for me! Pick your flavor now.

    By the way, Koko, I've one thing I'm still not very sure about : cin.get() and cin.getline().
    I've consulted the documentation and books, but the way they explain it is pretty ambiguous.

    Now, we know that the cin >> operator reads all the characters until it encounters a non-printable character. It then reads all the characters before this non-printable character into the stream, and pushes back the non-printable character(can be \n, \t, ' '....) into the input buffer.

    But what about cin.get() and cin.getline()?

    Is it that cin.get() reads everything that the user inputted, including all non-printable characters and even the terminating character as well?
    (that is, it doesn't push the terminating character into the input buffer)

    And for cin.getline(), it reads everything the user inputted, including all non-printable characters, but it pushes the terminating character back into the input buffer?

    Correct me if I'm wrong in this.

    Thanks KoKo!
    Xeon.
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  9. #39
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Namespace and stdafx.h

    Quote Originally Posted by Xeon
    Is it that cin.get() reads everything that the user inputted, including all non-printable characters and even the terminating character as well?
    (that is, it doesn't push the terminating character into the input buffer)
    cin.get(char*,ios::streamsize,char) reads all the characters from the input, until a delimiter character is reached. When that character is reached, it is pushed back to the input buffer, and the function returns the string that was built at this (the string does not contain the delimiter character).
    For example, if the input buffer is "hello\nworld", cin.get(buffer,bufferSize,'\n') will return the "hello" string, and the input buffer is then "\nworld".

    The getline function does not extract the delimiter character, so in our case cin.getline(buffer,bufferSize,'\n') will return the same "hello" string, but the input buffer would be "world" after the input.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  10. #40
    Xeon's Avatar
    Xeon is offline Want me to ban you?! Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Re: Namespace and stdafx.h

    Now I understand! Thanks S. KoKo!

    See you,
    Xeon.
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  11. #41
    Join Date
    Apr 2019
    Posts
    1

    Unhappy [Error] iostream.h: No such file or directory

    Code:
    #include<iostream.h> 
    \\ Esta  directiva permite usar la funcion de lectura cin y la funcion de escritura cout propias del c++
    using namespace std;  
     \\Localizar el nombre de identificadores para evitar colisiones tener compatibilidad cuando un programa en C++,incluye una libreria de C
    int main()
    {
     int a, b;            
      \\ declara el tipo de dato de a y b
     cout << "Ingresa el primer numero"<<endl; \\ la funcion cout, directiva de escritura,espera el valor del tipo de a
     cin >> a;              \\la  funcion cin, directiva de lectura
     std cout << "Ingresa el segundo numero"<<endl; \\la funcion cout, directiva de escritura
     cin >> b;              \\la  funcion cin, directiva de lectura
     cout <<"La suma de los numeros es: "<< a+b <<endl;\\la funcion cout, directiva de escritura
     cout <<"La resta de los numeros es: "<< a-b <<endl; // 
     la funcion cout, directiva de escritura
     cout <<"La multiplicacion de los numeros es: "<< a*b <<endl;\\la funcion cout, directiva de escritura
     cout <<"La division de los numeros es: "<< a/b <<endl;\\la funcion cout, directiva de escritura
     cout <<"El residuo es: "<< a%b <<endl;\\la funcion cout, directiva de escritura
    
     system("pause");\\
     return EXIT_SUCCESS;\\
    }
    buenas noches,saludos a todos gurus de C++
    he comenzado un curso en c++
    he aqui el primer traspies
    esta es la tarea, he buscado en la red y ya me duele la cabeza, dicen que no use el .h, pero si le quito la h a iostream, me genera mas errores como [Error] stray '\' in program, por favor su ayuda
    gracias
    Last edited by 2kaud; April 16th, 2019 at 03:52 AM. Reason: Added code tags

  12. #42
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,810

    Re: Namespace and stdafx.h

    [When posting code, please use code tags. Go Advanced, select the formatted code and click '#']. Cheers!

    In modern standard c++, the include file should be just iostream - not iostream.h
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.5)

  13. #43
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,059

    Re: [Error] iostream.h: No such file or directory

    Quote Originally Posted by popayan563 View Post
    Code:
    #include<iostream.h> 
    \\ Esta  directiva permite usar la funcion de lectura cin y la funcion de escritura cout propias del c++
    using namespace std;  
     \\Localizar el nombre de identificadores para evitar colisiones tener compatibilidad cuando un programa en C++,incluye una libreria de C
    int main()
    {
     int a, b;            
      \\ declara el tipo de dato de a y b
     cout << "Ingresa el primer numero"<<endl; \\ la funcion cout, directiva de escritura,espera el valor del tipo de a
     cin >> a;              \\la  funcion cin, directiva de lectura
     std cout << "Ingresa el segundo numero"<<endl; \\la funcion cout, directiva de escritura
     cin >> b;              \\la  funcion cin, directiva de lectura
     cout <<"La suma de los numeros es: "<< a+b <<endl;\\la funcion cout, directiva de escritura
     cout <<"La resta de los numeros es: "<< a-b <<endl; // 
     la funcion cout, directiva de escritura
     cout <<"La multiplicacion de los numeros es: "<< a*b <<endl;\\la funcion cout, directiva de escritura
     cout <<"La division de los numeros es: "<< a/b <<endl;\\la funcion cout, directiva de escritura
     cout <<"El residuo es: "<< a%b <<endl;\\la funcion cout, directiva de escritura
    
     system("pause");\\
     return EXIT_SUCCESS;\\
    }
    buenas noches,saludos a todos gurus de C++
    he comenzado un curso en c++
    he aqui el primer traspies
    esta es la tarea, he buscado en la red y ya me duele la cabeza, dicen que no use el .h, pero si le quito la h a iostream, me genera mas errores como [Error] stray '\' in program, por favor su ayuda
    gracias
    This is an English only forum. Please post only in English.

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)