I am having all kinds of problems with my assignment. I have worked on it for hours and cannot get it to work properly. I am new to programming and it gets frustrating very fast for me. Here is the code I am currently working and i have attached my assignment with what the output should look like... any help would be wonderful...

//Assignment 4: Functions
//By: Curtis Davenport 02/13/08

#include <iostream>
#include <cmath>

using namespace std;

int temperatureF;
int temperatureC;
int choice;
int result;
int FahrenheittoCelsius(int);
int CelsiustoFahrenheit(int);



int main()
{
    bool done;
    done = false;

    while(done == false)
    {

            cout << "Please select from the following \n";
            cout << "Fahrenheit-to-Celsius [1], Celsius-to-Fahrenheit [2], Quit [3] \n";
            cin >> choice;



        if (choice == 1)

            cout << "Enter the temperature in degree F to convert: << temperatureF <<";
            cin >> temperatureF;

            cout << "The temperature you have entered in F," << temperatureF << "is 5*(temperatureF-32)/9 C \n";

        if (choice == 2)

            cout << "Enter the temperature in degree C to convert: << temperatureC <<";
            cin >> temperatureC;

            cout << "The temperature you have entered in C," << temperatureC << "is ((9/5)*temperatureC)+32 F \n";

        if (choice < 1)  
            cout << "Incorrect Choice. Try again.";

        if (choice > 3)
            cout << "Incorrect Choice. Try again.";


        if (choice == 3)
        {
            bool done = true;
        }


    }

    return 0;
}
int FahrenheittoCelsius(int)
{
    result = 5*(temperatureF-32)/9;

    return result;
}

int CelsiustoFahrenheit(int)
{
    result = ((9/5)*temperatureC)+32;

    return result;
}

Recommended Answers

All 19 Replies

Firstly, please indent code for readability, and use the code tags (see the Announcement about BB tags)

You write the conversion functions, but never use them. You need to put curly braces { } around the code that makes up each if block. You cannot put the calculation for temp conversion inside the double quoted string text, the expression must be a separate output action. But, you should be using your function, not doing the work inline. So here's a fixed up bit:

if (choice == 1)
{
     cout << "Enter the temperature in degree F to convert: ";
     cin >> temperatureF;

     cout << "The temperature you have entered in F," << temperatureF << "is  "
            << FahrenheittoCelsius( temperatureF )  << " Celsius" << endl;;
}

Last comment (for now) - your division of 9/5 will not give you correct results. Remember that division of integers gives an integer result, 1 in this case. Change one or both operands to a floating point value for the accuracy you need. Then you'll need to do some typecasting of the final result back to int to satisfy your function's return type.

Ok I have that working correctly the spaces are off in areas but i can fix that later. My loops are all mixed up and it does not work in proper order. My selections 1 ,2 and 3 are not working right. Can you run it in your compiler and give me some pointers to fixing this, i am lost on knowing where to start....

#include <iostream>
#include <cmath>


using namespace std;


int temperatureF;
int temperatureC;
int choice;
int result;
int FahrenheittoCelsius(int);
int CelsiustoFahrenheit(int);


int main()
{
bool done;
done = false;


while(done == false)
{


cout << "Please select from the following \n";
cout << "Fahrenheit-to-Celsius [1], Celsius-to-Fahrenheit [2], Quit [3] \n";
cin >> choice;


if (choice == 1)
{


cout << "Enter the temperature in degree F to convert:";
cin >> temperatureF;


cout << "The temperature you have entered in F," << temperatureF << "is" <<FahrenheittoCelsius(temperatureF) << "Celsius" <<endl;;
}


if (choice == 2)
{
cout << "Enter the temperature in degree C to convert:";


cout << "The temperature you have entered in C," << temperatureC << "is" <<CelsiustoFahrenheit(temperatureC) <<"Fahrenheit" <<endl;;
}


if (choice < 1)
{
cout << "Incorrect Choice. Try again.";
}


if (choice > 3)
{
cout << "Incorrect Choice. Try again.";


}
if (choice == 3)
{
bool done = true;
}



}


return 0;
}
int FahrenheittoCelsius(int)
{
result = 5*(temperatureF-32)/9;


return result;
}


int CelsiustoFahrenheit(int)
{
result = ((9/5)*temperatureC)+32;


return result;
}

I refuse to read your code until it's properly formatted.

Like Joe, I shouldn't respond till you format better (INDENT).

Choice 2 - do I get to enter some temp to be converted?

Choice 3 - you declare a variable there, which is masking the one of the same name that you really meant to use to halt the loop.

No more help till your code is more readable.

commented: Thanks for the BB Code tip. +1

I have my code written very nice on my compiler with proper spacing and good alignment, how do i put it on daniweb so it looks like my compiler. everytime i make a thread and space it right the preview of the post looks messed up....

Check out this link on how to do it (I'm linking rather than showing you myself because I myself haven't figured out how to make it display the words "code=C++" and "/code" as text without it getting interpreted as code).
http://www.daniweb.com/forums/post535004-2.html

Check out this link on how to do it (I'm linking rather than showing you myself because I myself haven't figured out how to make it display the words "code=C++" and "/code" as text without it getting interpreted as code).

Use the noparse tag. E.g.

[noparse][code=c++] code goes here [/code][/noparse]

Turns into

[code=c++] code goes here [/code]

To show the noparse tag in your post, use another noparse tag.

[noparse][noparse][code=c++] code goes here [/code][/noparse][/noparse]

The joys of BBCode. :P

commented: Thanks. Useful. +1

It's actually code=cplusplus you guys!

You would use the same language names as in our code snippets section.

I have my code written very nice on my compiler with proper spacing and good alignment, how do i put it on daniweb so it looks like my compiler. everytime i make a thread and space it right the preview of the post looks messed up....

1) You could read The Rules as requested when you registered.
2) You could read the post titled "Read This Before Posting" at the top of the forum
3) You could read the words on the background of the box you typed all your messages in.
:icon_rolleyes:

Check out this link on how to do it (I'm linking rather than showing you myself because I myself haven't figured out how to make it display the words "code=C++" and "/code" as text without it getting interpreted as code).

You mean like enter code? :icon_wink:

commented: Cool BB Code +1

joeprogrammer and vmanes, thanks for the tutorial:

[code=c++] // your code here

[/code]
Now I can tell people to use code tags too!:)

WaltP, you get the prize for creativity. I'm gonna have to research why it works, but it does:

[co[B][/B]de]enter code[/co[B][/B]de]

yields

[code]enter code[/code]

Ok here is my code with more clarity finally. I am having trouble with the loops, they are not working correctly I want the program to do certain things when certain numbers are entered but any number works in my program for some reason i just cant figure it out. I have attached what i am trying to do....thanks for the help so far.. i am just having a heck of a time understanding this stuff.....

#include <iostream>
#include <cmath>

 using namespace std;

 int temperatureF;
 int temperatureC;
 int choice;
 int result;
 int FahrenheittoCelsius(int);
 int CelsiustoFahrenheit(int);


int main()
{
            bool done;
            done = false;

            while(done == false)
            {

                    cout << "Please select from the following \n";
                    cout << "Fahrenheit-to-Celsius [1], Celsius-to-Fahrenheit [2], Quit [3] \n";
                    cin >> choice;

            }

            if (choice == 1)
            {

                cout << "Enter the temperature in degree F to convert:";
                cin >> temperatureF;

                cout << "The temperature you have entered in F," << temperatureF << "is"    <<FahrenheittoCelsius(temperatureF) << "Celsius" <<endl;;
             }

             if (choice == 2)
             {
                cout << "Enter the temperature in degree C to convert:";

                cout << "The temperature you have entered in C," << temperatureC << "is"   <<CelsiustoFahrenheit(temperatureC) <<"Fahrenheit" <<endl;;
             }

             if (choice < 1)
             {
                cout << "Incorrect Choice. Try again.";
             }

             if (choice > 3)
             {
                cout << "Incorrect Choice. Try again.";

             }
             if (choice == 3)
             {
                bool done = true;
             }


}

return 0;
}
{
       int FahrenheittoCelsius(int)

       result = 5*(temperatureF-32)/9;

return result;
}

int CelsiustoFahrenheit(int)
{
       result = ((9/5)*temperatureC)+32;

       return result;
}

I think your bracket in line 28 is far too high. It ends the while loop too soon. In fact I think you can delete it completely as you have one in line 62. Do you have an extra one? You want the while loop to end at line 62, right? I think delete that bracket on line 28 and the one on line 66.

After that, I think you do not want to redeclare the boolean variable "done" here:

if (choice == 3)
             {
                bool done = true;
             }

You have declared "done" higher in the program and in a wider scope. The code directly above creates a NEW local variable called "done" which is DIFFERENT from the "done" variable that your while loop tests. The new variable immediately goes out of scope and the above code has no effect on the variable named "done" that you are checking in your while loop. Thus that while loop ends up as an infinite loop. In the above code block, take the "bool" out like this and the "done" that is checked in the while loop will be set to true, which is what I believe you want.

if (choice == 3)
             {
                done = true;
             }

I think you actually want the bracket that was on line 66 AFTER the function header rather than before, so don't delete it, just move it?

{                 // bracket from line 66
       int FahrenheittoCelsius(int)

       result = 5*(temperatureF-32)/9;

return result;
}

Should be here instead?

{          // delete this bracket
       int FahrenheittoCelsius(int)
{         // put it here instead

       result = 5*(temperatureF-32)/9;

return result;
}

Maybe if you'd format your code properly you wouldn't have this bracket problem. Give it a try, please.

Man you have to keep in mind i have been doing this for only two months. I am trying really hard to learn the ropes, so please refrain from giving me crap about writing code and formatting because i do what i think is "correct formatting". I am trying my very best and appreciate all of the help, because this stuff is very overwhelming to me and i am trying hard to understand it.

Ok I have this almost working correctly i just need some cosmetic things to make it look nicer. If you look at my attachment he has his spacing nice on the output. I dont know how to space them to look nice. And also when i use choice 2 and enter a celsius temperature i do not get the correct answer but i have the proper formula....

//Assignment 4: Functions
//By: Curtis Davenport 02/13/08

#include <iostream>
#include <cmath>

using namespace std;

float temperatureF;
float temperatureC;
int choice;
float result;
float FahrenheittoCelsius(int);
float CelsiustoFahrenheit(int);



int main()
{
	bool done;
	done = false;
	
	while(done == false)
	{
	
			cout << "Please select from the following \n";
			cout << "Fahrenheit-to-Celsius [1], Celsius-to-Fahrenheit [2], Quit [3] \n";
			cin >> choice;

	
	
		if (choice == 1)
		{
			
			cout << "Enter the temperature in degree F to convert:";
			cin >> temperatureF;

			cout << "The temperature you have entered in F," << temperatureF << "is" << FahrenheittoCelsius (temperatureF) << "Celsius" <<endl;;
		}

		if (choice == 2)
		{	
			cout << "Enter the temperature in degree C to convert:";
			cin >> temperatureC;
			
			cout << "The temperature you have entered in C," << temperatureC << "is" << CelsiustoFahrenheit (temperatureC) << "Fahrenheit" <<endl;;
		}
	
		if (choice < 1) 
		{
			cout << "Incorrect Choice. Try again. \n\n";
		}

		if (choice > 3)
		{
			cout << "Incorrect Choice. Try again. \n\n";

		}
		if (choice == 3)
		{
			 done = true;
		}
	
	}
		
	

	return 0;
}
float FahrenheittoCelsius(int)
{
    result = 5*(temperatureF-32)/9;

    return result;
}

float CelsiustoFahrenheit(int)
{
	result = ((9/5)*temperatureC)+32;

	return result;
}

On lines 39 and 47 i need spaces between where it says the temperature C and F and when it displays the converted temperature. And also when i run to convert to Fahrenheit from celsius it does not give me the right answer...my attachment has the way it should look like....thanks for anymore help on this ongoing task....

float CelsiustoFahrenheit(int)
{
    //
    // This fails because you are dividing using two integers here,
    // 
    // result = ((9/5)*temperatureC)+32;
    //
    // use the following instead
    //
    result = ((9/5[B].0[/B])*temperatureC)+32;
    return result;
}

You might want to run the following snippet to see the difference

cout 
	<< "9/5 using integers is [ " << 9/5 << " ]" << endl 
	<< "9/5 using floating point is [ " << 9/5.0 << " ]" << endl;

Man you have to keep in mind i have been doing this for only two months. I am trying really hard to learn the ropes, so please refrain from giving me crap about writing code and formatting because i do what i think is "correct formatting". I am trying my very best and appreciate all of the help, because this stuff is very overwhelming to me and i am trying hard to understand it.

I'm not giving you crap. You don't have to get defensive. If you are trying to "learn the ropes", doesn't that include learning how to format your code correctly? Especially from a professional programmer? If you are new to programing, how can you expect to have "correct formatting" yet? I'm simply pointing out one way to learn it.

If you format your code properly
1) You will be able to follow it easier
2) We can follow it easier
3) We can give you better and faster responses because the code's easy to read.

Thanks everyone for all of your help, I got it to work correctly. Slowly getting a hold on this stuff. I have another assignment due this friday already, might have to give another ring if i get completely stuck. I really appreciate the help though this is a great site for learning programming!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.