Hi all I have written a program to issue a bursary according to the amount of modules passed in 2007 and the amount registered for in 2008 , basically
if the student did not pass any modules in 2007 :no bursary
if the student passed 1 or 2 modules in 2007 and is registered for atleast 1 module in 2008 the bursary will be R300.
otherwise the bursary is equal to the number of modules passed in 2007 + the number of modules registered in 2008 times 100.
but my code keeps returning 300 , can anyone help?

CODE: [ //Program calculating Bursarys.

#include <iostream>
using namespace std;
float bursary(int noModsPassed07 ,int noModsReg08)
{
if (noModsPassed07 = 0)
return 0;
if (noModsPassed07 >0 && noModsPassed07 <= 2 && noModsReg08 >= 1)
return 300;
else
return (noModsPassed07 + noModsReg08 * 100);
}

int main()

{


int noModsPassed07, noModsReg08;
float burs;


cout << "Enter number of modules passed in 07 followed by the number registered in 08 ";
cin >> noModsPassed07;
cin >> noModsReg08;
burs = bursary(noModsPassed07, noModsReg08);


cout << "With " << noModsPassed07 << " modules passed in 2007 and " << noModsReg08 << " modules registered in 2008 , you get a bursary of R" << burs << endl;


return 0;
} ]

#include <iostream>
using namespace std;
float bursary(int noModsPassed07 ,int noModsReg08)
{
if (noModsPassed07 = 0)
return 0;
if (noModsPassed07 >0 && noModsPassed07 <= 2 && noModsReg08 >= 1)
return 300;
else
return (noModsPassed07 + noModsReg08 * 100);
}



int main()

{


int noModsPassed07, noModsReg08;
float burs;


cout << "Enter number of modules passed in 07 followed by the number registered in 08 ";
cin >> noModsPassed07;
cin >> noModsReg08;
burs = bursary(noModsPassed07, noModsReg08);


cout << "With " << noModsPassed07 << " modules passed in 2007 and " << noModsReg08 << " modules registered in 2008 , you get a bursary of R" << burs << endl;
return 0;

In line 5 of the above program the code is

if (noModsPassed07 = 0)

Which is the assignment operator. It doesnt compare but assigns 0 to noModsPassed07

It should actually be

if (noModsPassed07 == 0)

Note Assignment ('=') is not equal to Comparision (==)

You could write a condition statement also so that the variable is on the right side of the expression, i.e. if (0 == noModsPassed07) This way your compiler will spot an obvious error, i.e. if (0 = noModsPassed07) will not compile.

commented: Nice Way!! +1

You could write a condition statement also so that the variable is on the right side of the expression, i.e. if (0 == noModsPassed07) This way your compiler will spot an obvious error, i.e. if (0 = noModsPassed07) will not compile.

It is quite Informal. But Its a good Way :)

Thanks so much thats perfect , I cant believe I missed that , now just to add in a loop and im done thanks very much!

> You could write a condition statement also so that the variable is on the right side
Enabling enhanced compiler warnings would tell you the same no matter whether you had a constant there or not.
Plus, you'll get to find out a lot of other problems as well.

Swapping the operands is an 80's syntactic trick, but it basically destroys readability.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.