Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Square program

Posted on 1999-06-28
15
Medium Priority
?
345 Views
Last Modified: 2010-04-02
Can someone help me please?  Maybe explain what this program is to do?  I have the Main program below, and the output underneath that.  I am to write sq1.h and sq1.cpp.  Thank you!  :-)

#include <iostream.h>
#include "sq.1.h"

void main()
{
  square sq1,sq2(6), sq3(-6),sq4(14);

cout<<"Size of sq1: "<<sq1.size()<<endl<<sq1;
cout<<"Size of sq2: "<<sq2.size()<<endl<<sq2;
cout<<"Size of sq3: "<<sq3.size()<<endl<<sq3;
cout<<"Size of sq4: "<<sq4.size()<<endl<<sq4;

sq1=-6;
cout<<"Size of sq1: "<<sq1.size()<<endl<<sq1;
sq1=16;
cout<<"Size of sq1: "<<sq1.size()<<endl<<sq1;
sq1=7;
cout<<"Size of sq1: "<<sq1.size()<<endl<<sq1;

sq3=sq3-5;
cout<<"Size of sq3: "<<sq3.size()<<endl<<sq3;
sq3=sq3+3;
cout<<"Size of sq3: "<<sq3.size()<<endl<<sq3;
sq4=sq4+3;
cout<<"Size of sq4: "<<sq4.size()<<endl<<sq4;
sq4=sq4-5;
cout<<"Size of sq4: "<<sq4.size()<<endl<<sq4;
sq2=sq2+4;
cout<<"Size of sq2: "<<sq2.size()<<endl<<sq2;
sq2=sq2-2;
cout<<"Size of sq2: "<<sq2.size()<<endl<<sq2;

~~~~~~~~~~The output~~~~~~~~~~~~~

Size of sq1: 4
* * * *
* * * *
* * * *
* * * *
Size of sq2: 6
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
Size of sq3:2
* *
* *
Size of sq10:
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
Size of sq1: 2
* *
* *
Size of sq1: 10
* * * * * * * * * *
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
* * * * * * * * * *  
Size of sq1: 7
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
Size of sq3: 2
* *
* *
Size of sq3: 5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Size of sq4: 10
etc
etc
Size of sq4: 5
etc
etc
Size of sq2: 10
etc
etc
Size of sq2: 8
etc
etc
0
Comment
Question by:Catt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +5
15 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1198646
Homework?
0
 

Expert Comment

by:lypku
ID: 1198647
What do you want to know!
Isn't the result you hoped?
0
 

Expert Comment

by:lypku
ID: 1198648
What do you want to know!
Isn't the result you hoped?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Catt
ID: 1198649
I do not understand a few things in the program.  I cannot get the squares to print out correctly.  I have tried nested for loops, is that right?  I don't quite understand it.  And no this isn't homework, I am a 32 year old career woman trying to learn c++ and VB as well.  I am using some books with self help programs like this but there are no answers or anything in the book.  Thanks for any help you may be able to give!!  :-)
0
 
LVL 23

Expert Comment

by:chensu
ID: 1198650
I am sorry that I misunderstood you.

square is a class, which should at least contain the following member functions.

square();
square(int size);
int size();
operator +;
operator -;
operator <<;

You may show us what you have done in order to get it to work.
0
 
LVL 3

Expert Comment

by:Try
ID: 1198651
You sound like a beginner, and as such, you might find it more rewarding to fully concentrate your energy, attention, and focus on just one language for the time being, considering that both C++ and VB taken together, can be too big a bite for you to chew on, just now.

C++ is a huge and complicated language, that even after working with it for twenty years, you would not be able to say, "I know everything about it!"  It is that enormous!  Therefore, for you to try and take on both languages at the same time will not yield you the same result as if you were to just concentrate on one and give it your undivided attention.

Having said that, because I'm somewhat partial to C++, I would encourage you to go with it because it is a lot more powerful in accomplishing things VB is not design to do, meaning, it offers you a wide range of usage from writing simple stand alone Console Applications, to incorporating other technolgies (like MFC, ActiveX, COM, etc.), to getting into the heart of the operating system using API's.  You can choose which level you feel most comfortable at, and stay there, or continue expanding into deeper and more specialized areas (e.g. application or system programming).

Note:  C++ is not the same as Visual C++.  C++ is the language that Visual C++ builds on, while incorporating Windows technologies.  IOW, knowing C++ is NOT the same as knowing Visual C++.  After you believe you are able to stand on your two legs with C++, then you are ready to take on VC++.  Working with VC++, you MUST already know C++.
0
 
LVL 3

Expert Comment

by:Try
ID: 1198652
I wish you success!!
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1198653
You probably need a header file that looks something like:

class square {
public:
    square(int sz = 4);
    square operator+(int sz);
    square operator-(int sz);
    int size() const { return mSize; }
private:
    int mSize;
};

ostream& operator<<(ostream& o, const square& rhs);

A nested loop inside the ostream operator would be fine to display the squares.
0
 
LVL 3

Expert Comment

by:Try
ID: 1198654
I don't know what books you may be using, but if I could suggest one, it would be "C++ How To Program" [3rd. Edition] by Deitel & Deitel.  It is a good book, used as text in several universities, and it'll take you from ground zero to intermediate.  Even after programming in C++ for six years, I still refer to it (from time to time) to check up on certain points.

After you're through reading that one, move on to "C++ Primer 3rd. Edition" by Stan Lippman (though this book contains some errors: syntactically, most are typographical) which is why you'll need the knowledge foundation from the first book I mentioned.

"C++ Primer" packs the foundation of your knowledge more solidly, preparing you for the next one, "C++ Effective Object-Oriented Software Construction" by Kayshav Dattatri, which transposes and sharpens your knowledge of the language, into a potent tool.  (Again some errors, but only a few.)

Absolutely next in line is, "Effective C++" and "More Effective C++" by Scott Meyers.  Nobody (and I mean NOBODY), can say they know how to program in C++ like an expert if they have not read and practiced what these two books says.  The knowledge they impart are like gold, ... solid gold.  (Very interesting readings; not boring at all.)

Perhaps on the same level with Scott Meyers, is "C++ Strategies and Tactics" by Robert Murray.  (Interesting and powerful, but lacks the humor and light-heartedness of Scott Meyers.  Knowledge galore!)

Having read all these books, and then put them into action, you are now a black belt in the use and expertise of C++.

There are a lot of readings ahead, and along with it, you must also experiment; the two go hand in hand.

I wish you well, and I wish you success!!
0
 

Author Comment

by:Catt
ID: 1198655
Thanks everyone!  I truly appreciate it.  =)

STILL working on the square program.....unsuccessfully!  Will keep trying!
0
 
LVL 3

Expert Comment

by:Try
ID: 1198656
If I could offer a suggestion.  There are times in everyone's life when they seemed to be hitting a brick wall.  Instead of keep hitting, and hitting at it, why not stop, put it aside, and try something else; then return to it at a later date.  Sometimes, just the change in venue opens up other creative channels, and when you feel up to the battle again, simply return to task and give it another try.

Remember, "He who fights and walk away, will live to fight another day!"  You're not giving!  You're simply calling a truce!
0
 
LVL 3

Expert Comment

by:Try
ID: 1198657
I meant, "You're not giving up!  You're simply calling a truce!"
0
 

Expert Comment

by:doin
ID: 1198658
guess u need a operator= in your square class too.(beside the once chensu mentioned)
in your outpout is there any square size less than 2 or greater than 10?
if not, set any values <= 2 to 2 an d any >= 10 to 10

i don't want to ruin your practice, but if you're still havin problems just tell me
0
 

Expert Comment

by:dmaroff
ID: 1198659
I have to agree with Try.  To add, VB is a good language but don't start off with it.  The reason being is that people who start off with VB get too attached to the user interface portion of programming.  They get a false sense of what programming really is because VB will in fact spoil you.  A lot of the work has been done for you already, behind the scenes.  VB only programmers don't seem to learn the algorithm and data structures side of programming, which is the heart of every good program.  Thats because a lof of it is built into the functions they call.  Remember, Bill Gates wants to make his Operating System look attractive to programmers.  So Microsoft developed VB.  

Thats why 9 out of 10 programming books you find in those giant book stores now teach GUI programming because everyone wants to learn it.  There is lots of money in writing those books!!  Honestly, the GUI stuff is the easy part.  My advice is to stay away from any GUI programming until you understand how to code console style apps.  Console meaning Command Line like DOS, UNIX, etc.  VB is an awesome language don't get me wrong.  Once you get good at the science of programming that stage when you need to quickly code up a fast Windows app, you would use VB, and you would use it smarter.

My opion, if I had to choose between the two languages to learn I would choose C/C++ over VB, and take my advice...    LEARN C BEFORE C++!!!!!!!    C++ is a great language and is really a joy to use, but its hyped a lot lately.  So much in fact that some people are learning virtual functions before pointers!!!  At that stage they eventually learn how pointers work along with objects.  Thats too much to absorb.  Thats where they get into trouble. Object Oriented Programming in C++ will kill you if you don't understand basic C concepts like pointers, structures, scope, variable lifetime and advanced stuff like searching and sorting algorithms and Multi-Threading using, Mutexes, Semaphores, the list goes on.

Also, stay with ONE language.  At least until your comfortable enough to write apps that people will use.  Then move on to VB if you like.  By the way, I just learned VB about 5 months ago, and because of my C/C++ background I wrote a fully functional app in 3 days!!


One more thing. People will always tell you that a certain book is the best to use or learn from.  Everyone has this big opinion on what books to get.  You know what, THERE IS NO MAGIC BOOK!!  I've tried them all.  The best learning experience is when you run into trouble and you figure it out.  That means EFFORT!!  You just gotta keep writing programs, there is now way around it.  But for a first book, stick with something that is around 300 pages or so.  Don't buy one of those 1200 page monster books because they contain more info.  They will only intimidate you.  Those books are intended more for pros who need a reference.  Get a book you understand, stick with it and have patience.   At least thats my experience.

Good Luck,
-DM
0
 
LVL 1

Accepted Solution

by:
roaknog earned 600 total points
ID: 1198660
//header file sq.h********************************************************************
class c_box      
{
public:
void sq (void);      
      

private:
int x;            // vertical
int y;            // horizontal
};

// main file sq.cpp**************************************************
#include <iostream.h>
#include "sq.h"

int ssqq = 0;

void c_box::sq(){
      x = ssqq;
      y = ssqq;
      for (int i = 0; i < y; i++){
            for (int i = 0; i < x; i++){
            cout << "* ";}
            cout << "\n";
      }
}

void main() {
do_it_again:;
      cout << "\n\n     Enter square size.  Zero exits: ";
      cin >> ssqq;
      if(ssqq == 0) goto skippy;
      c_box my_box;
      cout << "Size of sq" << ssqq << ": " <<  ssqq << endl;
      my_box.sq();
      goto do_it_again;
skippy:;
      }


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question