Hello Reem Alnuaimi,
This is just a copy and paste, so do not take it the wrong way:
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
Your program is difficult to understand and follow. You said:
I used X= as my input
y=Is the returned integer part
z=is the return for the decimal part
w= is the return of the string sign
|
This is nice, but really should be in the program as a comment. The alternative is to avoid single letter variables and use a name that better describes the variable. It has been said: this should be a noun or sometimes a verb that describes what the variable is or does.
The following code is an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include<iostream>
#include<iomanip>
#include<string>
#include<cmath>
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
int getIntpart(int intNum);
float Decpart(int intNum);
std::string getsign(int intNum);
int main()
{
int intNum{};
std::cout << std::fixed << std::showpoint << std::setprecision(4); // <--- Only needs done once.
while (intNum != -9999)
{
std::cout << "\n Enter any number (-9999 to end): ";
std::cin >> intNum;
std::cout << "\n " << getIntpart(intNum) << ", " << Decpart(intNum) << ", " << getsign(intNum) /*<< std::endl*/;
std::cout << std::endl; // <--- Changed for testing. Makes a good break point.
}
return 0; // <--- Not necessary, but also makes a good break point.
}
int getIntpart(int intNum)
{
//int y = (int)abs(intNum); // <--- Do not need to define "y" the return is all you need. Also the type cast
// is not needed as the variable used is already an "int"
return abs(intNum);
}
float Decpart(int intNum)
{
float floatNum = static_cast<float>(abs(intNum)); // <--- Here the type cast could be used, but it is not
// necessary as the "int" will store in the "float" without
// the type cast.
//z = abs(intNum) - (int)abs(intNum); // <--- It is already an "int". You do not need to type cast it.
// Given intNum = 10 this would look like z = 10 - 10, so z would be 0.000000.
// If you are expecting something more you need to do something different.
return floatNum;
}
std::string getsign(int intNum)
{
std::string word;
if (intNum > 0)
word = "positive";
else if (intNum < 0)
word = "negative";
else
word = "neutral";
return word;
//return intNum >= 0 ? "positive" : "negative"; // <--- The above could be as simple as this.
}
|
Be sure to read the comments in the code.
Although the minimum requirement for a prototype is to list the variable(s) type. I find it helpful is you include the variable(s) name(s). This makes it easier to write the prototype by coping the function definition and pasting it then put the semi-colon at the end.
In "main" the variables that you used; "y", "z" and "w" are not needed nor do they need to be passed to the functions. Also all you do is pass these variables to the functions, but never use them in "main". if needed they are better defined in the function.
As the comment says line 18 only needs done once and will affect how floating point numbers are used in the "cout" statement. This is good until it is changed somewhere.
I changed the while loop a bit along with the prompt. What you had could see the prompt scroll off the screen.
Changing the prompt as I did by removing the "\n" and "endl" this puts the "cin" statement on the same line as the prompt and makes it much easier to understand. With what you have the "cin" is two lines below the prompt leaving one to wonder what it is waiting for.
Line 25 calls the functions to output their return value. This is why "y", "z" and "w" are not needed in "main".
The function "getIntpart" is a bit misleading. Int part of what? You send the function an "int". This is only a whole number. There is no part unless you consider the " - " of a negative number to be a part.
As you can see this function can use only one line, the return statement. Should you want to use a variable I left defining "y", did not bother changing the name as it is not really needed, as an example of what you could do.
The function "Decpart". Should I take this name as what you expect the return value should be, only what is to the right of the decimal point? As you have the function now the only returned value will always be "0.00000000". Playing with the function I have it returning the "intNum" value stored in a "float" which makes it a floating point number. As the comments starting on line 42 alludes to the only value the function can return is "0.000000000...".
The last line of the "getsign" function is a simpler way of writing the if/else if statements. I believe that (0) zero is generally considered positive most of the time. But if you want (0) zero to be considered "neutral" keep the if statement.
These are suggestions to get you thinking and show you what could be done.
Hope this helps,
Andy