Not according to them:Quote:
Originally Posted by stober
Quote:
Note closely that this is not preprocessing, but compiling.
Printable View
Not according to them:Quote:
Originally Posted by stober
Quote:
Note closely that this is not preprocessing, but compiling.
Comeau says that he is a compiler:
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).Quote:
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.
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
Ok, back to the original question about cin.sync(). :)
Just for reference, the original code was :The reply 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;
}
From SuperKoko:
Hmmm.....was there any newline character after the number input(cin >> dCelsius;) in the code above?Quote:
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.
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? :D
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. :eek:
Good day and thanks all! :wave:
Xeon.
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::operator>>(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().
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. :D
Yeah! You got it! You understood!Quote:
Originally Posted by Xeon
Congratulations.
I suppose that you were lucky to never encounter any problem with VC++.Quote:
Originally Posted by Xeon
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:
The user can input the two values on a single line!Code:std::cin>>someVariable;
std::cin>>anotherVariable;
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.Code:42 76
Note : i tried that code:
With turbo C++ 1.0.1, and it stops immediately after the [return] key was pressed after the character was entered.Code:#include <iostream.h>
int main()
{
char c;
cin>>c;
cin.get();
return 0;
}
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).
From S. Koko:
2 girls for you and 2 girls for me! Pick your flavor now. :wave:Quote:
Yeah! You got it! You understood. Congratulations.
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! :D:)
Xeon.
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).Quote:
Originally Posted by Xeon
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.
Now I understand! Thanks S. KoKo! :D:thumb::wave:
See you,
Xeon.
buenas noches,saludos a todos gurus de C++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;\\
}
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
[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