What is wrong with my program

This is a discussion on What is wrong with my program within the C++ Programming forums, part of the General Programming Boards category; when i run this it gives these error Code: calc.h(12): error C2628: 'numbers' followed by 'float' is illegal (did you ...

  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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    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.

Page 1 of 2 12 LastLast
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, 02: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, 01: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, 01:03 PM

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