Repeat Code, cmath & memory storing

Hi I was wondering if anybody could help me as this is my first program.

Here is what I want to do

- At the end of an answer I want the program to ask the user if they'd like to do another calculation Y/N - if they choose N then the program quits.

- Calculate the square of a number i.e. 32 = 9

- Calculate a number to a power xy such as 43 = 64.

- Store a number to memory and retrieve it like the answer to a question asked

I mainly need help on the first 3 but the 4th would be great to have. Any help appreciated!!
Here is my code atm:
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream> //This includes the necessary Input/Output stream for basic commands for Cin/Cout.


using namespace std; //This is the namespace we are using (default std).

int main() //Our main class(an int)

{
	int fun; // integer we are using to determine the user's choice of function.
	double number1; // a user entered number1.
	double number2; // a user entered number2.

	
	cout << "List of Functions:\n"
         << "(1) Addition +\n(2) Subtraction -\n(3) Multiplication *\n(4) Division /\n\n"
		 << "______________________________________________________\n\n"
         << "Please use the corrosponding number to select function\n"
	     << "______________________________________________________\n\n";
	
	cin >> fun; //Sets user input to the variable Sel.

	if (fun == 1) // Determines if the user picks 1.
	{
	
				  cout<<"\n";
                  cout << "Enter first number: ";
                  cin >> number1;
                  cout << "Enter second number: ";
                  cin >> number2;
				  cout << endl;
                  
                  // adds the two numbers and displays the question and answer for the user
				  cout << number1 << " + " << number2 << " = " << number1 + number2 << endl << endl;
		
	
	}
	if (fun == 2) // Determines if the user picks 2.
	{
		          cout<<"\n";
                  cout << "Enter first number: ";
                  cin >> number1;
                  cout << "Enter second number: ";
                  cin >> number2;
				  cout << endl;
                  
                  // subtracts the two numbers and displays the question and answer for the user
				  cout << number1 << " - " << number2 << " = " << number1 - number2 << endl << endl;
	}
	if (fun == 3) // Determines if the user picks 3.
	{
		          cout<<"\n";
                  cout << "Enter first number: ";
                  cin >> number1;
                  cout << "Enter second number: ";
                  cin >> number2;
				  cout << endl;
                  
                  // multiplies the two numbers and displays the question and answer for the user
				  cout << number1 << " * " << number2 << " = " << number1 * number2 << endl << endl;
	}
	if (fun == 4) // Determines if the user picks 4.
	{
				  cout<<"\n";
                  cout << "Enter first number: ";
                  cin >> number1;
                  cout << "Enter second number: ";
                  cin >> number2;
				  cout << endl;
                  
                  // divides the two numbers and displays the question and answer for the user
				  cout << number1 << " / " << number2 << " = " << number1 / number2 << endl << endl;
	}

	system ("PAUSE>nul");
	return 0;
}

hi,
Firstly very well formatted code for your first program

I have put a while loop around your Input answer code for the Y/N to work.

also for calculating Powers use the pow(x,y) function and include <maths.h> at top of file

ie
 
    pow(3,2)  is 3 to the power of 2


And to store a value in memory just declare a variable
 
    int   store1 = {value}


here is the code with the while loop, just a couple of variables at the top to make it work.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream> //This includes the necessary Input/Output stream for basic commands for Cin/Cout.
#include <math.h> //for pow(x,y) function

using namespace std; //This is the namespace we are using (default std).

int main() //Our main class(an int)

{
	int fun; // integer we are using to determine the user's choice of function.
	double number1; // a user entered number1.
	double number2; // a user entered number2.

	bool	done = false;
	char	choice;

  while(!done)
  {

	cout << "List of Functions:\n"
         << "(1) Addition +\n(2) Subtraction -\n(3) Multiplication *\n(4) Division /\n\n"
		 << "______________________________________________________\n\n"
         << "Please use the corrosponding number to select function\n"
	     << "______________________________________________________\n\n";
	
	cin >> fun; //Sets user input to the variable Sel.

	if (fun == 1) // Determines if the user picks 1.
	{
	
				  cout<<"\n";
                  cout << "Enter first number: ";
                  cin >> number1;
                  cout << "Enter second number: ";
                  cin >> number2;
				  cout << endl;
                  
                  // adds the two numbers and displays the question and answer for the user
				  cout << number1 << " + " << number2 << " = " << number1 + number2 << endl << endl;
		
	
	}
	if (fun == 2) // Determines if the user picks 2.
	{
		          cout<<"\n";
                  cout << "Enter first number: ";
                  cin >> number1;
                  cout << "Enter second number: ";
                  cin >> number2;
				  cout << endl;
                  
                  // subtracts the two numbers and displays the question and answer for the user
				  cout << number1 << " - " << number2 << " = " << number1 - number2 << endl << endl;
	}
	if (fun == 3) // Determines if the user picks 3.
	{
		          cout<<"\n";
                  cout << "Enter first number: ";
                  cin >> number1;
                  cout << "Enter second number: ";
                  cin >> number2;
				  cout << endl;
                  
                  // multiplies the two numbers and displays the question and answer for the user
				  cout << number1 << " * " << number2 << " = " << number1 * number2 << endl << endl;
	}
	if (fun == 4) // Determines if the user picks 4.
	{
				  cout<<"\n";
                  cout << "Enter first number: ";
                  cin >> number1;
                  cout << "Enter second number: ";
                  cin >> number2;
				  cout << endl;
                  
                  // divides the two numbers and displays the question and answer for the user
				  cout << number1 << " / " << number2 << " = " << number1 / number2 << endl << endl;
	}

	cout << "Would you like to do another calculation Y/N ";	

	cin >> choice;

	if (choice == 'N' || choice == 'n')
	{
		done = true;
	}
  }

	//system ("PAUSE>nul");
	return 0;
}
Thanks very much!

But how does the user store something? and the retrieve it?
Like say if i wanted the user to press 'm' for memory to retrieve it
Last edited on

First you need to store your result to a variable

i would declare the variable before the while loop

 
     int     store;


then when you make your calculation you need to asign the result

eg.
 
     store = number1 / number2;


then to retrieve the value from memory, I would add a sixth option in the menu,
(because the menu inputs an Int) and then something like this

 
    cout << store;


hope this is what you meant
Shredded
thanks a lot!
just one more question sorry. Sent program to my friend and burned it onto disc and tried on different computer and i got an error when trying to open the .exe

THe application has failed to start because its side-by-side configuration is incorect. Please see the application event log or use the command-line sxstrace.exe tool for more detail.

need to fix this asappp
hi,

there seems to be 2 possible problems that will cause this

first if you are compiling with a manifest file try removing this in the linker properties
{i am assuming that you are using VC++ express}

The other way around this is to install the VC redistributables on your friends machine,
making sure they are the same ones that are on your.

hope this helped
Shredded
cheers!

Thanks for all the help!
Aphex
actually, hah sorry you must be getting annoyed.

I was wondering how I make it so that instead of the user pressing 5 to carry out the sqaure function they can press 's'.

All my 'fun' inputs are set as integers, so my program is looking for an integer rather than a letter all the time. At the moment if i enter a letter the program repeats and repeats really quickly so the program flickers.

I've tried looking at the ASCII code table and tried 115 which equals s but it hasnt worked... :s
Last edited on
hi ,

try adding this code around your input

1
2
3
4
5
6
7
8
9
        tempChar = cin.peek();
	cin >> fun; //Sets user input to the variable Sel.

	if (tempChar == 's')
	{
		cin.clear();
		cin.ignore(1);
		fun = 5;  //or do code here
	}


and add the tempChar like this

1
2
3
        bool	done = false;
	char	choice;
	char	tempChar;    //add this line 
Topic archived. No new replies allowed.