Sign in to follow this  

Need Program to pause

This topic is 4730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need this program to pause, after is displays the celcius temperature. Code: /*A Jeremiah Cooper Production*/ #include <cstdlib> #include <iostream> using namespace std; float convert(float); int main() { float tempfer; float tempcel; cout << "Please enter the temperature in Fahrenheit: "; cin >> tempfer; cout << "\nHere's the temperature in Celcius: "; cout << tempcel << endl; return 0; system("PAUSE"); } float convert (float tempfer) { float tempcel; tempcel = ((tempfer - 32) * 5) / 9; return tempcel; }

Share this post


Link to post
Share on other sites
Ok, well the program isnt working like it should. I put in 212 as the fahrenheit, which should be 100 celcius. And I get 6.12428e-39...whats the problem?

Share this post


Link to post
Share on other sites
I was trying to tinker with the program and make it convert Celsius to Fahrenheit instead. I'm new so that's all I've been doing is tinkering. I ran into some trouble, and couldn't figure out a way to make it work.

Converting Fahrenheit to Celsius looks like this in his code:
tempcel = ((tempfer - 32) * 5) / 9;

And through my tinkering, my Celsius to Fahrenheit looks like this:
tempfer2 = ((tempcel2 * 9) / 5) + 32;

In my calculator (and in my head),
((212-32)*5)/9 = 100
and
((100*9)/5)+32 = 212

but in my program, they both work the same, making 212 into 100 on both equations.

Here is my code

#include <cstdlib>
#include <iostream>

using namespace std;
float convert(float);
float convert2(float);

int main()
{
float tempfer = 0.0;
float tempcel = 0.0;
float tempfer2 = 0.0;
float tempcel2 = 0.0;

cout << "Please enter a temperature in FAHRENHEIT: ";
cin >> tempfer;
tempcel = convert(tempfer);
cout << "\nHere's the temperature in CELSIUS: ";
cout << tempcel << endl;

cout << "Please enter another temperature in CELSIUS: ";
cin >> tempcel2;
tempfer2 = convert(tempcel2);
cout << "\nHere's the temperature in FAHRENHEIT: ";
cout << tempfer2 << endl;

system("PAUSE");
return 0;
}

float convert (float tempfer)
{
float tempcel;
tempcel = ((tempfer - 32) * 5) / 9;
return tempcel;
}

float convert2 (float tempcel2)
{
float tempfer2;
tempfer2 = ((tempcel2 * 9) / 5) + 32;
return tempfer2;
}




I can't seem to find anything on "float convert(float)" so, what is that? I'd like to read up on it in my book if you could name what it is.

Share this post


Link to post
Share on other sites
Quote:
Original post by alien3456
I can't seem to find anything on "float convert(float)" so, what is that?
At the top of the file he has float convert(float); as a function prototype or function declaration. At the bottom of the file, he has the full body for the function.

By the way, it'd be nice to see some better naming and smarter variable use:

#include <iostream>

float F2C(float F)
{
return (F - 32) * 5 / 9;
}

float C2F(float C)
{
return (C * 9 / 5) + 32;
}

int main()
{
using namespace std;

float F, C;
cout << "Enter Fahrenheit temperature: ";
cin >> F;
cout << "Celcius temperature equivalent: " << F2C(F) << endl;
cout << "Enter Celcius temperature: ";
cin >> C;
cout << "Fahrenheit temperature equivalent: " << C2F(C) << endl;

// uncomment the following line if you're using a low-grade IDE :P
// cin.get();
return 0;
}


Next task: turn them into two command line utilities, F2C and C2F, which run like so:
C:\> F2C 212
100C

C:\> C2F 100
212F

Share this post


Link to post
Share on other sites
Yes boss.

I changed what you asked for and made it so you could put as many arguments and it will spit out all the answer

F2C file

#include <iostream>
#include <sstream>

float F2C(float F)
{
return (F - 32) * 5 / 9;
}

float C2F(float C)
{
return (C * 9 / 5) + 32;
}

int main(int argc, char* argv[])
{


if (argc < 2) // make sure there is at least one argument.
{
std::cout << "no input temperatures\n";
exit (1);
}

float F;

for (int x = 1; x < argc; ++x)
{
std::stringstream ss;
ss << argv[x];
ss >> F;
std::cout << argv[x] << "F = " << F2C(F) << "C\n";

}

// uncomment the following line if you're using a low-grade IDE :P
// cin.get();
return 0;
}



and with minimal changes
c2f file

#include <iostream>
#include <sstream>

float F2C(float F)
{
return (F - 32) * 5 / 9;
}

float C2F(float C)
{
return (C * 9 / 5) + 32;
}

int main(int argc, char* argv[])
{


if (argc < 2) // make sure there is at least one argument.
{
std::cout << "no input temperatures\n";
exit (1);
}

float C;

for (int x = 1; x < argc; ++x)
{
std::stringstream ss;
ss << argv[x];
ss >> C;
std::cout << argv[x] << "C = " << C2F(C) << "F\n";

}

// uncomment the following line if you're using a low-grade IDE :P
// cin.get();
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by alien3456

but in my program, they both work the same, making 212 into 100 on both equations.

Here is my code
*** Source Snippet Removed ***


The functions should work perfectly as you expect them to, but you are calling the same function both times

tempcel = convert(tempfer);
and
tempfer2 = convert(tempcel2);

I think you need to change the second one to:
tempfer2 = convert2(tempcel2);

Hope this helps.

//end rant
CJM

Share this post


Link to post
Share on other sites

This topic is 4730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this