Completely Stuck

Pages: 123
show code , and no. you only can return once.
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
#include <iostream> //used for cout and cin statements.
#include <string> //used for potential strings
#include <iomanip> //used for potential manipulation of data outcomes
#include <fstream> //needed for saving data file

using namespace std;

int FracPart(float n); //function prototype

int main()
{
float n;
cout<< "Please enter a mixed number with six or more decimal places. \n ";
cin>> n;
cout<<n<<" is the decimal number you have entered. \n";
cout<<FracPart(n);
}
int FracPart(float n)
{
	int x;
	x=(n*(10*10*10*10*10*10))+(0.5);
	

return x;
cout<< "\n";
} 

int WholePart(int x)
{
cout<< "\n";
return x;
cout<< "\n";

}


That is everything the cout<< does not work
Which ones, the couts after the returns?
A function's execution terminates at the return, going back to (aka returning to) the calling function. Those lines are never reached.
On your problem with the missing \n, once a function hits a return, the function exits. You would need to code another \n in main.

and idealy, your functions should cout anything unless that's what they were designed to do.

Since your WholePart function was designed to just give you the int value of a float, it shouldn't cout anything at all. Do all your output in main (or another function that was designed solely for output ;P)
WholePart is your decimal
cout in Wholepart are not needed.
do all that in main.
Ok so since i have return x in the FractPart that is just a waste of space correct?
FractPart needs to be rewritten.

do all your printing in main. Not in functions.

in main , Sample
1
2
3
4
5
float n , f;
cout<< "Please enter a mixed number with six or more decimal places. \n ";
cin >> n;
cout << "you enter : " << n << endl << endl; // endl same things as "\n"
cout << WholePart(n) << " whole part of decimal." << endl << endl;

Last edited on
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
#include <iostream> //used for cout and cin statements.
#include <string> //used for potential strings
#include <iomanip> //used for potential manipulation of data outcomes
#include <fstream> //needed for saving data file

using namespace std;

int FracPart(float n); //function prototype
int WholePart(int x);
int x;
int main()
{
float n;
int x;
cout<< "Please enter a mixed number with six or more decimal places. \n ";
cin>> n;
cout<<n<<" is the decimal number you have entered. \n";
cout<<FracPart(n);
cout<<"\n";
cout<<WholePart(x);

}
int FracPart(float n)
{
	int x;
	x=(n*(10*10*10*10*10*10))+(0.5);
	

return x;

} 

int WholePart(int x)
{

return x;


}


Ok so this is everything i did. Hopefully did it correctly. Running it works fine until everything is done then says it tried to use x without being initialized
The x in the function is a totally different entity from the one in main. You aren't assigning the return of FracPart to the x in main, so what are you doing with it?
Last edited on
Ok took the int x; out in main. That got rid of the error/crash.
However not I am getting an extra 0 on its own line.
Is this because of WholePart returning x with it not being defined there?

Edit: After tweeking it i see that WholePart is sending the 0 back. Shouldent it be sending the value back instead?
Last edited on
Show the new code.
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
#include <iostream> //used for cout and cin statements.
#include <string> //used for potential strings
#include <iomanip> //used for potential manipulation of data outcomes
#include <fstream> //needed for saving data file

using namespace std;

int FracPart(float n); //function prototype
int WholePart(int x);
int x;
int main()
{
float n;
cout<< "Please enter a mixed number with six or more decimal places. \n ";
cin>> n;
cout<<n<<" is the decimal number you have entered. \n";
cout<<FracPart(n);
cout<<"\n";
cout<<WholePart(x);
cout<<"]n";

}
int FracPart(float n)
{
	int x;
	x=(n*(10*10*10*10*10*10))+(0.5);
	

return x;

} 

int WholePart(int x)
{
return x;


}


The WholePart seems pointless since it only returns 0 because nothing is there. Shouldent the fact part be the WholePart?
Get rid of the int x in FracPart; it's obscuring the global x on which you are calling WholePart.
Global variables are a messy solution. You could have just had
1
2
x = FracPart(n);
cout << x;
Why would whole return 0 unless what you input was something <1 but >0?
@tummychow Thanks :]
Now i have it returning a value. Except it is the exact same. So WholePart=FracPart. Why would i need them to be the same.
"The function FracPart returns the fractional part of the mixed number that was passed to it by main." - assignment

@oghmaosiris i use .123456 every time this way i can see what changes. It now returns 2x so it shows on screen
123456
123456press any...
Last edited on
I understand that. But what's x supposed to be then? (in main and in the function)
Well, then yeah, it'll return 0. You won't have a whole part if you're always testing a fraction.

You should test both aspects of the code.

And do what Drake said, put x in main and don't use it globally.

Also, you put "]n" instead of "\n" on the last cout.
Last edited on
@tummychow
x would be the value they entered multipled by 10^n. Since i always need atleast 6 decimal spots it would be x*10^6

Is this what you mean?
I think that should go down to the WholePart
Which means FractPart is something else.

@oghmaosiris

Not sure what you mean.

Should i Enter 1.123456? Is this what you mean?
Also i caught the ] instead of \ after i posted it :] Thanks though!
Last edited on
I'm so confused by this solution. What's the output you would put in, and what would come out?
Judging from your original assignment you could (I know, type conversion) here's what I would do:
use modf (in cstdio iirc) to get the remainder of somefloat / 1 (the decimal part). Cast the float to an integer (float somenum = someint;) to get the integer part. Then you can do your rounding.
Heh if your confused that makes two of us. The input is anything as long as it has 6 or more decimal places.

I have to have a 3 functions
FractPart()
WholePart()
Rounder()

Working on the WholePart() right now. Trying to get it to detect the Integer.
Not working very well
Last edited on
Pages: 123