0

Hi Everyone I am trying to learn the basics of classes and objects. As far as I know my syntax is correct but I get these error messages with my program ...

Error: 'A' was not declared in the scope

Error: 'a' was not declared in the scope

Error: 'UIClass' was not declared in the scope

Error: 'AgeObject' was not declared in the scope

Error: Expected ';' before 'NameObject'

Error: 'NameObject' was not declared in the scope

Error: Expected ';' before 'ResultObject'

Error: 'ResultObject' was not declared in the scope

#include <iostream>
#include <string>
 using namespace std;

class UI{

public:

void Age(){
int a;
cout << "Age?" << endl;
cin >> a;}

void Name(){
string A;
cout << "Name" << endl;
cin >> A;}

void Results(){
cout << "Your name is " << A << "and you are " << a << " years old." << endl;
 }


};


int main ()

{

cout << "Enter Your Name and Age?" << endl;

UIClass; AgeObject;
AgeObject.Age();

UIClass NameObject;
NameObject.Name();

UIClass ResultObject;
ResultObject.Results();

return 0;

}
  • 4
    Try removing all but 5-10 lines and working on that until it compiles. Then add in more bits of code. – John Zwinck Mar 29 '14 at 08:27
  • 3
    Whatever book or tutorial you have been using, I think you need to find another, because there are so many problems with your code, that I have to conclude that whatever you're using now is no good. You might want to check e.g. [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Mar 29 '14 at 08:29
  • 1
    First major problem: "using namespace std;" - Stop it. Stop it right now and never do it again. Namespaces are designed to prevent ambiguity and naming collision. When you say "using X", you're dragging everything in there into global scope. Simply use std::string, std::cout, somenamespace::something_else, etc. –  Mar 29 '14 at 08:36
  • @JoachimPileborg Thanks man I know it's not the greatest but I'm trying to get better everyday. Also I'm currently learning from Cplusplus.com and the Newboston.com – MastersProgression Mar 30 '14 at 02:31
  • 1
    @TechnikEmpire Ok Thanks for the advise. The website where I watch the tutorials said to always keep it there. I think a legit book is the way to go about learning from what I'm hearing. – MastersProgression Mar 30 '14 at 02:33
  • 1
    @MastersProgression Look for one of the many books written by the guy who invented C++, Bjarne Stroustrup. For beginners, you might look at "Programming Principles and Practice Using C++". –  Mar 30 '14 at 07:28

3 Answers3

2

So in your code, in the Results method, you're trying to access variables that are not declared in there.

So you have:

void age()
{
    // does stuff with age
} 

void name()
{
    // does stuff with name
}

The variables only exist in these methods. So when you try to get to them from Results() you'll get an "Out of scope" error.

So what you could do is declare four additional methods, setAge, setName which will take in arguments as follows:

class UI
{
    private:
        int age;
        string name;

    public:
        void setAge(int ag)
        {
            age = ag;
        }

        int getAge()
        {
            return age;
        }

Then you'd change your void age() method to something like this:

void age()
{
    // Do the stuff you've already done
    setAge(a);
}

Then when you try to get the output done:

cout << "Your name is " << getName() << " and you are " << getAge() << " years old." << endl;

Which ever book you're using, they really should have explained this sort of stuff. If not, I'd get a new one. This is one of the most basic programs you'll ever write in C++.

I've not given you the complete answer, but this should encourage you and give you a starting point. Hope it all helps.

Happy coding.

0

Error clearly says the variables are declared out of scope. The variable int a and string A are declared inside the function, they are out of scope when you try to use other than that function. Declare as the public variables of a class. Also you have instantiated 3 UI objects to call three functions,you should not do that because each object has its own memory. Instantiate one object and call the functions.

 class UI
   {

     public:
     int a;
     string A;
     void Age()
     {
       //int a;  remove the local varaible,  'a' can't be used outside function Name
       cout << "Age?" << endl;
       cin >> a;
     }

     void Name()
     {
       //string A;  remove the local varaible, 'A' can't be used outside function Name
       cout << "Name" << endl;
       cin >> A;
     }

      void Results()
      {
        cout << "Your name is " << A << "and you are " << a << " years old." << endl;
      }   

    };
Balu
  • 2,247
  • 1
  • 18
  • 23
0

You have declared a and A to be local variables of the Name and Age methods, so they are not available in the Results method. You probably wanted to make them member variables instead. Move their declarations to the class scope instead of the method scope. Also, a and A are the worst names ever!

Then, you declare three separate instances of the class (except you added a semi-colon between the class and instance name), and call each method on a different instance. Try creating a single instance, and calling all three methods on it.

Oh, and please please please learn how to indent code...

pat
  • 12,587
  • 1
  • 23
  • 52