Hi, I just started learning C++ a few days ago and I wanted to get some feedback on this program I just made. It basically asks the user to input the temperature in fahrenheit or celsius, and it gives the conversion. Yes, I know, basic, nooby, but so far that's the biggest thing I've ever done so far ._.
#include <iostream>
usingnamespace std;
float celsius;
float fahrenheit;
void convertF()
{
celsius = ((fahrenheit - 32) * 5) / 9;
}
void convertC()
{
fahrenheit = (((celsius * 9) / 5) + 32);
}
int main()
{
unsignedshort choice;
cout << "Welcome to the temperature converter.\n";
cout << "Please type '1' for Celsius to Fahrenheit conversion." << endl;
cout << "Or, type '2' for Fahrenheit to Celsius conversion." << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Please enter your temperature in Celsius.\n";
cin >> celsius;
convertC();
cout << "\n";
cout << "Computing...\n\n";
cout << "The temperature in Fahrenheit is " << fahrenheit << ".\n";
cout << "Press any key to terminate the program." << endl;
cout << endl;
break;
case 2:
cout << "Please enter your temperature in Fahrenheit.\n";
cin >> fahrenheit;
convertF();
cout << "\n";
cout << "Computing...\n\n";
cout << "The temperature in Celsius is " << celsius << ".\n";
cout << "Press any key to terminate the program." << endl;
cout << endl;
break;
default:
cout << "That is not an option!" << endl;
cout << "Please close the program and try again." << endl;
break;
}
return 0;
}
..and if you want to laugh at my noob calc then you can do that too *shrinks*
Now, for the calculator, the main question I'm asking myself is "How can I go back to the main screen without terminating the program?"
If anyone has an answer please share. :P
#include <iostream> // prep.
usingnamespace std; // put so that I don't have to go std:: before every iostream command
unsignedshort choice; // global variable choice; the user command variable
void greeting()
{
cout << "Welcome to Calculator Plus v1.05. Here, you can quickly calculate large numbers!\n\n";
cout << "Commands:\n\n";
cout << "(1) About (2) Addition (3) Subtraction (4) Multiplication (5) Division\n\n";
cout << "(0) Quit\n\n";
cin >> choice;
}
int main()
{
greeting();
switch (choice)
{
case 1: // the about screen
cout << "\nCalculator Plus created by OJK. 2011. \n";
cout << "To go back to the main screen terminate the program and start again." << endl;
break;
case 2: // the case for addition
float firstAdd;
float secondAdd;
cout << "\nPlease enter the first number to add: " << endl;
cin >> firstAdd;
cout << "Enter the second number: " << endl;
cin >> secondAdd;
cout << "The sum is " << firstAdd + secondAdd << "." << endl;
cout << "To go back to the main screen terminate the program and start again." << endl;
break;
case 3: // case for subtraction
float firstSub;
float secondSub;
cout << "\nPlease enter the first number to subtract: " << endl;
cin >> firstSub;
cout << "Enter the second number: " << endl;
cin >> secondSub;
cout << "The difference is " << firstSub - secondSub << "." << endl;
cout << "To go back to the main screen terminate the program and start again." << endl;
break;
case 4: // case for multiplication
float firstMul;
float secMul;
cout << "Please enter the first number to multiply: " << endl;
cin >> firstMul;
cout << "Enter the second number: " << endl;
cin >> secMul;
cout << "The product is " << firstMul * secMul << "." << endl;
cout << "To go back to the main screen terminate the program and start again." << endl;
break;
case 5: // case for division
float firstDiv;
float secDiv;
cout << "Please enter the first number to divide: " << endl;
cin >> firstDiv;
cout << "Enter the second number: " << endl;
cin >> secDiv;
cout << "The quotient is " << firstDiv / secDiv << "." << endl;
cout << "To go back to the main screen terminate the program and start again." << endl;
break;
default: // in case the user does not enter a valid command
cout << "Unrecognized command. Terminate the program and start again." << endl;
break;
}
return 0; // mandatory return function
}
by chance are you reading C++ for dummies. - because this is the first example.
but, good. keep it up. good array of functions and variables for a noob lol.
dangg lol, thats a pretty big code for a temperature converter. ima begginer too and i started a few days ago aswell, althought ur a lot smarter than i am. ive written a temperature converter as well, but using another book(C++ without fear), it was one of the first examples in the book, but i didnt use void, unsigned short, or float identificators, i just used double. although your program is lot more indepth than mine was. Mine just terminated after 1 conversion and it could only go one way(i hadnt learned how to use loops at the time). this is pretty awesome and although it may not mean much; im impressed you know how to do this after just a few days, im struggling to grasp things like functions and some of the terminology used for programming.
Way I found works great for learning, is taking some example problem (like the one you got goin) and modify it with more features and what not. tdeloggio has a good point, what if a user doesn't enter 1 or 2? He/she would still get the F to C conversion. One little tip, you dont need that first return statement in there.
no I don't use c++ for dummies although it's funny that this is the first example :o
if the user enters 3, I guess it'd do something completely random, I still have much to learn. The global variables just made it easy but I'll try ^^'
EDIT: Okay, here's the revised copy. I was trying to fix the program based on what tdel said, and I just learned about switch/case statements so here goes :D it basically fixes the problem where it activates something different if a user enters something other than 1 or 2, and also gets rid of some global variables. I couldn't remove the float celsius and fahrenheit because the compiler warned me of an uninitialized variable, so I just stayed safe.
Hey just one quick suggestion for you simpleashat.
In your calculator, instead of only allowing the user to input two numbers why not let them enter as many as they wish until the user inputs something like -1.
I will do one quick program showing you what i mean.
#include <iostream>
usingnamespace std;
int main()
{
//declare variables
int number;
int numbertotal = 0;
cout << "Please input the first number to be added or press -1 to quit: ";
cin >> number;
while(number != -1){ //use a while loop to decide when the user wants to exit the program
numbertotal += number; //keep adding the numbers entered and store them in a variable
cout << "Enter another number or press -1 to quit: ";
cin >> number;
}
cout << numbertotal; //output the total
}
Hope that helps and also keep programming, you are doing really well! :D
note: you would do some kind of sentinel value in here, like a if user doesnt enter a number then exit the loop. I just did (1 != 2) for example purposes.