View previous topic :: View next topic |
Author |
Message |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Jun 28, 2007 8:22 pm Post subject: worthless codes |
|
|
Well, I am reading "Teach Yourself C++ in 21 Days" and you know how after week 1 they have that big source code for the rectangle? Well, I typed it all in exactly the same, and got a whole bunch of errors. And then I thought, I will shorten it. So I did this.
Code: |
#include <iostream>
using namespace std;
int main()
{
int choice;
int fQuit = false;
int i = 0;
int j = 0;
int height = 30;
int width = 5;
int newHeight;
int newWidth;
cout<<" *** Menu *** "<<endl;
cout<<"(1) Draw Rectangle "<<endl;
cout<<"(2) Area "<<endl;
cout<<"(3) Perimeter "<<endl;
cout<<"(4) Resize "<<endl;
cout<<"(5) Quit "<<endl;
cin>>choice;
switch (choice)
{
case 1:
for (;i<height;i++) {
for (;j<width;j++) {
cout<<"*"<<endl;
}
break;
case 2:
cout<<"Area: "<<height*width<<"\n";
break;
case 3:
cout<<"Perimeter: "<<(2*height)+(2*width)<<"\n";
break;
case 4:
cout<<"Height: \n";
cin>>newHeight;
height = newHeight;
cout<<"Width: \n";
cin>>newWidth;
width = newWidth;
break;
case 5:
fQuit = true;
cout<<"Exiting...\n\n";
break;
default:
cout<<"Error in choice!\n";
fQuit = true;
break;
}
return 0;
}
|
I only got one error. And it was:
Code: |
57 D:\Dev-Cpp\all cpp files\Rectangle.cpp expected `}' at end of input
|
Ok, so I thought why does the book have such a long code with those errors when you can make it really short and do just the same with only one error? Is it more efficient or something?
After you answer that question^^ my second question is: how do I fix that error? =(
Edit:
Don't flame my source please...
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
UnLmtD Grandmaster Cheater
Reputation: 0
Joined: 13 Mar 2007 Posts: 894 Location: Canada
|
Posted: Thu Jun 28, 2007 8:30 pm Post subject: |
|
|
You are missing an "}' . Just count them, in your source you got 4 "{"
and 3 "}". Means your missing a "}". As for why did you get a lot of errors, you need to be more specific, it may be your compiler, maybe you forgot to add a .lib in your link settings or something else .
_________________
Last edited by UnLmtD on Thu Jun 28, 2007 8:40 pm; edited 1 time in total |
|
Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Thu Jun 28, 2007 8:37 pm Post subject: |
|
|
Code: |
for (;i<height;i++) {
for (;j<width;j++) {
cout<<"*"<<endl;
}
|
corrected:
Code: |
for (;i<height;i++) {
for (;j<width;j++) {
cout<<"*"<<endl;
}
}
|
|
|
Back to top |
|
 |
Willy Master Cheater
Reputation: 0
Joined: 23 Apr 2007 Posts: 280 Location: Woodlands
|
Posted: Thu Jun 28, 2007 8:44 pm Post subject: |
|
|
Dev C++'s compiler sucks.
|
|
Back to top |
|
 |
Haruhi Suzumiya. Master Cheater
Reputation: 1
Joined: 05 Feb 2005 Posts: 463
|
Posted: Thu Jun 28, 2007 9:00 pm Post subject: |
|
|
Willy wrote: | Dev C++'s compiler sucks. | got any better compilers?
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 28, 2007 9:03 pm Post subject: |
|
|
The Visual C++ one. It's the best you'll get outside of the Intel one.
|
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Jun 29, 2007 9:50 am Post subject: |
|
|
Ok, I got it to run, thx appal. But now, the problem is it crashes, and I know why. Its because after the the choices, it breaks and then it automaticly returns 0 which closes. So what I need to do is make a while loop. But I don't know what the condition should be. Any suggestions?
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
UnLmtD Grandmaster Cheater
Reputation: 0
Joined: 13 Mar 2007 Posts: 894 Location: Canada
|
Posted: Fri Jun 29, 2007 10:24 am Post subject: |
|
|
It doesn't crash, it closes. Heres the code Code: | #include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
int main()
{
int choice;
int fQuit = false;
int i = 0;
int j = 0;
int height = 30;
int width = 5;
int newHeight;
int newWidth;
for(;;)
{
cout<<" *** Menu *** "<<endl;
cout<<"(1) Draw Rectangle "<<endl;
cout<<"(2) Area "<<endl;
cout<<"(3) Perimeter "<<endl;
cout<<"(4) Resize "<<endl;
cout<<"(5) Quit "<<endl;
cin>>choice;
switch (choice)
{
case 1:
for (;i<height;i++) {
for (;j<width;j++) {
cout<<"*"<<endl;
}
}
break;
case 2:
cout<<"Area: "<<height*width<<"\n";
break;
case 3:
cout<<"Perimeter: "<<(2*height)+(2*width)<<"\n";
break;
case 4:
cout<<"Height: ";
cin>>newHeight;
height = newHeight;
cout<<"Width: ";
cin>>newWidth;
width = newWidth;
break;
case 5:
fQuit = true;
cout<<"Exiting...\n\n";
ExitProcess(0);
break;
default:
cout<<"Error in choice!\n";
fQuit = true;
break;
}
}
return 0;
} |
The code that I posted uses a infinite loop by doing for( ; ; ) {} instead of while (0==0){} I'm not sure if an infinite loop is good in this case... but it works =) BTW I used ExitProcess() but a return 0; will do the job.
_________________
|
|
Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Fri Jun 29, 2007 10:40 am Post subject: |
|
|
You could simply use return 0 here.
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Jun 29, 2007 11:04 am Post subject: |
|
|
Ok, I made seome changes to it, and I got it to not close, crash, w/e. But now the only problem is, if you choose 1 (draw rectangle) it only draws the height of the rectangle, and not the width. Any suggestions? Btw, you guys still havn't answered my first question.
Code: |
#include <iostream>
using namespace std;
int main()
{
int choice;
int fQuit = false;
int i = 0;
int j = 0;
int height = 30;
int width = 5;
int newHeight;
int newWidth;
for(;;)
{
cout<<" *** Menu *** "<<endl;
cout<<"(1) Draw Rectangle "<<endl;
cout<<"(2) Area "<<endl;
cout<<"(3) Perimeter "<<endl;
cout<<"(4) Resize "<<endl;
cout<<"(5) Quit "<<endl;
cout<<"\n";
cin>>choice;
switch (choice)
{
case 1:
cout<<"\n";
for (;i<width;i++) {
for (;j<height;j++) {
cout<<"*"<<endl;
}
}
cout<<"\n";
break;
case 2:
cout<<"\n";
cout<<"Area: "<<height*width<<"\n";
cout<<"\n";
break;
case 3:
cout<<"\n";
cout<<"Perimeter: "<<(2*height)+(2*width)<<"\n";
cout<<"\n";
break;
case 4:
cout<<"\n";
cout<<"Height: ";
cin>>newHeight;
height = newHeight;
cout<<"Width: ";
cin>>newWidth;
width = newWidth;
cout<<"\n";
break;
case 5:
cout<<"\n";
fQuit = true;
cout<<"Exiting...\n\n";
break;
default:
cout<<"\n";
cout<<"Error in choice!\n";
fQuit = true;
cout<<"\n";
break;
}
}
return 0;
}
|
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
UnLmtD Grandmaster Cheater
Reputation: 0
Joined: 13 Mar 2007 Posts: 894 Location: Canada
|
Posted: Fri Jun 29, 2007 11:24 am Post subject: |
|
|
Quote: | Ok, so I thought why does the book have such a long code with those errors when you can make it really short and do just the same with only one error? Is it more efficient or something?
|
Well for the errors I already replied, and if you want us to answer if it is more efficient we got to see the original code. (Well at least I do )
_________________
|
|
Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Fri Jun 29, 2007 11:33 am Post subject: |
|
|
You have to reset your counters every time you enter the for-loop. That's what the first line in a for-loop is for... Also, the width and height need to be switched for them to mean what your variable names them. Lastly, you should print a line, then print a return, not print a return with every star.
ie:
Change:
Code: |
for (;i<width;i++) {
for (;j<height;j++) {
cout<<"*"<<endl;
}
}
|
to:
Code: |
for (i = 0;i<height;i++) {
for (j = 0;j<width;j++) {
cout<<"*";
}
cout<<"\n";
}
|
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Jun 29, 2007 11:45 am Post subject: |
|
|
Ok, I finally finished. Thank you delta. I now understand what I did wrong. Now I will release the finished file!
Rectangle Source Code:
Code: |
#include <iostream>
using namespace std;
int main()
{
int choice;
int i = 0;
int j = 0;
int height = 30;
int width = 5;
int newHeight;
int newWidth;
for(;;)
{
cout<<" *** Menu *** "<<endl;
cout<<"(1) Draw Rectangle "<<endl;
cout<<"(2) Area "<<endl;
cout<<"(3) Perimeter "<<endl;
cout<<"(4) Resize "<<endl;
cout<<"(5) Quit "<<endl;
cout<<"\n";
cin>>choice;
switch (choice)
{
case 1:
cout<<"\n";
for (i = 0;i<width;i++) {
for (j = 0;j<height;j++) {
cout<<"*";
}
cout<<"\n";
}
cout<<"\n";
break;
case 2:
cout<<"\n";
cout<<"Area: "<<height*width<<"\n";
cout<<"\n";
break;
case 3:
cout<<"\n";
cout<<"Perimeter: "<<(2*height)+(2*width)<<"\n";
cout<<"\n";
break;
case 4:
cout<<"\n";
cout<<"Height: ";
cin>>newHeight;
height = newHeight;
cout<<"Width: ";
cin>>newWidth;
width = newWidth;
cout<<"\n";
break;
case 5:
cout<<"\n";
cout<<"Exiting...\n\n";
return 0;
default:
cout<<"\n";
cout<<"Error in choice!\n";
cout<<"\n";
break;
}
}
return 0;
}
|
Edit:
@zomgiownyou
Have you read "Teach Yourself C++ in 21 Days" or do you have it? If you do, go to Week 1 Review and look at the source code. I will type it out if you don't, its just really long (as stated).
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
Simsgy Grandmaster Cheater
Reputation: 0
Joined: 07 May 2007 Posts: 581 Location: My new avatar <3
|
Posted: Sun Jul 01, 2007 4:03 am Post subject: |
|
|
Wow C++ looks weird and hard to program in.
I prefer Delphi.
_________________
|
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Jul 01, 2007 7:06 am Post subject: |
|
|
It was actually that I was trying to reduce a code a lot and I had some problems along the way I couldn't figure out. But yea, I would say delphi is easier.
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
|