Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 20th, 2004, 2:32 PM   #1
JPPLAY
Newbie
 
Join Date: Oct 2004
Posts: 9
Rep Power: 0 JPPLAY is on a distinguished road
Two things how do I set it for a console program to not just close after I put in the input. Here the simple program. Also with how float isn't that powerful compared to double how come it takes so long to compile?

#include <stdio.h>
#include <iostream.h>

int main(int nNumberofArgs, char* pszArgs[])
{
float celsius;
cout << "Enter the temperature in Celsius:";
cin >> celsius;
float factor;
factor = 212 - 32;
float fahrenheit;
fahrenheit = factor * celsius/100 + 32;
cout << "Fahrenheit value is:";
cout << fahrenheit;
return 0;
}
JPPLAY is offline   Reply With Quote
Old Nov 20th, 2004, 3:51 PM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
#include <stdio.h>
#include <iostream.h>

int main(int nNumberofArgs, char* pszArgs[])
{
 float celsius;
 cout << "Enter the temperature in Celsius:";
 cin >> celsius;
 float factor;
 factor = 212 - 32;
 float fahrenheit;
 fahrenheit = factor * celsius/100 + 32;
 cout << "Fahrenheit value is:";
 cout << fahrenheit;

 cin.get(); // Add this from iostream, or getchar() from stdio.h

 return 0;
}
By asking for a single character from standard input, you cause a blocking read to pause the program. However, this simple solution can be defeated if there is already a newline character in the stream. Since all input in C++ is line based, cin.get() and getchar() will terminate when they see '\n'. You can solve that problem like so:
#include <stdio.h>
#include <iostream.h>

int main(int nNumberofArgs, char* pszArgs[])
{
 float celsius;
 cout << "Enter the temperature in Celsius:";
 cin >> celsius;
 float factor;
 factor = 212 - 32;
 float fahrenheit;
 fahrenheit = factor * celsius/100 + 32;
 cout << "Fahrenheit value is:";
 cout << fahrenheit;

 char ch;
 while ( cin.get ( ch ) && ch != '\n' )
  ;
 cin.get(); // Add this from iostream, or getchar() from stdio.h

 return 0;
}
The loop reads characters from the stream until a newline is encountered, at which point the next call to cin.get() will be a blocking read. There are other ways to "flush" the standard input stream, but this is the simplest.

>how come it takes so long to compile?
Sometimes it takes more effort to access a smaller type than a larger one. For example, most machines are byte-addressable, but it's far more efficient to access memory by the word. That's why the generally accepted wisdom is to use int and double as your primary types unless you have very special needs or you know for a fact that your system handles other types more efficiently.

However, all of that aside, whether you choose float or double has no real effect on compilation time. That depends on how smart your compiler is (by not recompiling unchanged code) and how many dependencies your code introduces (such as how many headers are included by iostream).
Eggbert is offline   Reply With Quote
Old Nov 21st, 2004, 1:09 PM   #3
JPPLAY
Newbie
 
Join Date: Oct 2004
Posts: 9
Rep Power: 0 JPPLAY is on a distinguished road
Quote:
Originally posted by Eggbert@Nov 20 2004, 08:51 PM
That's why the generally accepted wisdom is to use int and double as your primary types unless you have very special needs or you know for a fact that your system handles other types more efficiently.

Double is much harder on the computer then float. Whenever possible don't use double when you could use float.

Also to make that pause, the best way to me is to add
system("PAUSE");
you just need to remember to
#include <stdlib.h>
JPPLAY is offline   Reply With Quote
Old Nov 21st, 2004, 1:40 PM   #4
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>Double is much harder on the computer then float.
Prove it. Most processor FPUs do their internal work at double or long double precision, so it's fairly safe to say that double is at least as fast as float, and probably faster because the processor avoids the overhead required to convert from float to double and back again. Of course, float and double could be the same size, in which case they're equivalent. This backs up my claim that double should be used unless there's a good reason not to.

>Whenever possible don't use double when you could use float.
Perhaps if double is larger than float and you have a large number of them, such as an array then float is a better choice to save space.

>Also to make that pause, the best way to me is to add
Using system in this way isn't portable and is also a security risk.
Eggbert is offline   Reply With Quote
Old Nov 21st, 2004, 3:44 PM   #5
monkey8
Newbie
 
Join Date: Nov 2004
Posts: 12
Rep Power: 0 monkey8 is on a distinguished road
[quote]Originally posted by JPPLAY@Nov 21 2004, 06:09 PM
Quote:
Originally Posted by Eggbert,Nov 20 2004, 08:51 PM
system("PAUSE");
you just need to remember to
#include <stdlib.h>
You could run your programs from a console, then you wouldn't need to pause.

Since you only really need to pause on windows, "system("pause");" is an okay solution, just know that it will not work on other systems.

Since he is using c++, and not c, the correct c++ header is actually
#include <cstdlib>
monkey8 is offline   Reply With Quote
Old Nov 21st, 2004, 3:46 PM   #6
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>Since he is using c++, and not c, the correct c++ header is actually
At which point, the correct call is:
std::system ( "pause" );
Though our friend appears to be stuck in a world of pre-standard C++.
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC