Code Help

I am very new to C++ programming and any other programming. I have written below code to return the value with sign based on indicator value, If indiccator is Y, programme should written +Amount or else -Amount.

Could you please assist with feedback or provide me the correct code if it is wrong.

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
#include <iostream>
using namespace std;

double AmountSign(double Amount, char Indicator)
{
double Amountsign
If (Indicator='Y')
{
If (Amount='')
{
Amountsign=''
}
else
{
Amountsign=cout <<"+" <<Amount;
}
}
else
{
If (Amount='')
{
Amountsign=''
}
else
{
Amountsign=cout <<"-" <<Amount;
}
}
return(Amountsign);
}



Thanks in advance.
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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>

using namespace std;


double AmountSign(double Amount, char Indicator)
{
   if(tolower(Indicator) == 'y')
      return fabs(Amount);
   return -fabs(Amount);
}

 
int main()
{
   double d;
   char ch;
   cin >> d >> ch;
   cout << showpos << AmountSign(d, ch) << endl;  

   return 0;
}
Last edited on
Thanks Syuf,

Do we require main() function here as we have already written AmountSign function or else can we remove this?
AmountSign returns double value, with sign depending on the second parameter, if you want to output this value like "+Value" or "-Value", you need to use code, written in main (you can put it somewhere else). main only inputs the value and the char, after that outputs it in needed format (with sign in front of it).
Thanks Syuf.

Can we use thsi same code for Float return type and Amount datatype.

And Usage is AmountSign(Value,Indicator), Is this the way to use?
Last edited on
Topic archived. No new replies allowed.