Thread: What is wrong with my program

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

    Exclamation What is wrong with my program

    when i run this it gives these error
    Code:
    calc.h(12): error C2628: 'numbers' followed by 'float' is illegal (did you forget a ';'?)
    calc.h(26): error C2440: 'return' : cannot convert from 'float' to 'numbers'
    well here is the header were most of the code is

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    struct numbers
    {
    float x;
    float y;
    char a;
    }
    float gathering()
    {
    	float b;
    	float c;
    	char z;
    	numbers numb;
    
    	cout<<"enter an operation (select o for the operations)\n";
    	cin>> b >> z >> c;
    
    	b = numb.x;
    	z = numb.a;
    	c = numb.y;
    
    	return c;
    }
    
    float math()
    {
        float answer;
    	numbers numb;
    //this next part decides if you add, multiply, divide, or subtract depending on the input
    	float y;
        switch (numb.a){
    	case 's':
    		answer = sqrt (numb.x);
    		break;
    	case 'x':
    		answer = numb.x*numb.y;
    		break;
    	case '/':
    		answer = numb.x/numb.y;
    		break;
    	case '+':
    		answer = numb.x+numb.y;
    		break;
    	case '-':
    		answer = numb.x-numb.y;
    		break;
    	case '^':
    		answer = pow (numb.x,numb.y);
    		break;
    	}
    	return answer;
    }
    and the .cpp file
    Code:
    #include <iostream>
    #include "calc.h"
    
    using namespace std;
    
    int main()
    { 
    	float answer = math();
    
    	cout<< answer <<"\n";
    
    	system("pause");
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    struct numbers
    {
    float x;
    float y;
    char a;
    }
    I believe you need a semi colon after the "}".

    Code:
    numbers numb;
    If you get problems with this you probably need to put "struct " in front of "numbers", or use a "typedef" to define the type so you can simply use the type "numbers" as you are now.

    Code:
    answer = numb.x/numb.y;
    Of course here you need to make sure that "numb.y" isnt 0, otherwise the world will end.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    /dev/pts/0
    Posts
    29
    Quote Originally Posted by nadroj View Post
    Code:
    numbers numb;
    If you get problems with this you probably need to put "struct " in front of "numbers", or use a "typedef" to define the type so you can simply use the type "numbers" as you are now.
    In C++, the class/struct are not required when declaring an instance of a class or structure. Only in C (or perhaps some very old C++ compilers) are they absolutely required.

    It's a good habit to have to put them in, though.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by strange View Post
    In C++, the class/struct are not required when declaring an instance of a class or structure. Only in C (or perhaps some very old C++ compilers) are they absolutely required.

    It's a good habit to have to put them in, though.
    It is a habit that has not been adopted by any C++ expert whose code I have read. Considering that C programmers do make use of typedef to avoid this "good habit", I would argue that it isn't a good habit: you should do it in C when the struct name does not have an alias, but otherwise there is no reason to do so.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2009
    Location
    /dev/pts/0
    Posts
    29
    Quote Originally Posted by laserlight View Post
    It is a habit that has not been adopted by any C++ expert whose code I have read. Considering that C programmers do make use of typedef to avoid this "good habit", I would argue that it isn't a good habit: you should do it in C when the struct name does not have an alias, but otherwise there is no reason to do so.
    Aye, I suppose you have a valid point. I personally put in the struct keyword in C, as I feel that it helps keep things easier to manage.

    But you are correct, there is no real reason to do so. My mouth (fingers, actually) got a little ahead of me there. I apologize.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    i put a semicolon after the struct as shown here:
    Code:
    struct numbers
    {
    float x;
    float y;
    char a;
    };
    and it runs for a second then gave me the message
    Code:
    Run-Time Check Failure #3 - The variable 'numb' is being used without being initialized.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    float math()
    {
        float answer;
    	numbers numb;
    	float y;
        switch (numb.a){
    What do you suppose is in numb when that switch is hit??

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Quote Originally Posted by rags_to_riches View Post
    Code:
    float math()
    {
        float answer;
    	numbers numb;
    	float y;
        switch (numb.a){
    What do you suppose is in numb when that switch is hit??
    i don't know...

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Hes trying to get at:
    Run-Time Check Failure #3 - The variable 'numb' is being used without being initialized.
    That is, you never did initialize 'numb' though youre using it! So when you do
    Code:
    	numbers numb;
    	float y;
        switch (numb.a){
    The value of 'numb.a' could literally be (almost) anything. Theres no point in checking 'numb.a', because you never set it to anything (initlized it). You probably want to set it first, then work with it.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by nadroj View Post
    Code:
    answer = numb.x/numb.y;
    Of course here you need to make sure that "numb.y" isnt 0, otherwise the world will end.
    Division by 0 for floats is legal, I believe. I think the standard allows it. On most architectures, at the very least, division by 0 for floats will result in infinity (IF numb.x doesn't equal 0).

    Quote Originally Posted by bijan311 View Post
    i don't know...
    Nor does the compiler. Hence the error.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I never knew that... well probably because Ive never tried it. Whether its allowed or not ("legal") I dont think makes it OK to have your program accept/execute it. I imagine for most homework assignments it would be acceptable though.

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    well it's not a homework assignment, it's a hobby thing

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    What is 'numb.a' SUPPOSED to be?! Or, what do you EXPECT it to be?! You MUST expect it to be something, because your doing the "switch" statement on it.

    If youre declaring the variable then immediately doing a switch on it, then it doesnt have any (useful) data in it. Because of that, your probably doing something wrong. Should "numb" be passed as an argument to the function? Should you be checking a different variable altogether?

    I/we dont know what you want to do or whats in your head, so we cant figure out what to type for you.

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    well numb.a is the operation the was gathord in the function "float gathering"

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I never even noticed that, but that code isnt correct either.
    Code:
    float gathering()
    {
    	float b;
    	float c;
    	char z;
    	numbers numb;
    
    	cout<<"enter an operation (select o for the operations)\n";
    	cin>> b >> z >> c;
    
    	b = numb.x;
    	z = numb.a;
    	c = numb.y;
    
    	return c;
    }
    You declare 'numb' then access its fields (.x, .a, .y) without having initialized them. Also, this "numb" is not the same "numb" in the other function. They are two different local variables in two different functions. If you want to use the same variable, then pass it to the two functions from main or use a global variable (which is totally not necessary).

    If you arent sure what a global/local variable is or what "scope" is then read some tutorials before continuing.

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