I just started a C++ class at a local tech school. It is a self taught course, and I have just learned how much "self taught" applies to this course. The instructors have no experience in programming and can not answer the simplist of questions!!

The book I was asked to purchase is "Teach Yourself C++ in 21 Days", fifth edition.

The first couple of programs the book teaches are the basic "Hello World!" stuff. I was then asked to create the following program:

#include <iostream>
int main()
{
      int x = 5;
      int y = 7;
      std::cout << end1;
      std::cout << x + y << " " << x * y;
      std::cout << end;
      return 0;
}

While compiling, I received an error on line 6 (end1 : undeclared identifier)
and the same thing on line 8 (end : undeclared identifier).

I deleted both lines and the program worked. The problem is, the next program I am asked to create uses "std::end1" several times and I am getting the error "undeclared identifier" or "end1 is not a member of std".

Is there another way to list std::end1 in the source code that my compiler might recognize? I am using Visual Studios 2008.

Thanks,

Scott

Edited 6 Years Ago by Nick Evan: Added code-tags

First of all, it's not end1 (with number 1) but endl (with a letter ell), short for endline.
Second, I really doubt that "Teach Yourself C++ in 21 Days" will do you any good.

Comments
Exactly.

Try using std::endl instead :)

endl stands for end line, and is used for switching rows in console based C++ applications.

First of all, it's not end1 (with number 1) but endl (with a letter ell), short for endline.
Second, I really doubt that "Teach Yourself C++ in 21 Days" will do you any good.

Thanks for your reply. That corrected my problem!!!

The book may not do me any good, especially as hard headed as I am. It takes a little more effort for something to sink through my thick skull. But I did make it to Day 2 of the book, after using 3 different editors and compilers.

Thanks again!!

The alternative to prefixing "everything" with st:: is to place the line:

using namespace std;

after the list of includes (in this case between the first two lines of the program). Once you've read further in the book you should learn why this statement isn't necessarily the prefered option, though.

As for the opinion expressed nezachem regarding the book you have, you should be told that his/her opinion is just that, his/her opinion. I've reaqd both good and bad opinions about that book, as I have about others. Hopefully you got hold of an up to date edition of the book, and not the cheapest copy available, which will undoubtedly be out of date, though the major precepts should still be valid enen in an out of date edition.

The alternative to prefixing "everything" with st:: is to place the line:

using namespace std;

after the list of includes (in this case between the first two lines of the program). Once you've read further in the book you should learn why this statement isn't necessarily the prefered option, though.

As for the opinion expressed nezachem regarding the book you have, you should be told that his/her opinion is just that, his/her opinion. I've reaqd both good and bad opinions about that book, as I have about others. Hopefully you got hold of an up to date edition of the book, and not the cheapest copy available, which will undoubtedly be out of date, though the major precepts should still be valid enen in an out of date edition.

Thanks!!

I am actually reading about your tips right now in day 2 of the book. (It also just answered my first question). I have several sources that I refer to if the book is not clear enough to me. Reading the same information, but stated a little differently from another author is helping me.

Again, thanks for all the help and quick responses!!

I am now learning about if/else statements. An exercise my book asks me to do is to write a single "if" statement that examines two integer variables and changes the larger to the smaller, using only one "else" statement.

Here is what I wrote:

#include <iostream>
int main()
{
int x, y;
std::cout << "Enter a number for x: ";
std::cin >> x;
std::cout << "\nEnter a number for y: ";
std::cin >> y;
std::cout << "\n";


if (x > y)
(x = y);
std::cout << "For every time x > y, change the value of x to equal y.\n";


else //(y >= x)
(y = x);
std::cout << "For every time y > x, change the value of y to equal x.\n";


return 0;
}

I am getting an error that says: "illegal else without matching if

As I mentioned earlier, the instructors in the course I am taking are not programmers and they don't know how to help me.

What am I not seeing??

Any help would be appreciated!!!

Thanks,

Scott

Edited 3 Years Ago by happygeek: fixed formatting

Anytime you have an if statement without braces following it, only the next line down is included as part of the if statement.

if (condition)
      x = y;  //what gets evaluated pending true condition

cout <<"whatever "; //new program statement, if statement done

else  //compiler says where did this come from?

you need
if (condition)
{
     x  = y;
     cout <<"whatever ";
}

else  //immediately follows if statement because of { }
   etc.

Edited 6 Years Ago by jonsca: n/a

Anytime you have an if statement without braces following it, only the next line down is included as part of the if statement.

if (condition)
      x = y;  //what gets evaluated pending true condition

cout <<"whatever "; //new program statement, if statement done

else  //compiler says where did this come from?

you need
if (condition)
{
     x  = y;
     cout <<"whatever ";
}

else  //immediately follows if statement because of { }
   etc.

Thanks jonsca!!

That makes sense. The book did not spell it out that clearly.

I put the parentheses in and it worked.

Scott

Again, I am looking for help!!!:'(

The book has now moved into object oriented programming and I understand the basic concept, but I am having trouble with how information is being passed between the functions . I have read this chapter 5 times, and I still am having a problem with the following:

Book example (including comments):

class Car
{
     public:                                                 // the next five are public

          void Start();
          void Accelerate();
          void Brake();
          void SetYear(int year);
          int GetYear();

     private:                                               // the rest is private
          
          int Year;
          Char Model [255];
};                                                            // end of class declaration

Car OldFaithful;                                      // make an instance of car
int bought;                                             // a local variable of type int
[B]OldFaithful.SetYear(84);                        // assign 84 to the year
bought = OldFaithful.GetYear();            // set bought to 84[/B]
OldFaithful.Start();                                // call the start method

The problem I am having is: How was 84 passed from OldFaithful.SetYear(84) to OldFaithfulGetYear()?? To this point the book has said that all functions (or methods in this case) are case sensitive and also any change in spelling creates a completely new function. So how can the number 84 be passed from SetYear to GetYear if they are different functions? What am I missing??

Any help would be greatly appreciated.

Scott

Edited 6 Years Ago by WaltP: Use CODE tags. They are explained all over the site.

Look at the code for the SetYear() and GetYear() routines. The answers are there.

Or are you supposed to write them?

Look at the code for the SetYear() and GetYear() routines. The answers are there.

Or are you supposed to write them?

In this example, the book is not asking for anything to be done. It is simply giving an example of type class (Car) and what an object is (OldFaithful). The book then gives a brief discription of making member data private and says that to access private data in a class, public functions (accessor methods) must be created.

This is where I am having my problem. How does the compiler know to pass the information from SetYear to GetYear when they are entirely different functions? If the information is past to GetYear, why is it not also past to the other functions in the declaration as well? Is there something more I need to learn about the process of accessor methods??

Does not GetYear() first need to be called to return the value?

Thanks,

Scott

The program doesn't transfer the information directly. The set function changes the value of the member variable involved and the get function retrieves the member variable indicated. There is no direct connection to the two functions. The both involve the indicated member variable.

In this example, the book is not asking for anything to be done. It is simply giving an example of type class (Car) and what an object is (OldFaithful). The book then gives a brief discription of making member data private and says that to access private data in a class, public functions (accessor methods) must be created.

This is where I am having my problem. How does the compiler know to pass the information from SetYear to GetYear when they are entirely different functions? If the information is past to GetYear, why is it not also past to the other functions in the declaration as well? Is there something more I need to learn about the process of accessor methods??

Does not GetYear() first need to be called to return the value?

Thanks,

Scott

Opps, I just realized that GetYear was called with bought = OldFaithful.GetYear();

So, if I understand this right, because private: int Year (which is now 84) is the only int, it is passed to int GetYear(). Is that right?? If so, if there was another int function() in the class Car declaration, would that function also become the value of 84?

Thanks!!

No, it is not now 84. The only way to get 84 into Year is to write the code inside of SetYear() that actually loads the value from the parameter into the variable.

Scott,

I had to buy a second book to self-teach basic C++. I ended up buying, in addition to the book you have, Sams Teach Yourself C++ in One Hour a Day.

Anyway, that book won't tell you everything. The reason that code won't work is because the setYear and getYear methods aren't defined at all.

It would look like this:

#include <iostream>

using namespace std;

class car
{
private:
    int itsYear;

public:
    void setYear(int year)
    {
        itsYear = year;
    }

    int getYear()
    {
        return itsYear;
    }
};

int main()
{
    car Ford;

    Ford.setYear(1995);

    cout << "This vehicle's year is " << Ford.getYear() << "." << endl;
}

Remember, since itsYear is private, it must be accessed through a public method. Then, in main, we create the Ford object. You can set the year because above, in class, you defined the setYear method as being able to take an int. You pass the int 1995 into year, which passes it along into itsYear, which allows you to pull its value in getYear.

Does this help?

Edited 6 Years Ago by angstwad: n/a

Scott,

I had to buy a second book to self-teach basic C++. I ended up buying, in addition to the book you have, Sams Teach Yourself C++ in One Hour a Day.

Anyway, that book won't tell you everything. The reason that code won't work is because the setYear and getYear methods aren't defined at all.

It would look like this:

#include <iostream>

using namespace std;

class car
{
private:
    int itsYear;

public:
    void setYear(int year)
    {
        itsYear = year;
    }

    int getYear()
    {
        return itsYear;
    }
};

int main()
{
    car Ford;

    Ford.setYear(1995);

    cout << "This vehicle's year is " << Ford.getYear() << "." << endl;
}

Remember, since itsYear is private, it must be accessed through a public method. Then, in main, we create the Ford object. You can set the year because above, in class, you defined the setYear method as being able to take an int. You pass the int 1995 into year, which passes it along into itsYear, which allows you to pull its value in getYear.

Does this help?

Yes, it helps a lot. I have been trying to make something out of incomplete code from the book example. Your example above makes perfect sense!

Thanks,

Scott

This article has been dead for over six months. Start a new discussion instead.