Sign in to follow this  

help

This topic is 3715 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

Im trying to make a program for doing log in c++. Im still novice but i wanted to give it a try...but something is not right. #include <iostream> #include <stdio.h> #include <math.h> float firstNumber(float inPut); int main() { using std::cout; using std::cin; int inPut; cout << "First number here: "; cin >> inPut; cout << "The log is: " << firstNumber(inPut) << "\t"; system("Pause"); return 0; } float firstNumber(float inPut) { float firstNumber; float input; firstNumber = inPut; inPut = log10(firstNumber); return inPut; } thats only the beginning ( i know its horrific to look at, but im learning ><) comment if you know whats wrong!

Share this post


Link to post
Share on other sites
1) You should include source code in [ source ] tags.

2) #include <stdio.h> is a worthless line.

3) Your function and variables names aren't very good. firstNumber is a worthless name for a function and a variable; it doesn't convey any meaning of what it's supposed to do. Naming a function and a variable the same thing is not a good idea.

4) firstNumber() takes a float. You're providing it with an int.

5) What is the purpose of "float input;" in firstNumber()? It doesn't do anything.
[/source]

Share this post


Link to post
Share on other sites
Well, I don't see why the program shouldn't work on Windows machines, but it sure is fugly [wink].

First of all, get rid of the habit to end your programs with a system call.
This is just not right. Not only does it prevent the program from running on other operating systems, it is also misplaced. I take it you use Visual Studio and want to keep the console window from disappearing when the program finishes.
You only want this behaviour during a debug session, so simply place a breakpoint at the very last line of your program. The debugger will step in and prevent the console window from closing.

Second of all, you obviously confused the newline character with the tab - replace "\t" with "\n" or std::endl.

Next, the "firstNumber" function is both dispensable and misleading - its name actually hides the purpose.
You should also verify the user input, since users love to crash programs by feeding them bad input. So making sure that the input is greater than zero seems like a good idea. Please note that the input will also be zero if you enter some random text.

And last but not least you should always use the [source] [/source] tags when posting code and indent your code properly.

HTH,
Pat

Share this post


Link to post
Share on other sites
Thanks for all the replies, and sorry for the "fugly" code. I take everything u guys said into consideration.

I'm writing this program for my pre-calc class, it finds the log base(10) of the first number, then finds the log base(10) of the second number and performs the chang-of-base.

thanks for the help:)

Share this post


Link to post
Share on other sites

This topic is 3715 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