What's wrong with this code?

This is a discussion on What's wrong with this code? within the C++ Programming forums, part of the General Programming Boards category; Just something I was trying to do off of the top of my head. Code: #include <stdafx.h> #include <cmath> #include ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    12

    Question What's wrong with this code?

    Just something I was trying to do off of the top of my head.


    Code:
    #include <stdafx.h>
    #include <cmath>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class vegetable{
    
    
    public:
    
    	int broccoli;
    	int carrot;
    	int bean;
    	static int corn;
    	int cauliflower;
    
    }
    
    
    int main()
    {
    	char cornanswer;
    	char Y = "Y";
    
    
    
    	cout << " Is  " << vegetable::corn << "a vegetable?";
    	cout << "Say Y for yes and N for no";
    	cin >> cornanswer;
    
    	if (cornanswer == Y)
    	{
    		cout << "YES!! You are very smart, it is a vegetable!";
    	}
    
    	else
    	{
    		cout << "Wow, you are very stupid! That answer is wrong!";
    	}
    
    	return 0;
    
    }
    I've looked it over dozens of times, and I can't seem to find what I did wrong. I think it has something to do with calling on the corn variable inside the class though.

    Build Errors.

    Code:
    1>Mess.cpp(24): error C2628: 'vegetable' followed by 'int' is illegal (did you forget a ';'?)
    1>Mess.cpp(25): error C3874: return type of 'main' should be 'int' instead of 'vegetable'
    1>Mess.cpp(27): error C2440: 'initializing' : cannot convert from 'const char [2]' to 'char'
    1>          There is no context in which this conversion is possible
    1>Mess.cpp(45): error C2440: 'return' : cannot convert from 'int' to 'vegetable'
    1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Need Semi colon at the end of vegetable
    Woop?

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Code:
    Mess.cpp(27): error C2440: 'initializing' : cannot convert from 'const char [2]' to 'char'
    "Y" is a constant char array consisting of the letter Y and the null terminator character (hence the const char[2] in the error message); a single character should be enclosed in single quotes, not double quotes.
    Last edited by rags_to_riches; 03-02-2010 at 06:10 PM. Reason: Clarification

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Also you need a semicolon after the declaration of vegetable:

    Code:
    ...
            int bean;
    	static int corn;
    	int cauliflower;
    
    }; //<- here
    The compiler thinks that main is an instance of class vegetable since the semicolon isn't there, but this is obviously not the case.
    Also you need single quotes around the 'Y' here:

    Code:
    if (cornanswer == Y)
    On another note,
    Code:
    cout << " Is  " << vegetable::corn << "a vegetable?";
    isn't going to print "Is corn a vegetable?", if that's what you were going for - vegetable::corn is an int, so it will be something like "Is 0 a vegetable?".

    Which makes me think of the comical situation that could ensue:
    Code:
    >./mess
    Is 0 a vegetable?
    Say Y for yes and N for no
    >N
    Wow, you are very stupid! That answer is wrong!
    >wtf
    wtf: command not found
    >sudo rm ./mess

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    Quote Originally Posted by bernt View Post
    Also you need a semicolon after the declaration of vegetable:

    Code:
    ...
            int bean;
    	static int corn;
    	int cauliflower;
    
    }; //<- here
    The compiler thinks that main is an instance of class vegetable since the semicolon isn't there, but this is obviously not the case.
    Also you need single quotes around the 'Y' here:

    Code:
    if (cornanswer == Y)
    On another note,
    Code:
    cout << " Is  " << vegetable::corn << "a vegetable?";
    isn't going to print "Is corn a vegetable?", if that's what you were going for - vegetable::corn is an int, so it will be something like "Is 0 a vegetable?".

    Which makes me think of the comical situation that could ensue:
    Code:
    >./mess
    Is 0 a vegetable?
    Say Y for yes and N for no
    >N
    Wow, you are very stupid! That answer is wrong!
    >wtf
    wtf: command not found
    >sudo rm ./mess
    XD. Hahaha Thanks for pointing out my mistakes, all of you. And thank you for adding that extra note Bernt. If I do want it to say 'corn', would it be the following in the vegetable class? :

    Code:
    const char corn = 'corn';

  6. #6
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Code:
    const char corn = 'corn';
    C Tutorial: Lesson 9 - Strings

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    You definitely do not want to be using a truncated multicharacter constant there.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    Quote Originally Posted by bernt View Post
    Ah, yes thank you. I couldn't remember the syntax for a string.

    Also, you guys took most of the bugs out, but when I compile this



    Code:
    #include <stdafx.h>
    #include <cmath>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class vegetable
    
    {
    
    
    public:
    
    	int broccoli;
    	int carrot;
    	int bean;
    	static int corn;
    	int cauliflower;
    
    };
    
    
    int main()
    {
    	char cornanswer;
    	char Y = 'Y';
    
    
    
    	cout << " Is  " << vegetable::corn << "a vegetable?";
    	cout << "Say Y for yes and N for no";
    	cin >> cornanswer;
    
    	if (cornanswer == 'Y')
    	{
    		cout << "YES!! You are very smart, it is a vegetable!";
    	}
    
    	else
    	{
    		cout << "Wow, you are very stupid! That answer is wrong!";
    	}
    
    	return 0;
    
    }

    I get these Build Errors:

    Code:
    1>Mess.obj : error LNK2020: unresolved token (0A000332) "public: static int vegetable::corn" (?corn@vegetable@@2HA)
    1>Mess.obj : error LNK2001: unresolved external symbol "public: static int vegetable::corn" (?corn@vegetable@@2HA)
    Which I've never seen before.

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624

    Smile

    Υοθ αρε ηοπελεσσ. Ι ψαν'τ τακε τηισ ανυ μορε!!!!!

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Add...
    int vegetable::corn;
    ...at global level.

    Since static variables aren't part of the instances of a class, you only declare them in the class, and not define them. So you need to add a definition at global level.

    And do try to speak english in an english-speaking forum!
    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.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And do try to speak english in an english-speaking forum!
    That is English, just typed with a Greek keyboard/Symbol font. A bit rude and unconstructive, though there might be some truth.

    You probably don't need that vegetable class as you have it at all, and need something like:

    Code:
        ...
        std::string plant = "corn";
        std::cout << "Is " << plant << " a vegetable?";
        ...
    You probably have some misunderstanding about variable names. These are identifiers used by a coder. To store "words" use a string object.

    If you want to pose multiple questions later, you'd probably need something like:

    Code:
    class Plant
    {
         std::string m_name;
         bool m_vegetable;
    public:
         Plant(const std::string& name, bool is_vegetable);
         std::string get_name() const;
         bool is_vegetable() const;
    };
    
    int main()
    {
        Plant plants[] = { Plant("corn", true), Plant("carrot", true), ... };
        ...
        std::cout << "Is " << plants[random].get_name() << " a vegetable.";
        if (answer == 'y' && plants[random].is_vegetable() || answer == 'n' && !plants[random].is_vegetable()) {
            std::cout << "Correct";
        }
        ...
    Last edited by anon; 03-03-2010 at 05:08 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by anon View Post
    That is English, just typed with a Greek keyboard/Symbol font. A bit rude and unconstructive, though there might be some truth.
    But the point is... can I read it? I cannot. Hence, the criticism.
    What is the point of a reply if others cannot read 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.

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Well done anon!!

    I wanted to test your deciphering abilities, although it was pretty simple...

    Sorry if i caused frustration, i guess i shouldn't me using this topic for my silly games.

  14. #14
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    Lol, thanks for all of your help guys. Started learning WinAPI programming from XoaX. Unless there's an easier tutorial list out there, please let me know.

  15. #15
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Quote Originally Posted by Code Thug View Post
    Lol, thanks for all of your help guys. Started learning WinAPI programming from XoaX. Unless there's an easier tutorial list out there, please let me know.
    This was my favorite

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

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