how to avoid taking in a variable

This is a discussion on how to avoid taking in a variable within the C++ Programming forums, part of the General Programming Boards category; my code is Code: #include <iostream> #include <cmath> using namespace std; float math() { float x; float y; char a; ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Exclamation how to avoid taking in a variable

    my code is
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    float math()
    {
    	float x;
        float y;
        char a;
    	float answer;
       cout<<"enter an problem (operations are x,/,+,-,^,s2(square root))";
       cin>> x >> a >> y;
    
    	switch (a){
    	case 's':
    		answer = sqrt (x);
    		break;
    	case 'x':
    		answer = x*y;
    		break;
    	case '/':
    		answer = x/y;
    		break;
    	case '+':
    		answer = x+y;
    		break;
    	case '-':
    		answer = x-y;
    		break;
    	case '^':
    		answer = pow (x,y);
    		break;
    	}
    	return answer;
    }
    and to get the answer for square root you have put in input for y to take in, I was wondering how to avoid taking in input for y only in the square root function.

    Thank you

  2. #2
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    so you are asking how to 'do something' .... 'if something'
    Asking a question you already know the answer to might teach you something you did not know...

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Read the whole line as a string, then parse the string to pick out the x, a & optional y arguments.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Again, the void main issue: SourceForge.net: Void main - cpwiki
    Unless you really are working on a very specific embedded system, don't use void main.

    Even easier would actually be to just use a couple of if statements. It's all pure logic. Make a flowchart and implement it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a grasp on pointers
    By lilrayray in forum C Programming
    Replies: 23
    Last Post: 07-26-2006, 06:58 PM
  2. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  3. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  4. About classes and HINSTANCE variable to Main
    By conright in forum Windows Programming
    Replies: 2
    Last Post: 01-01-2003, 07:00 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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