Author Topic: [Cpp] What is wrong w/ my code?  (Read 3832 times)

0 Members and 1 Guest are viewing this topic.

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
[Cpp] What is wrong w/ my code?
« on: August 12, 2013, 01:46:27 am »
Code: [Select]
#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;
        }
"To find happiness is to not always laugh."

xC

  • Guest
Re: [Cpp] What is wrong w/ my code?
« Reply #1 on: August 12, 2013, 02:26:12 am »
Use strcmpi() for checks on the type of mathematic operation to use. It's not case sensitive.

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: [Cpp] What is wrong w/ my code?
« Reply #2 on: August 12, 2013, 02:35:16 am »
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. 
"To find happiness is to not always laugh."

Offline Thor

  • Serf
  • *
  • Posts: 29
  • Cookies: 15
  • whoami?
    • View Profile
Re: [Cpp] What is wrong w/ my code?
« Reply #3 on: August 12, 2013, 02:39:17 am »
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.

Code: [Select]
#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;
        }
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.

xC

  • Guest
Re: [Cpp] What is wrong w/ my code?
« Reply #4 on: August 12, 2013, 02:51:13 am »
I fixed it as well, well here is how I would have done it anyhow.

Edit: Fixed poor syntax.

Code: [Select]
#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 );
}
« Last Edit: August 12, 2013, 04:43:19 am by xC »

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [Cpp] What is wrong w/ my code?
« Reply #5 on: August 12, 2013, 04:52:43 am »
I fixed it as well, well here is how I would have done it anyhow.

Edit: Fixed poor syntax.

Code: [Select]
#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 have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

xC

  • Guest
Re: [Cpp] What is wrong w/ my code?
« Reply #6 on: August 12, 2013, 04:57:56 am »
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.
« Last Edit: August 12, 2013, 05:41:48 am by xC »

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [Cpp] What is wrong w/ my code?
« Reply #7 on: August 12, 2013, 02:21:25 pm »
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
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

xC

  • Guest
Re: [Cpp] What is wrong w/ my code?
« Reply #8 on: August 12, 2013, 02:46:14 pm »
That is C as well sir.
« Last Edit: August 12, 2013, 02:47:49 pm by xC »

Offline WirelessDesert

  • Knight
  • **
  • Posts: 356
  • Cookies: 10
  • I think...
    • View Profile
Re: [Cpp] What is wrong w/ my code?
« Reply #9 on: August 12, 2013, 05:52:22 pm »
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
Check out my arduino project: Moving car - School project!
"I'm like current, I always take the easiest route."

xC

  • Guest
Re: [Cpp] What is wrong w/ my code?
« Reply #10 on: August 12, 2013, 06:32:23 pm »
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.

Offline WirelessDesert

  • Knight
  • **
  • Posts: 356
  • Cookies: 10
  • I think...
    • View Profile
Re: [Cpp] What is wrong w/ my code?
« Reply #11 on: August 12, 2013, 09:41:59 pm »
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.


Code: (C++) [Select]
str = "Hello!";
for(int i = 0; i > str.length(); i++){
  if(str[i] > 'A' || str[i] < 'Z')
    str[i] -= 'A';
}
Check out my arduino project: Moving car - School project!
"I'm like current, I always take the easiest route."

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [Cpp] What is wrong w/ my code?
« Reply #12 on: August 13, 2013, 12:17:51 am »

Ok, then maybe looping every character in the str like this. Note, this is sudo code, I'm lazy.


Code: (C++) [Select]
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.
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: [Cpp] What is wrong w/ my code?
« Reply #13 on: November 01, 2013, 03:04:16 am »
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.

Code: (cpp) [Select]
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.


Code: (C++) [Select]
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:
Code: (cpp) [Select]
std::string s("Testing");
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
std::cout << s << std::endl;
« Last Edit: November 01, 2013, 03:24:57 am by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [Cpp] What is wrong w/ my code?
« Reply #14 on: November 01, 2013, 09:03:01 pm »
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.
-Xires