C++ Basic Function Help, PLS.
Having trouble trying to utilise a function in my C++ program that I wrote. It prompts the user for a mesurement to be converted to centimetres.
Two things I would like to know about it is would this program work and how could I use the 'return' feature in this program? Code:
#include<iostream> |
C program - very similar
#include <stdio.h>
float fnConvert(float inches); void main(void) { float x1=0, x2=0; printf("\nEnter number to convert to inches."); scanf('%f", x1); x2=fnConvert(x1); printf("\n %f centimeters in inches = %f \n"); return; } float fnConvert(float temp) { return(temp/2.54); } / * ******************************* to use the switch statement: with input variable "x" switch (x) { case 1: ...call function 1 break; case 2: ... call function 2 break; etc. default: break; } ******************************************************************************/ |
Man, that is so confusing. I am use to cout<<"" but so many people use scanf and printf. I think it's also because I don't know how to return multiple values yet, does anyone know a good guide on it that is easy to read examples(with not too advanced code)? Little bit lost, sorry I still would like some furture help.
Thanks anyway. P.S fastest reply I have come accross. |
Hmm, it might be easiest and more modular to create a number of functions, each with their own purpose. For example, this is how I would do a inches to centimetres function in C (it shouldn't be too different in C++ but I guess #define'd constants are frowned upon):
Code:
#define INCHES_FACTOR 2.54 Code:
int main() Code:
#define INCHES_FACTOR 2.54 Hope that gives you some food for thought. ~sind |
#include <iostream.h>
int main(void) { int iChoice; cout << "Enter in Choice : " << endl; cout << "Convert from inches to 1. feet, 2. centimeters" << endl; cin >> iChoice; cout << "You entered " << iChoice << endl; if ((iChoice < 1) || (iChoice > 2)) { cout << "error." << endl; return -1; } switch(iChoice) { case 1: cout << "Inches divided by 12 = feet" << endl; break; case 2: cout << "Inches times 2.54 = centimeters"<< endl; break; default: break; } cout << "Done." << endl; return 0; } |
Hi, you need to get books on generic programming concepts. It is not so much C/C++ that you need to learn, but the good old heart of lambda calculus programing concepts. C++ examples will also help you learn, nothing better than seeing something that works and working from this. May I suggest "Practical C++" By O'Reilly. A great book for learning programming and even deeper C++ incite.
Anyhow, as you might already know, you cannot return multiple values. You *can* however pass references to variables with side-effects meaning this. Code:
int some_function(int& integer, int& cool) Code:
int main(void) "Thing". But this could be a struct or Object (in C++) that contains many data. |
Quote:
A few things I noticed from your reply: Quote:
cout and its family are better for C++ programming for a variety of reasons I wont get into right now so stick with cin/cout, but maybe try and learn a bit about printf and scanf (hint: research "format strings"). Quote:
Heres a simple function that takes 2 integer values and returns the sum, along with a simple C++ program that uses the function. Code:
#include <iostream> Note: I will use a C++ class to show you how to do this, simply out of my own preference. A C-style "struct" would also work well in this situation, but I'll try not to confuse you any more ;) One more thing, I dont want you to get confused on constructors yet, but I'm going to add a very simple constructor to the class. Every time an object gets created from this class, this "constructor" will get called ONCE and only once. It will set all values to 0, this is just good practice (initializing all your variables to appropriate values). Code:
#include <iostream> |
Wow, that was totaly amazing. It was like the code turned into a 3D game and jumped out at me saying, "This is how I work". Damn, I'm actually starting to understand now, thanks for all of your help.
I'm going to get back to learning some more 00P language :). Once again, thanks.:) ::EDIT:: I have attached my source code for the centimetre conversion program. I was hoping that someone could correct it, as it isn't working for some reason. But if they could also leave the structure as it is(like no, printfs or functions) because I want to see what I've done wrong. Just to correct the syntax or similar. In advance thanks from noo:newbie: programmer. P.S This is a great forum, can't wait until I finished backing up my hard drives and install Linux because you people really seem to understand what you're talking about :) Code:
#include<iostream> |
Quote:
Code:
cout << "The conversion of " << InchesConversion << " inches to centimetres is " << InchesConversion * OneInch; |
Actually did that even compile?
InchesConversion * OneInch = InchesConversion; If so, what would the assignment be? Interesting. Perhaps evaluated and thrown out. |
Quote:
Also take a look at the break statements - no semicolons. Also, this line is there somewhere: Code:
void ConversionTime(); Quick question: is this legal C++: Code:
float f1 = 42.651; |
Quote:
|
Yes, all depends on the compiler. It may try to convert the f1 to ASCII char array, and try to output it before applying the '*' operator. This is solved, and is better written code by explicitly placing the parenthesis on this evaluation.
BETTER YET: Do you think you may have the need to use that result later in your code? If so, please feel free to use a variable to hold this: Code:
|
OK I was trying to do something dumb in my tired state (cout << something *= somethingElse ...), so I made those fixes and heres the code. I only implemented 2/5 switch cases, and I didnt change any variable names (although i was very tempted) and tried to leave the code as much "as-is" as I could, while still showing you a few new things. Hope you enjoy, I tested this in VC++6.0, but it should work in any C++ compiler.
Code:
#include <iostream> |
Just a programmers note, we usually declare named constants as to avoid magic numbers. For example: Will everyone remember what type 2 refers to without looking through much of your code? Probably not. anyhow that is really easy, just
do a const int FEET_CONVERSION = 2; and so on (outside of main is fine as global constants are OK). This helps make your code readable. so inside your switch could be case FEET_CONVERSION: /* STUFF*/ break; Makes life easier for you and anyone who dares modify the code. |
All times are GMT -5. The time now is 07:39 AM. |