I got a problem with this simple program

Hello
I am studying C++ from a book ,and now i am the end of the chapter where are the exercises .This one requires me to make a simple calculator that accepts just single-digit numbers written as either digits or spelled out.Please help me to fix my code , or give ideas how to do it.Thank you very much.
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
/*Chapter 4 ,ex 5 - Making a spimple calculator doing the five basic math operations - add,substract
multiply,devide and modulus on two input values-values that must be single-digit numbers written as either digits
or spelled out. */
#include <iostream>
#include <string>
#include <vector>
using namespace std;		
	

int main()
{
	string value1 ="";
	string value2 ="";
	string sign="";
	int i;
	int number1=0;
	int number2=0;
	cout << "PLease enter the 2 numbers followed by the sign of the operation!" <<endl;
	cout <<"WARNING! If you enter any double numbers and require the modulus operation - "<<endl;
	cout <<"those numbers will be,converted into integer numbers!" <<endl;
	cout <<"For leaving the program please enter the control+Z character." <<endl;
	
	
	while (cin >>value1 >> value2 >> sign)
	{
		
		vector<string>numbers(10);
		numbers[0] ="zero";
		numbers[1] ="one";
		numbers[2] ="two";
		numbers[3] ="three";
		numbers[4] ="four";
		numbers[5] ="five";
		numbers[6] ="six";
		numbers[7] ="seven";
		numbers[8] ="eight";
		numbers[9] ="nine";
		for (i =0; i<numbers.size()-1;++i){
			if(value1==numbers[i])
				number1 = i;
			if(value2==numbers[i])
				number2= i;
                         if(value1 = i)
				number1 =i;
			if(value2 =i)
				number2 =i;			
		}

		switch(sign){
			case '+':
				cout <<"The sum of : " << number1 <<" and " << number2 <<" is:" << number1+number2 <<endl;
			break;
			case '-':
				cout <<"The subsdtraction of: " << number1 <<" and " << number2 << " is: " << number1-number2<<endl;
			break;
			case '*':
				cout <<"The multiplication of: " << number1 <<" and " << number2 << " is: " << number1*number2<<endl;
			break;
			case '/':
				if (number2 == 0){
				cout <<"Cannot Devide by zero!"<<endl;
				}			
				else
				cout<<"The devision of: " << number1 <<" and " << number2 << " is: " << number1/number2 <<endl;
				break;
			case '%':
				cout <<"The remainder of: " << number1 <<" and " << number2 << " is: " << number1%number2  <<endl;
				break;
			default:
				cout << "This operation is still unavailable.PLease use the +,-,*,/ or % operations." <<endl;
		}
	}
}
Last edited on
The first thing I would recommend is to decompose the program into functions. There are some basic operations here that stand out:

1. User input. Get the numbers and operator from the user.
2. Parsing the input. Converting numeric values or strings into integers.
3. Performing the calculator operations.

This will allow you to focus on one piece at a time and identify for us what is not working.

One thing that would be helpful for us to know is what sort of topics were covered in this chapter. Have you been introduced to std::map yet?
This chapter is something like a summary for the last 3 chapters.It includes: expressions,constant expressions,operators,conversions,statements(if,while,for,switch),functions and vectors.
I guess your right- i should break it into functions and see where is the problem.I think i know where is the problem - i guess i don`t approach the problem correctly.Ok , i will try to change it tomorrow afternoon and will see what will happen then.

Thank you for the reply and advice.
As Pan said you should try cleaning it all up by moving some things out in separate functions.


But anyway I did some quick changes to your code to make it work,

http://cube.toadass.com/onlinecpp/?file=calctest.cpp


also, on line 43-45

if(value1 = i)
this will try to put the value of i into value1, and not compare the two.
Ok my main problem is that i don`t know whether the user will input a string value or a digit.So i have to use string for getting that value.But how am i supposed to convert string into integer???
I finally made it work but it needs some clean out.Could you advice me how to make it cleaner and simpler.It will be nice to learn some tweaks.
Here is the new code:
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
92
93
/*Chapter 4 ,ex 5 - Making a spimple calculator doing the five basic math operations - add,substract
multiply,devide and modulus on two input values-values that must be single-digit numbers written as either digits
or spelled out. */
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int convertion(string alpha){
		int i=0;
		int number1=0;
		vector<string>digits(10);
		vector<string>numbers(10);
		numbers[0] ="zero";
		numbers[1] ="one";
		numbers[2] ="two";
		numbers[3] ="three";
		numbers[4] ="four";
		numbers[5] ="five";
		numbers[6] ="six";
		numbers[7] ="seven";
		numbers[8] ="eight";
		numbers[9] ="nine";
		digits[0] ="0";
		digits[1] ="1";
		digits[2] ="2";
		digits[3] ="3";
		digits[4] ="4";
		digits[5] ="5";
		digits[6] ="6";
		digits[7] ="7";
		digits[8] ="8";
		digits[9] ="9";
		
		for (i =0; i < 10;++i){
			if(alpha==numbers[i])
				number1 = i;
			
			if(alpha ==digits[i])
				number1 = i;
			
		}
		return number1;
		
}
void switching(char sign,int number1,int number2)
{
	switch(sign){
			case '+':
				cout <<"The sum of : " << number1 <<" and " << number2 <<" is:" << number1+number2 <<endl;
			break;
			case '-':
				cout <<"The subsdtraction of: " << number1 <<" and " << number2 << " is: " << number1-number2<<endl;
			break;
			case '*':
				cout <<"The multiplication of: " << number1 <<" and " << number2 << " is: " << number1*number2<<endl;
			break;
			case '/':
				if (number2 == 0){
				cout <<"Cannot Devide by zero!"<<endl;
				}			
				else
					cout<<"The devision of: " << number1 <<" and " << number2 << " is: " << number1/number2 <<endl;
				break;
			case '%':
				cout <<"The remainder of: " << number1 <<" and " << number2 << " is: " << number1%number2  <<endl;
				break;
			default:
				cout << "This operation is still unavailable.PLease use the +,-,*,/ or % operations." <<endl;
		}
}

int main()
{
	string value1 ="";
	string value2 ="";
	char newsign ='a';
	int number1=0;
	int number2=0;
	cout << "PLease enter the 2 numbers followed by the sign of the operation!" <<endl;
	cout <<"For leaving the program please enter the control+Z character." <<endl;
	
	
	while (cin >>value1 >> value2 >> newsign)
	{
		
	number1 = convertion(value1);
	number2 = convertion(value2);
	
		switching(newsign,number1,number2);
		
	}
}
Last edited on
The reason I asked about std::map was precisely for this: you have multiple strings that can map to the numeric values. A map is an associative container, allowing you to have any key represent any value. With a map you would do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
map<string, int> numbers;
numbers["one"] = 1;
numbers["1"] = 1;
numbers["two"] = 2;
numbers["2"] = 2;
...
string input = getUserInput();

// Check if it is a valid number first.
if (numbers.count(input) == 0)
{
    // No such number.
    ...
}

int value = numbers[input];

Last edited on
I checked about maps in the textbook - maps are covered in a later chapter so I don`t want to hurry up skipping the chapters before it.So i have the choice to use :expressions,constant expressions,operators,conversions,statements(if,while,for,switch),functions and vectors.
I guess its enough for making the program.But if you have any idea to make the program more simple, or more efficient- go on.
Actually this function is unknown to me (yet).
string input = getUserInput();
Thanks to the reply ,I am really grateful - I guess with asking people do more stuff.
Last edited on
Topic archived. No new replies allowed.