Help with simple program

Jul 26, 2014 at 11:26pm
Hi

I am a complete beginner with C++ and I am currently experimenting to improve my skills. After doing Hello world and a calculator programm Im trying my hand at programming a percentage calculator. I have tried loads of different work arounds but I cannot seem to get the program to * the total by 100. It keeps returning the score to 0. Could you please look at the below and tell me where I am going wrong:

#include "stdafx.h"
#include <iostream>
using namespace std;

int ProgramExplanationScore()
{
cout << "Welcome, this programe works out the percentage of your given score by the values that your provide" << endl;
cout << "Please enter your score: " << endl;
int nValue;
cin >> nValue;
return nValue;

}

int EnterTotal()
{
cout << "Please enter the total your wish to compare your score to: " << endl;
int nValue;
cin >> nValue;
return nValue;

}

int DoDivision(int Nx, int Ny)
{

return Nx / Ny;


}


void PrintResult(int TotalResult)
{
cout << "The percentage of your score is: " << TotalResult << endl;
}

int main()
{

int input1 = ProgramExplanationScore();
int input2 = EnterTotal();
int Total = DoDivision(input1, input2);
int TotalResult = Total * 100;


PrintResult(TotalResult);

}

Cheers
Jul 26, 2014 at 11:50pm
closed account (NUj6URfi)
Please use code tags in the feature. [/code] to end and [ code] to start.

Try

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
#include "stdafx.h"
#include <iostream>
using namespace std;

int ProgramExplanationScore()
{
cout << "Welcome, this programe works out the percentage of your given score by the values that your provide" << endl;
cout << "Please enter your score: " << endl;
int nValue;
cin >> nValue;
return nValue;

}

int EnterTotal()
{
cout << "Please enter the total your wish to compare your score to: " << endl;
int nValue;
cin >> nValue;
return nValue;

}

int DoDivision(int Nx, int Ny)
{

int ToBeReturned = Nx/Ny
return ToBeReturned;


}


void PrintResult(int TotalResult)
{
cout << "The percentage of your score is: " << TotalResult << endl;
}

int main()
{

int input1 = ProgramExplanationScore();
int input2 = EnterTotal();
int Total = DoDivision(input1, input2);
int TotalResult = Total * 100;


PrintResult(TotalResult);

}


Don't have my compiler on me and I am a bit rusty yet that should work.
Last edited on Jul 26, 2014 at 11:51pm
Jul 27, 2014 at 12:00am
Try using float or double instead of int. If the first input is smaller than the second, the result of the division will always be 0.

For example:
1
2
3
4
5
6
float DoDivision(int Nx, int Ny)
{
	float div = static_cast<float>(Nx) / static_cast<float>(Ny);

	return div;
}

You'll also need to change Total to a float.
Last edited on Jul 27, 2014 at 12:11am
Jul 27, 2014 at 12:10am
Those functions are uneeded.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
 using namespace std;

 int main()
 {
    double first, second;
    cout << "Welcome, this programe works out the percentage of your given score by the values that your provide" << endl;
    cout << "Please enter your score: " << endl;
    cin >> first;
    cout << "Please enter the total your wish to compare your score to: " << endl;
    cin >> second;
    cout << "The percentage of your score is: " << (first/second) * 100;
 }
Last edited on Jul 27, 2014 at 12:10am
Topic archived. No new replies allowed.