| View previous topic :: View next topic |
| Author |
Message |
Mousai Expert Cheater
Reputation: 0
Joined: 24 Jul 2006 Posts: 119 Location: Irving, Texas
|
Posted: Wed May 14, 2008 11:55 pm Post subject: Mousai's C++ Help Thread |
|
|
So yeah I'm trying to learn some C++, and I haven't tried actually compiling anything, and I tend to forget some things so I started actually making everything I learn so I'm actually using it. Anyways, I try compiling this with DEV-C++(I know I should get VSC++, but I haven't yet so whatever.) and it comes out with like every error possible, lol. I'm learning from a book but it's rather old so that may be the problem.(doubt it) Anyways here's the source:
| Code: | #include <iostream.h>
class HotDogStand
{
Private:
int hotDogsOnHand;
int bunsOnHand;
Public:
void displayData()
{
cout << "\n Hotdogs: " << hotDogsOnHand;
cout << "\n Buns: " << bunsOnHand;
}
void soldOneDog()
{
--hotDogsOnHand;
--bunsOnHand;
}
void initializeData()
{
cout << "\nEnter hotdogs on hand: ";
cin >> hotDogsOnHand;
cout << "\nEnter buns on hand: ";
cin >> bunsOnHand;
}
};
////////////////////////////END OF CLASS///////////////////////////////////
void main()
{
char choice = 'X';
HotDogStand Stand1;
HotDogStand Stand2;
HotDogStand Stand3;
cout << "\nInitialize data for Stand one:";
Stand1.initializeData();
cout << "\nInitialize data for Stand two:";
Stand2.initializeData();
cout << "\nInitialize data for Stand three:";
Stand3.initializeData();
while(choice != 'q')
{
cout << "\nEnter stand number 1, 2, or 3, or q to quit.";
cin >> choice;
switch(choice)
{
case '1':
{
cout << "\nSelling a hotdog at stand one.";
Stand1.soldOneDog;
break;
case '2':
cout << "\nSelling a hotdog at stand two.";
Stand2.soldOneDog;
break;
case '3':
cout << "\nSelling a hotdog at stand three.";
Stand3.soldOneDog;
break;
}
};
cout << "\nSupplies on hand at Stand one:";
Stand1.displayData;
cout << "\nSupplies on hand at Stand two:";
Stand2.displayData;
cout << "\nSupplies on hand at Stand three:";
Stand3.displayData;
} |
It's pretty much straight from the book, but gives like a thousand errors.
Can y'all correct any errors and help me out I'm still pretty new at C++ and don't know much but what's in the book.
Also it's just supposed to be a console application.
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Thu May 15, 2008 1:18 am Post subject: |
|
|
Fixed based on the above, I do not recommend the coding style of that book though:
| Code: |
#include <iostream>
class HotDogStand
{
private:
int hotDogsOnHand;
int bunsOnHand;
public:
void displayData()
{
std::cout << "\n Hotdogs: " << hotDogsOnHand;
std::cout << "\n Buns: " << bunsOnHand;
}
void soldOneDog()
{
--hotDogsOnHand;
--bunsOnHand;
}
void initializeData()
{
std::cout << "\nEnter hotdogs on hand: ";
std::cin >> hotDogsOnHand;
std::cout << "\nEnter buns on hand: ";
std::cin >> bunsOnHand;
}
};
////////////////////////////END OF CLASS///////////////////////////////////
void main()
{
char choice = 'X';
HotDogStand Stand1;
HotDogStand Stand2;
HotDogStand Stand3;
std::cout << "\nInitialize data for Stand one:";
Stand1.initializeData();
std::cout << "\nInitialize data for Stand two:";
Stand2.initializeData();
std::cout << "\nInitialize data for Stand three:";
Stand3.initializeData();
while(choice != 'q')
{
std::cout << "\nEnter stand number 1, 2, or 3, or q to quit.";
std::cin >> choice;
switch(choice)
{
case '1':
std::cout << "\nSelling a hotdog at stand one.";
Stand1.soldOneDog();
break;
case '2':
std::cout << "\nSelling a hotdog at stand two.";
Stand2.soldOneDog();
break;
case '3':
std::cout << "\nSelling a hotdog at stand three.";
Stand3.soldOneDog();
break;
}
}
std::cout << "\nSupplies on hand at Stand one:";
Stand1.displayData();
std::cout << "\nSupplies on hand at Stand two:";
Stand2.displayData();
std::cout << "\nSupplies on hand at Stand three:";
Stand3.displayData();
}; |
_________________
- Retired. |
|
| Back to top |
|
 |
Mousai Expert Cheater
Reputation: 0
Joined: 24 Jul 2006 Posts: 119 Location: Irving, Texas
|
Posted: Thu May 15, 2008 3:58 am Post subject: |
|
|
What are the std::'s? What's it for\mean\do?
Also could you show me a better style of coding that then?
Or perhaps a better book to learn from, as I said this book is pretty old, though it is written by Robert Lafore.
Also, you fixed all the errors but one , main must return an integer.
I don't see why it would have to, maybe so when you press enter and the console closes?
_________________
|
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Thu May 15, 2008 4:33 am Post subject: |
|
|
If you need to return an integer, you have to put int before main() (if you want to return a boolean, put boolean before main() etc). Also, put a return at the end of main: (If i recall correctly returning zero means no errors occured)
| Code: |
int main(){
//
//Code...
//
return 0;
};
|
cout and cin are member functions of the std namespace. You could either put using namespace std; or something like that at the top, and use cout, or just use std::cout.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Thu May 15, 2008 4:52 am Post subject: |
|
|
| Mousai wrote: | What are the std::'s? What's it for\mean\do?
Also could you show me a better style of coding that then?
Or perhaps a better book to learn from, as I said this book is pretty old, though it is written by Robert Lafore.
Also, you fixed all the errors but one , main must return an integer.
I don't see why it would have to, maybe so when you press enter and the console closes? |
Like I said, I fixed it based on what it was, I didn't go through and correct what I thought would be better to do. Yes, int return on the main function does follow standards, but, like I said, that was not the purpose of what i posted. And that shouldn't error on compile as it is accepted to have the main function as void.
std:: is a namespace. For a detailed explanation check out:
http://www.cplusplus.com/doc/tutorial/namespaces.html
Cplusplus.com gives a general overall read of the basics for C++, I suggest you read through that site as it is a nice resource. I personally never read any books for C/C++ I taught myself based on web tutorials and trial and error. But, people learn in different methods, thats just how I learn. The book you are reading may be for a specific OS other then Windows, or might be outdated.
If you can, just look for newer books. You can find a lot of them for free around the net as an ebook as well as many sites that have tons of tutorials. Don't waste money unless you actually plan on reading a full book front to back.
_________________
- Retired. |
|
| Back to top |
|
 |
Mousai Expert Cheater
Reputation: 0
Joined: 24 Jul 2006 Posts: 119 Location: Irving, Texas
|
Posted: Thu May 15, 2008 4:57 am Post subject: |
|
|
Yee it worked, except for the last part it closed as soon as I choose to quit.
Thanks for that, I was about to check the console skeleton and compare them. Also I tried using header that I got from the skeleton, but it didn't work. I can see I need a new book, anyone got a good one to recommend before I go shopping? Lol
Edit: I tried using, using namespace std;
But for some reason it didn't work, also I've started on the tutorial there. I'll try this out before I buy a book.
_________________
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Thu May 15, 2008 5:27 am Post subject: |
|
|
cplusplus.com
Save some money
Anything you need you can find on the internet
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Thu May 15, 2008 6:54 am Post subject: |
|
|
Don't use 'using namespace std;' unless you plan to actually use the entire namespace which I can almost guarantee you never will. Just be unlazy and use std:: in front of the functions that are used from the namespace.
You can pause the console before the return by using:
| Code: | std::cin.sync();
std::cin.ignore(); |
_________________
- Retired. |
|
| Back to top |
|
 |
|