C++ homework; using strings and math

This is a discussion on C++ homework; using strings and math within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using namespace std; int main() { string operation, firstnumber, secondnumber; float value; int number1, number2; ...

  1. #16
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
       string operation, firstnumber, secondnumber;  
       float value;
       int number1, number2;
                         
                    // Ask user for numbers and operator
              
       cout << "Please enter the first number(zero, one or two): " << endl;
       cin >> firstnumber;
       cout << "Please enter operator(plus, minus, divide, times): " << endl;
       cin >> operation; 
       cout << "Please enter the second number(zero, one or two): " << endl;
       cin >> secondnumber;
       
          
              // Solve the expression  
         if(firstnumber == "zero")
          {
              number1 = 0;
          }
         else if(firstnumber == "one")
          {
             number1 = 1;
          }
         else if(firstnumber == "two")
          {
            number1 = 2;
          }
         if(secondnumber == "zero")
         {
           number2 = 0;
         }  
        else if(secondnumber == "one")
         {
           number2 = 1;
         } 
        else if(secondnumber == "two")
         {
           number2 = 2;
         }  
    
         if(operation=="plus")
             {
             value = (number1 + number2);
             cout << "The value of the expression you entered is " << value << "." << endl;
             }
           else if(operation=="minus")
             {
             value = float(number1 - number2);
               if (value > 2)
               cout << "The expression you entered is not valid." << endl;
             else
             cout << "The value of the expression you entered is " << value << "." << endl;
             }
           else if(operation=="times")
             {
             value = float(number1 * number2);
               if (value > 2)
               cout << "The expression you entered is not valid." << endl;
             else
             cout << "The value of the expression you entered is " << value << "." << endl;
             }
            if(operation=="divide")
             {
             value = float(number1 / number2);
               if (value > 2)
               cout << "The expression you entered is not valid." << endl;
             else
             if (value == 0)
             cout << "Error cannot divide by zero." << endl;
             else
             cout << "The value of the expression you entered is " << value << "." << endl;
             }               
    return 0;
    }
    Wow thanks guys, everything works now once I figure out how to make 1/2=.5...thank you guys so much I have been working on this for over a week and it is due at 930 tonight so thanks again

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Before you turn that in, you should properly indent the code. Good exercise and oh-so-important.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    well it looks like I ran into another problem I didnt even know about. If the person typing in the numbers types in Two instead of two, it is suppose to say error..Is there a way to make it so if the word is capitalized it will say error or do I have to assign each one individually?

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Try
    std::transform(input.begin(), input.end(), input.begin(), &std::tolower);
    This should make the input (if it's a std::string) into all lowercase.
    Though I am not sure you are allowed to store it directly into the same variable.

    Oh yes, and include <algorithm> for std::transform and <locale> for std::tolower.
    Last edited by Elysia; 09-28-2010 at 02:12 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Check out the <cctype> standard header. std::tolower could come in handy.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    it says no such namespace std::tolower

  7. #22
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    this is what the program is suppose to do

    Good
    Enter the first operand (zero, one or two):
    one
    Enter an arithmetic operator (plus, minus, times, divide):
    plus
    Enter the second operand (zero, one or two):
    two
    The value of the expression you entered is 3.

    Bad
    Enter the first operand (zero, one or two):
    Two
    Enter an arithmetic operator (plus, minus, times, divide):
    plus
    Enter the second operand (zero, one or two):
    two
    The expression that you entered is not valid.

    I can do the good part fine, I just cannot do the bad part

  8. #23
    Registered User
    Join Date
    Sep 2010
    Posts
    20
    I also cannot use tolower. We cannot use functions loops or arrays

  9. #24
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    You can actually use tolower, it's just that it actually looks like this:

    Code:
    std::transform (aString.begin(), aString.end(), aString.begin(), ::tolower);
    I forget the exact reason why, but it seems to be a problem whenever you want to use anything from C like this (e.g. tolower(), rand()). It's not clear to me why std::anythingFromC does not resolve to a function pointer.

    Also, since this was left unresolved by Elysia, if the first three arguments to transform are the same container, it only means that the transformation occurs in place.

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quote Originally Posted by UWP student View Post
    I also cannot use tolower. We cannot use functions loops or arrays
    In that case, what you want to do is not possible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching within strings
    By Poincare in forum C Programming
    Replies: 3
    Last Post: 04-24-2009, 08:53 PM
  2. Library for math strings
    By Boomba in forum C Programming
    Replies: 4
    Last Post: 10-15-2005, 08:05 PM
  3. Easy Way To Replace Strings...
    By DeepBlackMagic in forum C++ Programming
    Replies: 8
    Last Post: 07-28-2003, 12:32 PM
  4. need help on a collection of strings
    By masongray in forum C Programming
    Replies: 2
    Last Post: 12-04-2002, 02:50 PM
  5. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21