# cin/cout help

## Recommended Posts

kk so i am writing this program look at it but when i put in my celsuis the dos window closes #include <iostream> using namespace std; int main() { double ctemp, ftemp; cout <<"Input a Celsius temp and press Enter:"; cin >> ctemp; ftemp= (ctemp * 1.8) + 32; cout << "Fahrenheit temp is"; cin.get(); } [edit: admonished topic] [Edited by - Magmai Kai Holmlor on November 29, 2004 8:31:08 PM]

##### Share on other sites
You're not actually outputting the fahrenheit temperature as part of that line, nor are you outputting anything that would flush the output buffer (like 'endl').

##### Share on other sites
Quote:
 Original post by matthew_irelandkk so i am writing this program look at it but when i put in my celsuis the dos window closes#include using namespace std;int main() { double ctemp, ftemp; cout <<"Input a Celsius temp and press Enter:"; cin >> ctemp; ftemp= (ctemp * 1.8) + 32; cout << "Fahrenheit temp is"; cin.get(); }

Flush the input before the 'cin.get();' line. There's a trailing character that is getting read by the input [probably a newline], so its not waiting for input like you'd expect. Just tack 'cin.flush();' on there, and ti should fix you right up.

Of course, you're also not outputting the temperature, but that's an entirely different bug.

CM

##### Share on other sites
no offence but why does that even compile? main doesnt return anything, the compiler should generate an error. also when ftemp is shown (which in ur code its not, change cout << "Fahrenheit temp is"; to cout << "Fahrenheit temp is" << ftemp) the program ends and the windows shuts down. two ways around it, run ur program from a pre opened cmd prompt, ie type cmd into start->" Run..." window and then type in the exact path to ur program, ie c:\someprogram. or input something like :
int x;
cin >> x;

in ur program at the end, which will suspend the program, there is some inbuild windows command which displays "press any key to continue..." but i cant remember it so i just use this (if someone can tell me the right windows command for this then plz do):

#define FREEZE char __x__[8]; cout << endl; cin >> __x__

and then just place FREEZE; when i want my program to freeze.

sorry if iam patronicing u, but it looks to me like u r where i was about 5 years ago, when i ran my first ptogram which simply displayed my name, the window just flashed on and then off, i didnt know wtf to do untill i read the next section of the book on input and i got the idea...

##### Share on other sites

Quote:
 Original post by GregLoutsenkono offence but why does that even compile? main doesnt return anything, the compiler should generate an error.

Failure to return will only generate a warning, not an error. Also, the C++ standard specifies that a main function without a return statement will implicitly return 0.

Quote:
 C++ Standard (thanks Jingo)A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

##### Share on other sites
Hi everibody,

Quote:
 Original post by GregLoutsenkono offence but why does that even compile? main doesnt return anything, the compiler should generate an error.

A standard-compliant compiler should perhaps generate an error, VC++ and G++ are not - and they do compile this code.

Quote:
 Original post by GregLoutsenko...in ur program at the end, which will suspend the program, there is some inbuild windows command which displays "press any key to continue..." but i cant remember it so i just use this (if someone can tell me the right windows command for this then plz do):

I often saw an incredible system("pause") at the end of a console programs here at gamedev. This is not a good idea at all, for a lot of reasons (security, process managment, and so on). I encourage you to stick with cin manipulations.

Quote:
 Original post by GregLoutsenko#define FREEZE char __x__[8]; cout << endl; cin >> __x__

Take care of this :
if (cond) FREEZE;

This will not do what you wanted to do, isn't it ?
In your case, it would be better to define a small function.

Regards,

##### Share on other sites
ah yes, for some strange reason VC++ doesnt mind return missing in main but it does when u define a funstion to return something, i mean its really strange as main is also a function and in this case it should return something, i forgot about that little quirk, iam soo sorry.

##### Share on other sites
Quote:
 ah yes, for some strange reason VC++ doesnt mind return missing in main but it does when u define a funstion to return something, i mean its really strange as main is also a function and in this case it should return something, i forgot about that little quirk, iam soo sorry.

It is a quirk in older Visual Studio versions.

Quote:
 sorry if iam patronicing u, but it looks to me like u r where i was about 5 years ago, when i ran my first ptogram which simply displayed my name, the window just flashed on and then off, i didnt know wtf to do untill i read the next section of the book on input and i got the idea...

Everyone starts somewhere.

##### Share on other sites
well i found a way to do it with this pause thing look here.

#include <iostream>
using namespace std;

int main(){
int pause;
double ctemp, ftemp;

cout <<"input celsios temp and press enter";
cin >>ctemp;
ftemp=(ctemp*1.8)+32;
cout <<"fahreinhet is: " << ftemp;
cin>>pause;
return 0;
}

##### Share on other sites
Quote:
 Original post by GregLoutsenkoah yes, for some strange reason VC++ doesnt mind return missing in main

That's not strange at all, because it's been defined to be so in the standard - main is a special function, which implicitly returns 0 if it reaches the end of the function. Programs don't even have to exit via returning from main - they can call exit or abort for example.

What IS strange, IMHO, is that the standard is written to allow this.

Also, it is perfectly valid C++ code to create a function that has a return value, but never returns - but if it reaches the end of the function, the results are undefined (I remember a few programs I wrote using Borland C++ Builder in my early days that crashed because I had frogotten a return).

Consider for a moment this:

int (* some_function )( void );int a = some_function(); //WHOOPS, results undefined.int some_null_function( void ){    throw error( "Attempted to use a null function" );}some_function = some_null_function;int b = some_function(); //WHOOPS, still an error, but at least this time we can catch it and do better debugging.int the_some_function( void ){    return 5;}some_function = the_some_function;int c = some_function; //OK

Contrived example, but it explains my point.

Quote:
 but it does when u define a funstion to return something, i mean its really strange as main is also a function and in this case it should return something, i forgot about that little quirk, iam soo sorry.

Just to reiterate: there is an implicit returning of 0, according to the standard, but only with main.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627678
• Total Posts
2978605

• 12
• 12
• 10
• 12
• 22