Thread: You think this is worthy of sending into the website to be posted?

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079

    You think this is worthy of sending into the website to be posted?

    I know this is a simple program, but I like the idea of what it does and I remember when I first started programming, it's one of the things I really wanted to make.

    It's a simple calculator that accepts an undetermined amount of input.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
      double numInput[100];
      char signInput[100];
      double answer;
      int x = 0;
      
       cout << "Type a standard mathematical operation ending with equals: " << endl
            << "ex. 5 + 5 =" << endl;
       
       cin >> numInput[0];
       cin >> signInput[0];  
          
       do {
          x++;
          cin >> numInput[x];  
          cin >> signInput[x];
       } while (signInput[x] != '=');
       answer = numInput[0];
       
       for (int y = 0; y <= x; y++) {
           switch (signInput[y]) {
               case '+':
                  answer += numInput[y+1];
                  break;
               case '-':
                  answer -= numInput[y+1];
                  break;
               case '*':
                  answer *= numInput[y+1];
                  break;
               case '/':
                  answer /= numInput[y+1];
                  break;     
           }
       }  
       
       cout << "Your answer is: " << answer << endl;
       
       return 0;
    }
    What do ya think? If you can think of ways of improving it, please say. I love critiques.
    Sent from my iPadŽ

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    I like the concept. I would put in an order of operations now. For example:
    5+2+3*5/6=

    It will do 3*5 then divide that result by 6, then add the remaining values. Maybe even put in a function to balance single variable equations after that. Then throw in more operators like sin, cos, tan, arccos, arctan, powers, etc. Good work.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    nice work dude but here's what else u can do:
    -after displaying the result make it ask u again if u would like to do another calculation or if u would like to quit. here's some code extracted from one of my projects:

    Code:
    int again() 
    	{
    		int option;
    		cout << "again? (1 = yes, 2 = no)  ";
    		cin >> option;
    		switch (option)
    		{
    		case 1:
    			dreptunghi();
    			break;
    		case 2:
    			return 0;
    			break;
    		default:
    			cout << "introduce un raspuns valid! \n";
    			again();
    			
    		}
    		return 0;
    	}

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yeah, order of operations seems like a good idea. I'll also add some more functions, I would have to change the code a bit since it couldn't just read number, sign, number, sign... but it's no problem and it's another good thing to show in the program. I'm also considering the abilty to use an "ans" arguement to append the last equation.

    I'm glad that you see potential, thanks for the reply. I'll spruce it up a bit, make it more interesting.

    EDIT: Here is a little Order of Operations logic. It works fine, it just looks a bit screwy, what do you think?
    Code:
       if (signInput[0] != '*' && signInput[0] != '/')
          answer = numInput[0]; 
       
       for (int y = 0; y <= x; y++) {
           switch (signInput[y]) {
               case '*':
                  numInput[y+1] *= numInput[y];
                  signInput[y] = '+';
                  numInput[y] = 0;
                  break;
               case '/':
                  numInput[y+1] = numInput[y] / numInput[y+1];
                  signInput[y] = '+';
                  numInput[y] = 0;
                  break;     
           }
       }
        for (int y = 0; y <= x; y++) {
           switch (signInput[y]) {
               case '+':
                  answer += numInput[y+1];
                  break;
               case '-':
                  answer -= numInput[y+1];
                  break;     
           }
       }
    Last edited by SlyMaelstrom; 11-08-2005 at 02:56 AM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Since this is a pretty basic program, why not try to demonstrate some of the basic rules:

    1) Always initialize your variables.
    Code:
    double numInput[100] = {0};
    char signInput[100] = {0};
    double answer = 0;
    2) Use read statements in a loop conditional. If there are any stream errors, the loop will terminate and not vainly try to read in more data or enter into an infinite loop. Your program can enter into an infinite loop if a stream error occurs while reading the data.

    You could actually do this:
    Code:
    for(int x = 0; (cin>>numInput[x]>>signInput[x]) && (signInput[x] != '='); ++x)
    but you might want to opt for the more readable:
    Code:
    int x = 0;
    while( cin>>numInput[x]>>signInput[x]  && signInput[x] != '=' )
    {
    	++x;
    }
    or,
    Code:
    int x = 0;
    while( cin>>numInput[x]>>signInput[x] ) 
    {
    	if(signInput[x] == '=')
    		break;
    	
            ++x;
    }
    3) Check for bad input. Currently your program crashes.

    You think this is worthy of sending into the website to be posted?
    It's posted right now. Tell your mom you're famous.
    Last edited by 7stud; 11-08-2005 at 04:52 AM.

  6. #6
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    when u can do something like this:
    http://www.algebra.com/algebra/homework/general/#Solve

    Then u can tell ur mom and ur dad and frankly, the whole family ur famous

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You might want to read up on how to convert an equation from infix to a postfix expression. This will make your life a lot easier when you try to handle order of operations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How's my new Website
    By asbo60 in forum General Discussions
    Replies: 53
    Last Post: 07-10-2009, 10:10 AM
  2. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 07:38 PM