EvilZone
Programming and Scripting => C - C++ => : Chef August 12, 2013, 01:46:27 AM
-
#include <iostream>
#include <string>
using namespace std;
int main ()
{
float a;
float b;
float result;
string asd1;
cout << "What mathmatical operation would you like to do?";
cin >> asd1;
if (asd1=="addition", "Addition")
{
cout << "What is the first number in your addition sequence? ";
cin >> a;
cout << "What is the second number in your addition sequence? ";
cin >> b;
result = a+b;
cout << "The answer is: " << result << ".\n";
}
else if (asd1=="division", "Division")
{
cout << "What is the first number in this division equation? ";
cin >> a;
cout << "What is the second number in this division equation? ";
cin >> b;
result = a/b;
cout << "The answer is " << result << ".\n";
}
else if (asd1=="subtraction", "Subtraction")
{
cout << "What is the first number in the subtraction equation?";
cin >> a;
cout << "What is the second number?";
cin >> b;
result=a-b;
cout << "The answer is: " << result << ".\n";
}
else if (asd1 == "multiplication", "Multiplication")
{
cout << "What is the first number in your multiplication equation?";
cin >> a;
cout << "What is the second number?";
cin >> b;
result=a*b;
cout << "The answer is " << result << ".\n";
}
return 0;
}
-
Use strcmpi() for checks on the type of mathematic operation to use. It's not case sensitive.
-
Use strcmpi() for checks on the type of mathematic operation to use. It's not case sensitive.
I don't need help w/ that. My problem is when it asks the user "What mathmatical operation would you like to do?" basically, no matter what I type is says "What is the first number in your addition sequence?".
I can't get it to choose accordingly based on what the user inputs.
-
I fixed the code for you using the same method you're using to check what type of mathematical operation to perform, but doing what xC said is better, as then you won't have to worry about upper/lower case inputs.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
float a;
float b;
float result;
string asd1;
cout << "What mathmatical operation would you like to do?";
cin >> asd1;
if ((asd1=="addition") || (asd1=="Addition"))
{
cout << "What is the first number in your addition sequence? ";
cin >> a;
cout << "What is the second number in your addition sequence? ";
cin >> b;
result = a+b;
cout << "The answer is: " << result << ".\n";
}
else if ((asd1=="division") || (asd1=="Division"))
{
cout << "What is the first number in this division equation? ";
cin >> a;
cout << "What is the second number in this division equation? ";
cin >> b;
result = a/b;
cout << "The answer is " << result << ".\n";
}
else if ((asd1=="subtraction") || (asd1=="Subtraction"))
{
cout << "What is the first number in the subtraction equation?";
cin >> a;
cout << "What is the second number?";
cin >> b;
result=a-b;
cout << "The answer is: " << result << ".\n";
}
else if ((asd1 == "multiplication") || (asd1=="Multiplication"))
{
cout << "What is the first number in your multiplication equation?";
cin >> a;
cout << "What is the second number?";
cin >> b;
result=a*b;
cout << "The answer is " << result << ".\n";
}
return 0;
}
-
I fixed it as well, well here is how I would have done it anyhow.
Edit: Fixed poor syntax.
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
float a, b, result;
string asd1;
cout << "What mathmatical operation would you like to do?\n";
cin >> asd1;
cout << "What is the first number in your equation?\n";
cin >> a;
cout << "What is the second number in your equation?\n";
cin >> b;
if ( strcmpi( asd1.c_str( ), "addition" ) == 0 ) {
result = a+b;
}
else if ( strcmpi( asd1.c_str( ), "division" ) == 0 ) {
result = a/b;
}
else if ( strcmpi( asd1.c_str( ), "subtraction" ) == 0 ) {
result=a-b;
}
else if ( strcmpi( asd1.c_str( ), "multiplication" ) == 0 ) {
result=a*b;
}
cout << "The answer is " << result << ".\n";
return( 0 );
}
-
I fixed it as well, well here is how I would have done it anyhow.
Edit: Fixed poor syntax.
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
float a, b, result;
string asd1;
cout << "What mathmatical operation would you like to do?\n";
cin >> asd1;
cout << "What is the first number in your equation?\n";
cin >> a;
cout << "What is the second number in your equation?\n";
cin >> b;
if ( strcmpi( asd1.c_str( ), "addition" ) == 0 ) {
result = a+b;
}
else if ( strcmpi( asd1.c_str( ), "division" ) == 0 ) {
result = a/b;
}
else if ( strcmpi( asd1.c_str( ), "subtraction" ) == 0 ) {
result=a-b;
}
else if ( strcmpi( asd1.c_str( ), "multiplication" ) == 0 ) {
result=a*b;
}
cout << "The answer is " << result << ".\n";
return( 0 );
}
This is C++ not C, you don't need to do a strncmp although that's effectively what a string == characters does. Also @Chef, learn to use the OR ( || ) condition and && condition. It's boolean logic.
-
I realize the difference, however it is much more effective then doing "asdf1 == Addition || asdf1 ==aDdition || asdf1 == adDition", etc. There are other methods like compare() in C++ but that is case sensitive as well.
Edit: Also, you could use the C++ functions tolower() or toupper() to convert the given strings to lower or upper case so they match then compare from there with the simply A == B.
Edit 2: I'm sure you know, being an administrator, that A == B will not return true if the strings are not case sensitive.
-
I realize the difference, however it is much more effective then doing "asdf1 == Addition || asdf1 ==aDdition || asdf1 == adDition", etc. There are other methods like compare() in C++ but that is case sensitive as well.
Edit: Also, you could use the C++ functions tolower() or toupper() to convert the given strings to lower or upper case so they match then compare from there with the simply A == B.
Edit 2: I'm sure you know, being an administrator, that A == B will not return true if the strings are not case sensitive.
That is where you use strncasecmp
-
That is C as well sir.
-
Wouldn't just making the input lower/upper case before checking be easier then?
One way would be to loop tolower()/toupper() for each char in str
-
Yeah if you want to use a pure C++ way of going about it. There is nothing wrong with mixing C with C++ . Seems like bluechill is against that though.
-
Yeah if you want to use a pure C++ way of going about it. There is nothing wrong with mixing C with C++ . Seems like bluechill is against that though.
Ok, then maybe looping every character in the str like this. Note, this is sudo code, I'm lazy.
str = "Hello!";
for(int i = 0; i > str.length(); i++){
if(str[i] > 'A' || str[i] < 'Z')
str[i] -= 'A';
}
-
Ok, then maybe looping every character in the str like this. Note, this is sudo code, I'm lazy.
str = "Hello!";
for(int i = 0; i > str.length(); i++){
if(str[i] > 'A' || str[i] < 'Z')
str[i] -= 'A';
}
I'm only against it when you're duplicating functionality C++ has. If you want to do case insensitive string comparisons then either do what WirelessDesert posted or use a strncasecmp. In my IRCd I use strncasecmp everywhere but I hardly use C when I can use C++ because it typically looks better.
-
This is C++ not C, you don't need to do a strncmp although that's effectively what a string == characters does. Also @Chef, learn to use the OR ( || ) condition and && condition. It's boolean logic.
This assumes that the == operator is overloaded to do what is intended, which is not always the case. The string class just makes ease of using that operator to compare the strings, because it has been overloaded to do a char by char (case sensitive) comparison.
Most cases, you'd use a string, but in the case that you're dealing with c-strings in C++, that == operator doesn't make a difference really, C++ or C.
char test1a[5] = "test";
char *test1b = "test";
std::cout << (test1a == test1b ? "Equal" : "Not Equal") << std::endl; // Not Equal
char test2a[5] = "test";
char test2b[5] = "test";
std::cout << (test2a == test2b ? "Equal" : "Not Equal") << std::endl; // Not Equal
Ok, then maybe looping every character in the str like this. Note, this is sudo code, I'm lazy.
str = "Hello!";
for(int i = 0; i > str.length(); i++){
if(str[i] > 'A' || str[i] < 'Z')
str[i] -= 'A';
}
If you absolutely needed to convert a string's case, I would use std::transform() from the <algorithm> header.
Ex:
std::string s("Testing");
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
std::cout << s << std::endl;
-
Excellent suggestion for use of std::transform(); +cookie. Most implementations of transform() use bitwise operations to take care of the task as well, so there are several potential performance benefits.