Question:
C++ coding problem!!?
1970-01-01 00:00:00 UTC
C++ coding problem!!?
Five answers:
Prakash
2010-01-22 23:21:01 UTC
Hi,





Problem 1 : scope problem for variable x

solution : pls declare the variable a global variable



Problem 2 : there is no return value is present for each method.

solution : pls include the return value to each of the user defined method

user defined methods are 1.int notadd() 2.int notsubtract() 3.int notmultiply()

4. int add(), 5.int subtract(), 6.int multiply(), 7.int divide(), and

8. int divide();



I have modify and upload your program... pls find the below.... the below code will match your requirement...





#include

#include



int x;

int notadd();

int notsubtract();

int notmultiply();

int add();

int subtract();

int multiply();

int divide();

int divide();



int main()

{

using namespace std;



cout << "press 1 for add, 2 for subtract, 3 for multiply, and 4 for divide" << endl;

cin >> x;

if (x == 1)

{ add(); }

else { notadd(); }

getch();

}

int notadd()

{

if (x == 2)

{ subtract(); }

else

{ notsubtract();}

return 0;



}

int notsubtract()

{

if (x == 3)

{ multiply(); }

else

{ notmultiply(); }

return 0;

}



int notmultiply()

{

if (x == 4)

{ divide(); }

return 0;

}

int add()

{

using namespace std;

int a = 0; //makes x an vaiable

cout << "enter first number" << endl; // diaplays "enter first number" on screen

cin >> a; // saves entered number as x

int b = 0; //makes y an vaiable

cout << "enter second number" << endl; // diaplays "enter second number" on screen

cin >> b; // saves entered number as y

int c = a + b; // makes c = a + b

cout << a << " plus " << b << " equals " << c << endl; // diaplays x plus y = z

return 0;

}

int subtract()

{

using namespace std;

int d = 0; //makes d an vaiable

cout << "enter first number" << endl; // diaplays "enter first number" on screen

cin >> d; // saves entered number as d

int e = 0; //makes e an vaiable

cout << "enter second number" << endl; // diaplays "enter second number" on screen

cin >> e; // saves entered number as e

int f = d - e; // makes f = d - e

cout << d << " minus " << e << " equals " << f << endl; // diaplays x plus y = z

return 0;

}

int multiply()

{

using namespace std;

int g = 0; //makes g an vaiable

cout << "enter first number" << endl; // diaplays "enter first number" on screen

cin >> g; // saves entered number as g

int h = 0; //makes h an vaiable

cout << "enter second number" << endl; // diaplays "enter second number" on screen

cin >> h; // saves entered number as h

int i = g * h; // makes z = x * y

cout << g << " times " << h << " equals " << i << endl; // diaplays x plus y = z

return 0;

}



int divide()

{

using namespace std;

int j = 0;// makes j an vaiable

cout << "enter first number" << endl;// diaplays "enter first number" on screen

cin >> j;// saves entered number as j

int k = 0;//makes k an vaiable

cout << "enter second number" << endl;// diaplays "enter second number" on screen

cin >> k;// saves entered number as k

int l = j / k;// makes l = j / k

cout << j << " divided by " << k << " equals " << l << endl; // diaplays x plus y = z

return 0;

}











I think its is helpful for you...





Thank and Regards,

Prakash.R
cja
2010-01-22 21:39:59 UTC
You're right that the compiler errors are because you're using a variable x, which has not been declared, in your notadd, notsubtract, and notmultiply functions. I'll give you credit for a decent effort here, but you're doing much more work than is necessary. Your logic could be much simpler.



See below for an example of what you might want to try. You'll notice that I've added some input validation, where you didn't have any, but what I have is too simplistic and doesn't work that well. When you get further along, you're going to find that using getline and a stringstream object is the way to go. Since this is only your 2nd day of C++, you shouldn't worry much about those subleties right now.



What you should work on is reorganizing your logic, e.g. those not...( ) functions are not needed at all. Try to simplify your code, and try not to duplicate any code, e.g. your arithmetical operation functions should not be prompting for input, do that in one place and pass the operands to the calculation functions.



#include

#include



using namespace std;



int add(int, int);

int subtract(int, int);

int multiply(int, int);

float divide(int, int);



const string ops("+-*/");



int main(int argc, char *argv[]) {

    char op;

    int x, y;



    while (true) {

        cout << endl << "Enter operation (+, -, *, /) : ";

        cin >> op;

        if (ops.find(op) != string::npos) {

            cout << "Enter x : ";

            if (cin >> x) {

                cout << "Enter y : ";

                if (cin >> y) {

                    cout << x << ' ' << op << ' ' << y << " = ";

                    switch (op) {

                    case '+':

                          cout << add(x,y);

                        break;

                    case '-':

                        cout << subtract(x,y);

                        break;

                    case '*':

                        cout << multiply(x,y);

                        break;

                    case '/':

                        cout << divide(x,y);

                        break;

                    }

                    cout << endl;

                } else {

                    cout << "invalid entry" << endl;

                }

            } else {

                cout << "invalid entry" << endl;

            }

        }

       

    }

    return 0;

}



int add(int x, int y) { return x + y; }

int subtract(int x, int y) { return x - y; }

int multiply(int x, int y) { return x * y; }

float divide(int x, int y) { return static_cast(x) / y; }



#if 0



Sample run:



Enter operation (+, -, *, /) : +

Enter x : 3

Enter y : 4

3 + 4 = 7



Enter operation (+, -, *, /) : /

Enter x : 5

Enter y : 2

5 / 2 = 2.5



Enter operation (+, -, *, /) : x



Enter operation (+, -, *, /) : *

Enter x : 5

Enter y : 6

5 * 6 = 30



Enter operation (+, -, *, /) : -

Enter x : 9

Enter y : 2

9 - 2 = 7



#endif
Avaiz G
2010-01-22 21:35:12 UTC
Buddy... delcare your variable x as an instance variable and not as a local variable.



i.e. Your 'x' will die once the closing braces '}' of the main() gets encountered. Put it up with the methods you declared at the beginning
Erfan
2010-01-22 21:12:19 UTC
i think u should us the conio library like this

or without h
Nick T
2010-01-22 13:12:21 UTC
you declare x inside main so it is out of scope in all other functions.

Either declare it as a global, not good practise; pass it to the subsequent functions as a parameter, better but not the best; or restructure your code.



You have common code in each function to read the operands, why? You only need to read them once so only write the code once.



You can also read all 3 variables at once



Why daisy chain your function calls? just call the correct function, dont keep passing it around to all the others. In fact the code is so simple you dont need function calls.



You have used if's, look at using a case statement.

And why not use characters for the operator, much easier to type + for add



PSEUDO CODE

declare operator as character value 0

declare operand1 as floating point value 0

declare operand2 as floating point value 0

decalre result as floating point value 0

output Enter calcuation

input operand1 operator operand2

SWITCH on operator

..case '+' : result = operand1 + operand2;

..case '-' : result = operand1 - operand2;

..case '*' : result = operand1 * operand2;

..case '/' : result = operand1 / operand2;

END SWITCH



output operand1 operator operand2 ' = ' result



If you are determined to use function calls each as taking two parameters and returning a value i.e.



PSEUDO CODE

DEFINE FUNCTION Add( input : left type floating point, input : right type floating point) returns floating point

result = left + right

return result

END FUNCTION



Then when you call it use the following convention

result = Add(operand1, operand2)



EDIT - Well at least you have attempted it which is a good sign. The point is pseudocode is a generic, it is abstracted away from a specific language purposefully to make it portable.



you should move the using namespace std to after the #includes and outside of the definition of main.

main should really always be declared as

int main(int argc, char *argv[])



You don't need to declare optionion of size 1. its size 1 by default.

istream, in this case it means cin and cout, check your chevrons, cout << , cin >>

Switch statements need opening and closing braces.



All of the errors you are getting will have generated an error mesage , depending upon which compiler you are using they may be cryptic or reasonably explainitory. Looking at the code in with the error message should put it in context and hopefully make it easier for you to debug your own errors.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...