Hi everyone! I have some problems with value assignment in my following code, which is to create a very simple calculator which could run computations like "seven 5 +", which should result in 7 + 5 = 12. It seems that my int variables d1 and d2 do not accept values in switch cases. The problematic statements are indicated by comments in bold and italic next to the statement. Here is the code, and I appreciate your help! Thank you in advance!
void Result_Function(char x, int a, int b);
int t_to_d(string y);
int main()
{
int d1;
int d2;
string t1;
string t2;
char op;
cout << "Please specify the type of inputs." << endl;
cout << "(1) digit digit operation -- type 1 \n";
cout << "(2) digit text operation -- type 2 \n";
cout << "(3) text digit operation -- type 3 \n";
cout << "(4) text text operation -- type 4 \n";
int type;
cin >> type;
while (type != 1 && type != 2 && type != 3 && type != 4)
{
cout << "Sorry! Please only choose to type the numbers from 1 to 4 to specify the kind of inputs you want. \n";
cout << "Please specify the type of inputs." << endl;
cout << "(1) digit digit operation -- type 1 \n";
cout << "(2) digit text operation -- type 2 \n";
cout << "(3) text digit operation -- type 3 \n";
cout << "(4) text text operation -- type 4 \n";
cin >> type;
}
switch(type)
{
case 1:
cout << "You chose digit digit operation. \n";
cout << "Now please enter the digits you want and the operation. \n";
cin >> d1 >> d2 >> op;
break;
case 2:
cout << "You chose the digit text operation. \n";
cout << "Now please enter the digit, text, operation. \n";
cin >> d1 >> t2 >> op;
d2 = t_to_d(t2); // "d2 DOES NOT ACCEPT THE VALUE GIVEN BY THE FUNCTION"
break;
case 3:
cout << "You chose the text digit operation. \n";
cout << "Now please enter the text, digit, and the operation. \n";
cin >> t1 >> d2 >> op;
d1 = t_to_d(t1); // "d1 DOES NOT ACCEPT THE VALUE GIVEN BY THE FUNCTION"
break;
case 4:
cout << "You chose the text text opeation. \n";
cout << "Now please enter the texts and operation. \n";
cin >> t1 >> t2 >> op;
d1 = t_to_d(t1);
d2 = t_to_d(t2);
break;
}
Result_Function(op,d1,d2);
}
void Result_Function(char x, int a, int b)
{
switch(x)
{
case '+':
cout << "The sum of " << a << " and " << b << " is: " << a + b << endl;
break;
case '-':
cout << "The subtraction of " << a << " and " << b << " is: " << a - b << endl;
break;
case '*':
cout << "The product of " << a << " and " << b << " is: " << a*b << endl;
break;
case '/':
double q = a/b;
cout << "The quotient of " << a << " and " << b << " is: " << setprecision(20) << q << endl;
break;
}
}
for( int i = 0; i < 10; i++)
{
if(v[i] == y)
{
return i;
break;
}
}
}
has undefined behavior in case when y is not equal to any element of the vector. In this case it returns nothing.
You should check what values of y is passed to the function. So output it before calling the function to be sure that you are passing an correct vvalue.
Thank you so much for your reply, Vlad!
But still, even if I passed the correct value of string y, the corresponding digit is still not correct. I don't know why is that.
If you could, please try user input "2" to select "digit text operation", and then type "3 five +"; The result on my compiler is "The sum of 3 and 4138112 is 4138115" rather than the sum of 3 and 5 is 8, which means d2 did not store 5. I appreciate it if you could help me further investigate this problem.
I checked the function and it returned a correct value. Insert code for outputing the argument you pass tp function Also insert code for outputing the return value of the function to be sure that the function is correct.
After including the libraries, and changing the int to double, I get the correct answers. Not a bad program. Now, you have to let it include numbers larger than 9.
// Calculator.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
usingnamespace std;
void Result_Function(char x, double a, double b);
int t_to_d(string y);
int main()
{
int d1;
int d2;
string t1;
string t2;
char op;
vector<string>v(10);
v[0] = "zero";
v[1] = "one";
v[2] = "two";
v[3] = "three";
v[4] = "four";
v[5] = "five";
v[6] = "six";
v[7] = "seven";
v[8] = "eight";
v[9] = "nine";
cout << "Please specify the type of inputs." << endl;
cout << "(1) digit digit operation -- type 1 \n";
cout << "(2) digit text operation -- type 2 \n";
cout << "(3) text digit operation -- type 3 \n";
cout << "(4) text text operation -- type 4 \n";
int type;
cin >> type;
while (type != 1 && type != 2 && type != 3 && type != 4)
{
cout << "Sorry! Please only choose to type the numbers from 1 to 4 to specify the kind of inputs you want. \n";
cout << "Please specify the type of inputs." << endl;
cout << "(1) digit digit operation -- type 1 \n";
cout << "(2) digit text operation -- type 2 \n";
cout << "(3) text digit operation -- type 3 \n";
cout << "(4) text text operation -- type 4 \n";
cin >> type;
}
switch(type)
{
case 1:
cout << "You chose digit digit operation. \n";
cout << "Now please enter the digits you want and the operation. \n";
cin >> d1 >> d2 >> op;
break;
case 2:
cout << "You chose the digit text operation. \n";
cout << "Now please enter the digit, text, operation. \n";
cin >> d1 >> t2 >> op;
d2 = t_to_d(t2); // "d2 DOES NOT ACCEPT THE VALUE GIVEN BY THE FUNCTION"
break;
case 3:
cout << "You chose the text digit operation. \n";
cout << "Now please enter the text, digit, and the operation. \n";
cin >> t1 >> d2 >> op;
d1 = t_to_d(t1); // "d1 DOES NOT ACCEPT THE VALUE GIVEN BY THE FUNCTION"
break;
case 4:
cout << "You chose the text text opeation. \n";
cout << "Now please enter the texts and operation. \n";
cin >> t1 >> t2 >> op;
d1 = t_to_d(t1);
d2 = t_to_d(t2);
break;
}
Result_Function(op,d1,d2);
}
void Result_Function(char x, double a, double b)
{
switch(x)
{
case'+':
cout << "The sum of " << a << " plus " << b << " is: " << a + b << endl;
break;
case'-':
cout << "The subtraction of " << a << " minus " << b << " is: " << a - b << endl;
break;
case'*':
cout << "The product of " << a << " times " << b << " is: " << a*b << endl;
break;
case'/':
double q = a/b;
cout << "The quotient of " << a << " divided by " << b << " is: " << setprecision(20) << q << endl;
break;
}
}
int t_to_d(string y)
{
vector<string>v(10);
v[0] = "zero";
v[1] = "one";
v[2] = "two";
v[3] = "three";
v[4] = "four";
v[5] = "five";
v[6] = "six";
v[7] = "seven";
v[8] = "eight";
v[9] = "nine";
for( int i = 0; i < 10; i++)
{
if(v[i] == y)
{
return i;
break;
}
}
}
Hi Vlad, thank you for your tip. I tried to print my d1 and d2 and found out that the values are not assigned(from the t_to_d function) to them at all. So the problem is still not resolved.
Hello, whitenite1! Thank you so much for your reply! I copied your program 100% but it still does not work. For example, I chose 4 "text text operation", and typed "five three +", which results in "The sum of " 8.39795e+006 plus 8.39795e_006 is 1.67959e+007 ", all incredibly large numbers. Could you investigate further to see which part goes wrong? I appreciate your help!
What program are you using to compile it with? I'm using Microsoft C++ Visual Express 2008. If I know what you're using, I'll try to d/l it and check into it further. Otherwise, it's near impossible to know why you're having these problems, and others aren't. I'll wait for your reply..
Hello Vlad and whitenite1! I am so sorry for my late response because some friends asked me out.
@Vlad, I know what you mean and tried your program (adding additional lines to show the values of d1 and d2 after applying the t_to_d function). It is merely a testing technique, not a solution. Like I said, they are showing incredibly big numbers that make no sense.
@whitenite1, I am using the Code::Blocks C++ compiler. I'd really appreciate it if you could try it out using Code::Blocks compiler as well. Thank you so much and I apologize for letting you wait so long.
Oh, so Code::Blocks is like an IDE? I am pretty new in C++ so sorry about the terminology confusion.
And I am using the GNU GCC Compiler!
Could you try it? Thank you so much!