Thread: Help please..

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    7

    Help please..

    Hello all! My first post here. My teacher gave me this code below for me to debug, it was about 11 errors in it and it's just one error left.. The error I get is "error C2679" Just started my c++ class one week ago so I'm a complete beginner. Help would be much appreciated. //Carl

    Btw, the program is supposed to point at values inputed by user and show them on screen. It goes wrong when I try to input a name in a char array.

    Code:
    #include <iostream>
    #include <cstring>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    void initialize(char[], int *);
    void input(const char [], int &);
    void print(const char *, const int);
    void growOlder(const char [], int *);
    bool comparePeople(const char*, const int *, const char *, const int *);
    
    int main()
    {
    	char name1[25];
    	char name2[25];
    	int age1;
    	int	age2;
    	int *agePtr1 = &age1;
    	int *agePtr2 = &age2;
    
    	initialize(name1, &age1);
    	initialize(name2, &age2);
    
    	print(name1, age1);
    	print(name2, age2);
    
    	input(name1, age1);
    	input(name2, age2);
    
    	print(name1, *agePtr1);
    	print(name2, *agePtr2);
    
    	growOlder(name2, &age2);
    
    	if(comparePeople(name1, &age1, name2, &age2))
    		cout << "Both people have the same age" <<endl;
    	return 0;
    };
    
    void initialize(char name[], int *age)
    {
    	*name = 0;
    	*age = 0;
    };
    
    void input (const char name[], int &age)
    {
    	cout << "Enter a name: ";
    	cin >> name;                       //This is where I get the error :(  (error C2679)
    	cout << "Enter an age: ";
    	cin >> age;
    	cout << endl;
    }
    void print(const char name[], const int age)
    {
    	cout << "The value stored in variable name is: " << name << endl << "The value stored in variable age is: " << age << endl << endl;
    }
    
    void growOlder(const char name[], int *age)
    {
    	cout << name << " has been grown one year older" << endl << endl;
    	*age++;
    }
    
    bool comparePeople(const char* name1, const int *age1, const char *name2, const int *age2)
    {
    	return (age1 == age2 && strcmp(name1, name2));
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What does the text of the error say? What clues does it give you about the source of the error?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Probably something about trying to modify a const.

    So basically make your mind up
    - is it const, and the cin is wrong
    - you really need to do cin, and const is wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Thanks for replying, the text says:

    error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char []' (or there is no acceptable conversion)

    I tried making a temporary char array that stored 25 chars and then input a string instead of the "name" and then set name = temp;

    this compiles but I don't get any output so I guess it isn't possible to go that way.

    The error seems to be just on this line.. just getting the cin to actually get some values from the input doesn't seem to work.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    If you are a complete beginner, why would you be given such an advanced program?
    Ie: pointers, functions
    Double Helix STL

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Quote Originally Posted by Salem
    Probably something about trying to modify a const.

    So basically make your mind up
    - is it const, and the cin is wrong
    - you really need to do cin, and const is wrong.
    I don't think so though it would seem more logical Same rules apply for the age variable and it works without any problem. I'm pretty clueless

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> it would seem more logical Same rules apply for the age variable
    Is the age variable const? Have you learned about const?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't think so
    And what makes you think your opinion outweighs Salem's experience? It's clearly an issue of trying to assign to a const variable, regardless of whether you agree or not.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Quote Originally Posted by swgh
    If you are a complete beginner, why would you be given such an advanced program?
    Ie: pointers, functions
    That's what I thought as well, I have been sitting here for eight hours in a row now trying to complete it.. The reading material isn't very pedagogic either.. A nice beginner excersice would have been using cin and cout a bit, maybe some creation of classes and the like.. this is very hard for me as of now.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yup. That's why I had a teacher that used to say that a class room is not a place to use your brain, but your eyes and ears.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Ok, a good solution:

    Speak to your tutor and ask him how he excpects a totoal beginner in programmer to understand such an advanced program.

    You really should of been given somthing like this:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main ( void )
    {
       cout << "Hello World\nWelcome to the C++ computer language!" << endl;
    
       cin.get();
    
       return 0;
    }
    Double Helix STL

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    7
    Quote Originally Posted by Prelude
    >I don't think so
    And what makes you think your opinion outweighs Salem's experience? It's clearly an issue of trying to assign to a const variable, regardless of whether you agree or not.
    Sorry I had no clue that Salem had such experience, I just needed help. I know what a constant is and it seems like it is a constant. From what I've heard you can't change a constant.. thereof the name, but I must input a name in that char array nevertheless. Maybe that's the final problem with the code, I'll try and change it from constant to an ordinary variable and see if it works.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Speak to your tutor and ask him how he excpects a totoal beginner in programmer to understand such an advanced program.

    Apparently the OP was able to comlete the assignment, so why is it so bad? It doesn't seem like the OP is a complete beginner anyway.

  14. #14
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >From what I've heard you can't change a constant.

    If you use a constant in a class like this:

    Code:
    code Foo
    {
    public:
       Foo();
       ~Foo();
       int getAge() const;
    private:
       int age;
    };
    It means the value returned by getAge() will not change the variable age.
    Double Helix STL

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Sorry I had no clue that Salem had such experience, I just needed help.
    No problem, but it's best to assume that the people trying to help you know what they're talking about unless you can prove otherwise. If they do know their stuff, all is well. If not, those of us that do know what we're talking about will call them on it.

    >but I must input a name in that char array nevertheless
    If you need to input a name into that array, then it shouldn't be const.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed