Hi, everyone! Anyone want to help me?

This is a discussion on Hi, everyone! Anyone want to help me? within the C++ Programming forums, part of the General Programming Boards category; Thanks for helping earlier. It still won't work for some reason. Code: #include <stdio.h> #include <iostream.h> int main(int argc, char ...

  1. #1
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942

    Hi, everyone! Anyone want to help me?

    Thanks for helping earlier. It still won't work for some reason.

    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    int main(int argc, char *argv[])
    {
            cout << "Stephen's C++ Calculator" << endl;
            cout << "Enter a number and push ENTER" << endl;
            char a;
            cin >> a;
            cout << a << endl;
            cout << "Now push a math sign and push ENTER" << endl;
            char sign;
            cin >> sign;
            cout << a << sign << endl;
            cout << "Now push a second number and push ENTER" << endl;
            char b;
            cin >> b;
            cout << a << sign << b << " equals" << endl;
    if (sign == '+')
            {
            int resultplus;
            resultplus = a + b;
            cout << resultplus << endl;
            cin.get();
            }
    if(sign == '-')
            {
            int resultminus;
            resultminus = a - b;
            cout << resultminus << endl;
            cin.get();
            }
    if(sign == '/')
            {
            int resultdivide;
            resultdivide = a / b;
            cout << resultdivide << endl;
            cin.get();
            }
    if(sign == '*')
            {
            int resultmultiply
            resultmultiply = a * b
            cout << resultmultiply << endl;
            cin.get();
            }
    cin.get();
    return 0;
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Couple of things, first off always tell us what isn't working. We don't feel like figuring out what the error is on our own!

    Second, make sure you set your results equal to zero when you initialize them, otherwise you'll likely get garbage.

    Last, you are declaring the numbers as chars, so the number '1' isnt actually 1, but like 49. Declare them as ints instead. This will also enable people to enter in numbers over 10 which chars won't let you do.

  3. #3
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    1- read the sig
    2- thanks

  4. #4
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    but how do you use decimals?

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I played a bit with this. If it helps, then great. If not, then disregard. :P

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main() 
    {   
       char choice;
       float value1, value2;  
       
       cout << "Calc program use ! to quit\n" << endl;
       
       for(; ;)
       {
          
          cout << "Enter an operator + - / *\n-> ";
          cin >> choice;
          cin.ignore(1);
          
          if(choice == '!') break;
          
          cout << "Enter first number: ";
          cin >> value1;
          cin.ignore(1);      
          
          cout << "Enter second number: ";
          cin >> value2;
          
          if(value2 == 0)
          {
          	cout << "Warning possible division by zero" << endl;
          	value2 = 1;
          	// do stuff
          }
          
          switch(choice)
          {
             case '+': cout << "\n\nThe sum is: " 
                            << value1 + value2 << endl;
                            break;
             case '-': cout << "\n\nThe difference is: "
                            << value1 - value2 << endl;
                            break;
             case '/': cout << "\n\nThe result is: "
                            << value1 / value2 << endl;
                            break;
             case '*': cout << "\n\nThe result is: "
                            << value1 * value2 << endl;
                            break;
             default: // some message 
                       break;
          }
       }
       
       return 0;
    }
    Play around abit with the error conditions, messages and such

    HTH
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    If you want to use decimals, then use floats/doubles instead of ints.

    Also, I undestand that you are a newbie to programming. What I mean by telling us what is wrong is tell us exactly what the problem is. Just saying help and showing us code turns off a lot of people because then we have to look at ALL the code and decipher what the problem MIGHT be. If you had said something like help me, I don't understand why my program is printing out wrong numbers for answers, then you'll get a lot more relevant help. As it was, we don't know if there is something wrong with the output, the input, the formating, or even if the problem is that it won't compile. Just helps us out a bit in helping you out
    Last edited by PJYelton; 03-15-2003 at 01:10 PM.

  7. #7
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    how do you use exponents and square roots?

  8. #8
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Include the math header

    square roots

    double sqrt( double x_);
    long double sqrtl( long double x_);

    powers

    double pow( double x, double y_);
    long double powl( long double x, long double y_);

    exponents

    double exp( double x_);
    long double expl( long double x_);
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    25

    Ok im a newbie..

    .. But its seams to me that you used char instead of int

    Char is only characters..
    try int a; instead of char a;
    im not sure about the math signs though.

    Again. Im a newbie ,and your probably better than me.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    I think this is what you want...

    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    int main(int argc, char *argv[])
    {
            cout << "Stephen's C++ Calculator" << endl;
            cout << "Enter a number and push ENTER" << endl;
            float a;
            cin >> a;
            cout << a << endl;
            cout << "Now push a math sign and push ENTER" << endl;
            char sign;
            cin >> sign;
            cout << a << sign << endl;
            cout << "Now push a second number and push ENTER" << endl;
            float b;
            cin >> b;
            cout << a << sign << b << " equals" << endl;
    		
    		if (sign == '+')
            {
            float resultplus;
            resultplus = a + b;
            cout << resultplus << endl;
            cin.get();
            }
    		
    		if(sign == '-')
            {
            float resultminus;
            resultminus = a - b;
            cout << resultminus << endl;
            cin.get();
            }
    		
    		if(sign == '/')
            {
            float resultdivide;
            resultdivide = a / b;
            cout << resultdivide << endl;
            cin.get();
            }
    		
    		if(sign == '*')
            {
            float resultmultiply;
            resultmultiply = a * b;
            cout << resultmultiply << endl;
            cin.get();
            }
    	cin.get();
    	return 0;
    }
    Why drink and drive when you can smoke and fly?

Popular pages Recent additions subscribe to a feed

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