Thread: What is wrong with my program

  1. #16
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    ok i put the variables as a global variable (if these are global variables)
    Code:
    float x;
    float y;
    char a;
    
    float gathering()
    and it says that answer is not being initialized.

  2. #17

  3. #18
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    i found the answer, this is my new code
    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;
    }
    thank you all for the help.
    Last edited by bijan311; 12-12-2009 at 11:13 AM.

  4. #19
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by bijan311 View Post
    ok i put the variables as a global variable (if these are global variables)
    Code:
    float x;
    float y;
    char a;
    
    float gathering()
    and it says that answer is not being initialized.
    You know, C structs like C++ classes can have constructors and as such, you can pre-initialize the internal variables to known or default values....

    Just saying...

    Jeff

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  4. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  5. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM