c++ Calculator program using pointers and anonymous variables

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 8751 Views - Last Post: 12 September 2011 - 10:07 PM Rate Topic: -----

#1 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

c++ Calculator program using pointers and anonymous variables

Posted 08 September 2011 - 09:23 AM

Hi Everyone

I'm having trouble building a C++ Calculator program I wrote in Visual Studio 2010. VS isn't giving me any real errors just that the build was unsuccessful. So my question is: Can someone help me find what I am doing wrong or help me improve my code? I'll post my source code along with the output I am getting.

/*
		Programmer:	Iqbal
		Date:		09/06/2011
		Assignment:	Project 1; Calculator

		Write a simple calculator program that repeatedly allows the user to select from a menu of operations
		(comprising of the following operations: +,-,*,/ , % ) on real values (that the user enters) and then
		computes the value that results in applying the selected operation to those operands. However, the
		only named variables you may use are pointer variables; all others must be anonymous variables. 

		Pay close attention to the following aspects:

		error checking 
		loop function 
		pointer variables 
		code efficiency(smart code), readability, documentation.
*/

#include <iostream>
using namespace std;

int SubNum (int *a, int *B)/>;
int SumNum (int *x, int *y);
int MultNum(int *i, int *l);
int DivNum(int *j, int *k);
int ModNum(int *r, int *s);

int main ()
{
	int * num1 = NULL, * num2 = NULL, * total = NULL;
	char * oprd = NULL, * cnt = NULL;
	cout << "\nWelcome to Calculator!\t\t\n" << endl;
	
	do {
		cout << "Enter the operand (+, -, *, /, %) for the operation you wish to perform:" << endl;
		cin >> oprd;
		cout << "Enter two numbers: (separate with space)" << endl;
		cin >> num1; 
		cin	>> num2;

		if (*oprd == '+') 
			cout << SumNum(num1, num2) << endl;
		else if (*oprd == '-')
			cout << SubNum(num1, num2) << endl;
		else if (*oprd == '*')
			cout << MultNum(num1, num2) << endl;
		else if (*oprd == '/' && *num2 != 0)
			cout << (float)DivNum(num1, num2) << endl;
		else if (*oprd == '%')
			cout << ModNum(num1, num2) << endl;
		else
			cout << "Wrong operation, you may not divide by zero" << endl;

		cout << "Do you want to continue?" << endl;
		cin >> cnt;
	} while (*cnt == 'y');
	
	cout << "Exiting..." << endl;
}

//addition function
int SumNum (int *x, int *y)
{
	int total;
	total = *x + *y;
	return total;
}

//subtraction function
int SubNum (int * a, int * B)/>
{
	int total;
	total = *a - *b;
	return total;
}

//numtiplication function
int MultNum(int *i, int *l)
{
	int total;
	total = *i * *l;
	return total;
}

//division function
int DivNum(int * j, int * k)
{
	int total;
	total = *j / *k;
	return total;
}

//modulo function
int ModNum(int *r, int *s)
{
	int total;
	total = *r % *s;
	return total;
}



And here is the message I receive in the output window
Spoiler


Any help or suggestions would be greatly appreciated, thanks in advance.

This post has been edited by ishkabible: 08 September 2011 - 01:10 PM
Reason for edit:: added spoiler tag for list of errors, the list was way too long


Is This A Good Question/Topic? 0
  • +

Replies To: c++ Calculator program using pointers and anonymous variables

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: c++ Calculator program using pointers and anonymous variables

Posted 08 September 2011 - 09:44 AM

1>c:\users\administrator\documents\visual studio 2010\projects\calculator\calculator\calculator.cpp(38): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)


That looks like an error to me.

int *num1 = NULL;
...
cin >> num1; 


Well not only are you attempting to read directly into a memory address, but it's an invalid (NULL) one. Why are you using pointers here at all?

EDIT: Oh, it's a requirement I missed:

Quote

only named variables you may use are pointer variables; all others must be anonymous variables.


If your book and or notes are insufficient on pointers, try looking in our tutorials.
Was This Post Helpful? 1
  • +
  • -

#3 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: c++ Calculator program using pointers and anonymous variables

Posted 08 September 2011 - 10:00 AM

	int * num1 = NULL, * num2 = NULL, * total = NULL;
	char * oprd = NULL, * cnt = NULL;
	cout << "\nWelcome to Calculator!\t\t\n" << endl;
	
	do {
		cout << "Enter the operand (+, -, *, /, %) for the operation you wish to perform:" << endl;
		cin >> oprd;
		cout << "Enter two numbers: (separate with space)" << endl;
		cin >> num1; 
		cin >> num2;



In the declaration, you use pointers for num1, num2, ..., everything really. But you do not allocate space for them, so they don't really point to anything.

The cin commands then try to read a pointer, as in an address, which is not really something that is supported.

What you want is either

int num1 = 0:
cin >> num1;


You can then pass it to the functions (which take pointers) like this.

SumNum(&num1, &num2);



Or rewrite your functions so they get their parameters by value or by const reference, which makes a lot more sense really.

Or, you want to keep the pointers:

int* num1 = new int(0);
cin >> *num1;

...

delete num1;



But you then need to allocate integers with new and delete them again when you no longer need them.

I would definitely recommend the first option. As a rule of thumb, use pointers only when you are certain you need them.
Was This Post Helpful? 1
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: c++ Calculator program using pointers and anonymous variables

Posted 08 September 2011 - 10:09 AM

Quote

However, the only named variables you may use are pointer variables; all others must be anonymous variables.


You have used pointer variables but where are the anonymous variables?
Was This Post Helpful? 1
  • +
  • -

#5 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: c++ Calculator program using pointers and anonymous variables

Posted 08 September 2011 - 01:07 PM

Hi all, thanks for all the suggestions. I made several changes to my code and I think I have it however I am unsure. The program compiles and runs but I am not sure I used the anonymous variable correctly. Perhaps someone here may inform me?

/*
		Programmer:	Iqbal
		Date:		09/06/2011
		Assignment:	Project 1; Calculator

		Write a simple calculator program that repeatedly allows the user to select from a menu of operations
		(comprising of the following operations: +,-,*,/ , % ) on real values (that the user enters) and then
		computes the value that results in applying the selected operation to those operands. However, the
		only named variables you may use are pointer variables; all others must be anonymous variables. 

		Pay close attention to the following aspects:

		error checking 
		loop function 
		pointer variables 
		code efficiency(smart code), readability, documentation.
*/

#include <iostream>
using namespace std;

int SumNum (int &a, int &B)/>; //addition function prototype
int SubNum (int &a, int &B)/>; //subtraction function prototype
int MultNum(int &a, int &B)/>; //multiplication function prototype
int DivNum(int &a, int &B)/>; //division function prototype
int ModNum(int &a, int &B)/>; //modulo function prototype

//begin execution of function main
int main ()
{
	int *num1 = new int; //anonymous variable first number user enters
	int *num2 = new int; //anonymous variable second number user enters
	char *oprd = new char; //anonymous variable operand user enter
	char *cnt = new char; //anonymous variable ask user if continue program
	
	cout << "\nWelcome to Calculator!\t\t\n" << endl;
	
	do {
		cout << "Enter the operand (+, -, *, /, %) for the" << endl;
		cout <<	"operation you wish to perform:" << endl;
		cin >> *oprd;
		cout << "Enter two integers (separate with space):" << endl;
		cin >> *num1; //enter first number
		cin	>> *num2; //enter second number

		if (*oprd == '+') //addition condition
			cout << SumNum(*num1, *num2) << endl;
		else if (*oprd == '-') //subtraction condition
			cout << SubNum(*num1, *num2) << endl;
		else if (*oprd == '*') //multiplication condition
			cout << MultNum(*num1, *num2) << endl;
		else if (*oprd == '/' && *num2 != 0) //division condition
			cout << (float)DivNum(*num1, *num2) << endl;
		else if (*oprd == '%') //modulo condition
			cout << ModNum(*num1, *num2) << endl;
		else //illustrate to user error occurred
			cout << "Error...either you divided by a zero or you entered an invalid operand" << endl;

		cout << "Do you want to continue? ('y' for yes | 'n' for no):" << endl; //prompt user to continue
		cin >> *cnt; //pointer to anonymous variable continue
	} while (*cnt == 'y'); //condition to continue program
	
	cout << "Exiting..." << endl; //illustrate program exit
	return 0; //successful program termination
} //end function main

//addition function
int SumNum (int &a, int &B)/>
{
	return (a + B)/>;
}
//subtraction function
int SubNum (int &a, int &B)/>
{
	return (a - B)/>;
}

//numtiplication function
int MultNum(int &a, int &B)/>
{
	return (a * B)/>;
}

//division function
int DivNum(int &a, int &B)/>
{
	return (a / B)/>;
}

//modulo function
int ModNum(int &a, int &B)/>
{
	return (a % B)/>;
}


Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: c++ Calculator program using pointers and anonymous variables

Posted 08 September 2011 - 02:15 PM

You're still using named variables in your functions, rather than anonymous variables.

Note: I was unsure of what anonymous variables were, so I Googled "anonymous variable"; that was the first hit.
Was This Post Helpful? 1
  • +
  • -

#7 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: c++ Calculator program using pointers and anonymous variables

Posted 08 September 2011 - 03:32 PM

View PostJackOfAllTrades, on 08 September 2011 - 01:15 PM, said:

You're still using named variables in your functions, rather than anonymous variables.

Note: I was unsure of what anonymous variables were, so I Googled "anonymous variable"; that was the first hit.


I read this very same article, I believe I am doing just that? my functions are only returning values just as the article shows. Am I missing something? please let me know, i'm new to C++
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: c++ Calculator program using pointers and anonymous variables

Posted 08 September 2011 - 05:51 PM

Oh crap...I must've been looking at the original code. My bad. What you have there looks like you're using "anonymous variables" -- what I would refer to as a temporary, but perhaps that's actually wrong to a language lawyer -- correctly.
Was This Post Helpful? 0
  • +
  • -

#9 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: c++ Calculator program using pointers and anonymous variables

Posted 09 September 2011 - 01:31 PM

no problem. I totally understand, thats happened to me a bunch of times. Thanks for all the help bro, I really appreciate it. you guys are amazing :)
Was This Post Helpful? 0
  • +
  • -

#10 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: c++ Calculator program using pointers and anonymous variables

Posted 09 September 2011 - 01:54 PM

Glad that you now understand what temporaries are in C++, I would like to add this little note to the topic in case you like to know more about it. Before the C++11 standard, you are not allowed to modify temporaries (AKA anonymous variables); But in the new standard, this is added as a new feature, called Rvalue References.
Was This Post Helpful? 1
  • +
  • -

#11 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: c++ Calculator program using pointers and anonymous variables

Posted 09 September 2011 - 04:39 PM

View PostAnarion, on 09 September 2011 - 12:54 PM, said:

Glad that you now understand what temporaries are in C++, I would like to add this little note to the topic in case you like to know more about it. Before the C++11 standard, you are not allowed to modify temporaries (AKA anonymous variables); But in the new standard, this is added as a new feature, called Rvalue References.


Yes I always like to learn more when I can. Excellent wiki reference, kheili mamnoon dadash
Was This Post Helpful? 0
  • +
  • -

#12 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: c++ Calculator program using pointers and anonymous variables

Posted 09 September 2011 - 09:52 PM

As I was testing and debugging my code, I noticed I am not able to get an exact answer for function DivNum. Like for example if I enter '/' (divide) for the operand and 12 and 5 for the integers the value I receive is 2 and not 2.4 I already changed my data type to float, still no luck. Any suggestions guys?

Here's my output:

Quote

Enter the operand (+, -, *, /, %) for the
operation you wish to perform:
/
Enter two integers (separate with space):
12 5
2


And here's my code updated code:

/*
		Programmer:	Iqbal
		Date:		09/06/2011
		Assignment:	Project 1; Calculator

		Write a simple calculator program that repeatedly allows the user to select 
		from a menu of operations (comprising of the following operations: +,-,*,/ , % )
		on real values (that the user enters) and then computes the value that results
		in applying the selected operation to those operands. However, the only named
		variables you may use are pointer variables; all others must be anonymous
		variables. 

		Pay close attention to the following aspects:

		error checking 
		loop function 
		pointer variables 
		code efficiency(smart code), readability, documentation.
*/

#include <iostream>
using namespace std;

int SumNum (int &a, int &B)/>; //addition function prototype
int SubNum (int &a, int &B)/>; //subtraction function prototype
int MultNum(int &a, int &B)/>; //multiplication function prototype
float DivNum(int &a, int &B)/>; //division function prototype
int ModNum(int &a, int &B)/>; //modulo function prototype

//begin execution of function main
int main ()
{
	int *num1 = new int; //anonymous variable first number user enters
	int *num2 = new int; //anonymous variable second number user enters
	char *oprd = new char; //anonymous variable operand user enter
	char *cnt = new char; //anonymous variable ask user if continue program
	
	cout << "\nWelcome to Calculator!\t\t\n" << endl;
	
	do {
		cout << "Enter the operand (+, -, *, /, %) for the" << endl;
		cout <<	"operation you wish to perform:" << endl;
		cin >> *oprd;
		cout << "Enter two integers (separate with space):" << endl;
		cin >> *num1; //input first number
		cin	>> *num2; //input second number

		if (*oprd == '+') //addition condition
			cout << SumNum(*num1, *num2) << endl;

		else if (*oprd == '-') //subtraction condition
			cout << SubNum(*num1, *num2) << endl;

		else if (*oprd == '*') //multiplication condition
			cout << MultNum(*num1, *num2) << endl;

		else if (*oprd == '/' && *num2 != 0) //division condition
			cout << DivNum(*num1, *num2) << endl;

		else if (*oprd == '%') //modulo condition
			cout << ModNum(*num1, *num2) << endl;

		else //illustrate to user error occurred
			cout << "Error...either you divided by a zero or you entered an invalid operand" << endl;

		cout << "Do you want to continue? (enter 'y' to continue or 'n' to exit):" << endl; //prompt user to continue
		cin >> *cnt; //pointer to anonymous variable continue
	} while (*cnt == 'y'); //condition to continue program
	
	cout << "Exiting..." << endl; //illustrate program exit
	return 0; //successful program termination
} //end function main

//addition function
int SumNum (int &a, int &B)/>
{
	return (a + B)/>;
}
//subtraction function
int SubNum (int &a, int &B)/>
{
	return (a - B)/>;
}

//numtiplication function
int MultNum(int &a, int &B)/>
{
	return (a * B)/>;
}

//division function
float DivNum(int &a, int &B)/>
{
	return (a / B)/>;
}

//modulo function
int ModNum(int &a, int &B)/>
{
	return (a % B)/>;
}


Was This Post Helpful? 0
  • +
  • -

#13 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: c++ Calculator program using pointers and anonymous variables

Posted 09 September 2011 - 10:21 PM

//division function
float DivNum(int &a, int &B)/>
{
    return (a / B)/>;
}

You return a float, but a and b are still ints. When you divide two ints, you get an int as result. Cast one of them to a float before the division:
return (float) a / b;
return a / (float) b;

Was This Post Helpful? 1
  • +
  • -

#14 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: c++ Calculator program using pointers and anonymous variables

Posted 09 September 2011 - 10:26 PM

Oh wow, I understand. thanks hezekiah! i've learned something important.
Was This Post Helpful? 0
  • +
  • -

#15 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: c++ Calculator program using pointers and anonymous variables

Posted 12 September 2011 - 09:56 PM

hi guys (once again)
I noticed a fundamental problem with my code. The program stipulates I must prompt the user to enter real values (x.xx i.e. 2.34), I researched it and data type "int" is not a real value. So I must use data type "double", however function modulo may not use data type "double." Any suggestions on how to correct my code? i'm really confused. If this question doesn't make sense please let me know and I will revise it or explain it better.

Here's my updated code:

/*
		Programmer:	Iqbal
		Date:		09/06/2011
		Assignment:	Project 1; Calculator

		Write a simple calculator program that repeatedly allows the user to select 
		from a menu of operations (comprising of the following operations: +,-,*,/ , % )
		on real values (that the user enters) and then computes the value that results
		in applying the selected operation to those operands. However, the only named
		variables you may use are pointer variables; all others must be anonymous
		variables. 

		Pay close attention to the following aspects:

		error checking 
		loop function 
		pointer variables 
		code efficiency(smart code), readability, documentation.
 
 
        Stipulations:
 
        Your assignment should comprise of the following: 
        (a) a thoroughly commented source code
        (B)/> runtime output of your code (several runs with various inputs as required by the assignment)
        (c) and a section named “DISCUSSION” which should comprise of your comments on what your program
        is doing/not doing (corner cases that you have not tested OR specific inputs on which it will
        not work, etc.)
 
*/

#include <iostream>
using namespace std;

double SumNum (double &nA, double &nB); //addition function prototype
double SubNum (double &nA, double &nB); //subtraction function prototype
double MultNum (double &nA, double &nB); //multiplication function prototype
double DivNum (double &nA, double &nB); //division function prototype
int ModNum (double &nA, double &nB); //modulo function prototype

//begin execution of function main
int main ()
{
	double *num1 = new double; //first number user enters
	double *num2 = new double; //second number user enters
    char *oprd = new char; //operand user enter
	char *cnt = new char; //ask user if continue program
	
	cout << "\nWelcome to Calculator!\t\t\n" << endl;
	
	do {
		cout << "Enter the operand (+, -, *, /, %) for the" << endl;
		cout <<	"operation you wish to perform:" << endl; //prompt user for desired operation
		cin >> *oprd; //input operand
        
		cout << "Enter two real numbers (separate with space):" << endl;
		cin >> *num1; //input first number
		cin	>> *num2; //input second number

		if (*oprd == '+') //addition condition
			cout << SumNum(*num1, *num2) << endl;

		else if (*oprd == '-') //subtraction condition
			cout << SubNum(*num1, *num2) << endl;

		else if (*oprd == '*') //multiplication condition
			cout << MultNum(*num1, *num2) << endl;

		else if (*oprd == '/' && *num2 != 0) //division condition
			cout << DivNum(*num1, *num2) << endl;

		else if (*oprd == '%') //modulo condition
			cout << ModNum(*num1, *num2) << endl;

		else //illustrate to user error occurred
			cout << "Error...either you divided by a zero or you entered an invalid operand" << endl;
        
        //prompt user to continue
		cout << "Do you want to continue? (enter 'y' to continue or 'n' to exit):" << endl;
        
		cin >> *cnt; //pointer to anonymous variable continue
	} while (*cnt == 'y'); //condition to continue program
	
	cout << "Exiting..." << endl; //illustrate program exit
	
    return 0; //successful program termination
} //end function main



//addition function
double SumNum (double &nA, double &nB)
{
	return (nA + nB);
}

//subtraction function
double SubNum (double &nA, double &nB)
{
	return (nA - nB);
}

//numtiplication function
double MultNum(double &nA, double &nB)
{
	return (nA * nB);
}

//division function
double DivNum(double &nA, double &nB)
{
	return (nA / nB);
}

//modulo function
int ModNum(double &nA, double &nB)
{
	return (int)nA % nB;
}




This is the new error I'm getting:

Quote

originalcalc.cpp: In function ‘int ModNum(double&, double&)’:
originalcalc.cpp:118: error: invalid operands of types ‘int’ and ‘double’ to binary ‘operator%’



Any help or suggestions would be very much appreciated, thanks.

This post has been edited by iqbalmmz: 12 September 2011 - 09:58 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2