Thread: how to avoid taking in a variable

  1. #1
    Registered User
    Join Date
    Nov 2009

    Exclamation how to avoid taking in a variable

    my code is
    #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);
    	case 'x':
    		answer = x*y;
    	case '/':
    		answer = x/y;
    	case '+':
    		answer = x+y;
    	case '-':
    		answer = x-y;
    	case '^':
    		answer = pow (x,y);
    	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
    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
    Toronto, ON
    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
    Join Date
    Oct 2007
    Inside my computer
    Again, the void main issue: 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, 08:00 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM