Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 02-20-08   #11 (permalink)
Mmmm! Toast!
 
hometoast's Avatar
 
intel nvidia

Join Date: Sep 2007
Location: Pennsylvania
Posts: 1,270

Rep: 97 hometoast is acknowledged by some
Unique Rep: 80
Folding Team Rank: 335
Trader Rating: 8
Default

Quote:
Originally Posted by rabidgnome229 View Post
I would suggest what bartender posted rather than calling main again. Your way recursively calls main, which wastes memory. The program is trivial so it doesn't really matter, but bartender's post is better coding practice
most definitely! If it was something to run 100's ot 1000's of times you'd get a Stack Overflow error. Use
Code:
int main(int argc, char **argv) {

   do {
        /* your program */
   } while (again);

   return 0;
}

System: Fishtoast
CPU
e6750 @ 3.2Ghz
Motherboard
DFI Blood Iron
Memory
4x1G GSkill HZs @960
Graphics Card
eVGA 8800GT 512 @675/950
Hard Drive
2xWD 320 AAKS Raid0
Sound Card
on-board. (linux!=xfi)
Power Supply
PP&C 610W Silencer
Case
Modded Rocketfish
CPU cooling
Apogee GTX Coppertop
GPU cooling
Dtek GFX2+Unisink
OS
Vista Ultimate 64
Monitor
Samsung 204BW
hometoast is offline I fold for Overclock.net Overclocked Account   Reply With Quote
Old 02-20-08   #12 (permalink)
Overclocker
 
A Legend of Sorts's Avatar
 
amd ati

Join Date: Jul 2005
Location: Buffalo, NY
Posts: 235

Rep: 10 A Legend of Sorts Unknown
Unique Rep: 8
Trader Rating: 0
Default

Quote:
Originally Posted by hometoast View Post
most definitely! If it was something to run 100's ot 1000's of times you'd get a Stack Overflow error. Use
Code:
int main(int argc, char **argv) {

   do {
        /* your program */
   } while (again);
 
    return 0;
}
bah, while(condition){code} > do{code}while(condition);
Having to scroll to the bottom of a while loop to see the condition is annoying.
Also why do you keep a bracket on the same line as int main()? It makes it harder to count brackets when your compiler gets angry with you for having too many or too few end brackets.

What I am doing in a program that I have been working for the past year is:
Code:
while (answer != 0)
      {
	    cout<<" MAIN MENU                           n";
	    cout<<" quit                              0 n";
		cout<<" Read in raw to input              1 n";
		cout<<" Read in 3-D raw to input          2 n";
		cout<<" Write input image to file         3 n";
		cout<<" Write 3-D output image to file    4 n";
		cout<<" Write output image to file        5 n";
		cout<<" Zoom image with pixel replication 6 n";
		cout<<" Create a histogram                7 n";
		cout<<" Do Linear Interpolation           8 n";
		cout<<" Do Bilinear Interpolation         9 n";
		cout<<" Do Trilinear Interpolation        10n";
		cout<<" Generate circle contrast detail   11n";
		cout<<" Generate square contrast detail   12n";
		cout<<" Add noise to the image            13n";	
		cout<<" Use a convolution filter          14n";
		cout<<" Put processed image into input    15n";
		cout<<" Shrink Image                      16n";
		cout<<" Use Image calculator              17n";
		cout<<" Median Filter                     18n";
		cout<<" Average Filter                    19n";
		cout<<" Unsharp Mask                      20n";
		cout<<" Bit Reduction                     21n";
		cout<<" Generate hidden image(raw array)  22n";
		//cout<<" Fast Fourier Transform            23n";
		cout<<" Generate spoke image              24n";
		cout<<" Generate line pair image          25n";
		cout<<" Use Tile filter                   26n";
		cout<<" Read in Bitmap                    27n";
		cout<<" Change Characteristic Curve       28n";

               if (1 == answer)
              {
                    func(arguments);
              }
               else if (2 == answer)
              {
                    func(arguments);
              }
//etc....
}
EDIT: It removed my slashes.....
__________________
My next computer (Silent OCer)

System: My System
CPU
AMD 3700+ San Diego-CABHE
Motherboard
MSI K8N Neo4 Plat.
Sound Card
Creative Audigy 2 ZS
Power Supply
Antec SmartPower2.0 500w
Case
Antec p-180
OS
Window's XP Pro SP 2
Monitor
Samsung Syncmaster 950p

Last edited by A Legend of Sorts : 02-20-08 at 05:04 PM.
A Legend of Sorts is offline   Reply With Quote
Old 02-20-08   #13 (permalink)
Every base is base 10
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,009
Blog Entries: 1

Rep: 566 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

Quote:
Originally Posted by A Legend of Sorts View Post
bah, while(condition){code} > do{code}while(condtion);
Having to scroll to the bottom of a while loop to see the condition is annoying.
Also why do you keep a bracket on the same line as int main()? It makes it harder to count brackets when your compiler gets angry with you for having to many or too few end brackets.
Technically a do-while is more appropriate because the loop necessarily executes once. Either way, the compiler probably converts it to a do-while.

The bracket thing is just style. Keeping it on the same line gives a more compact look.
__________________
BIG BROTHER
Apple doesn't love you

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 02-20-08   #14 (permalink)
Mmmm! Toast!
 
hometoast's Avatar
 
intel nvidia

Join Date: Sep 2007
Location: Pennsylvania
Posts: 1,270

Rep: 97 hometoast is acknowledged by some
Unique Rep: 80
Folding Team Rank: 335
Trader Rating: 8
Default

[quote=A Legend of Sorts;3425532]bah, while(condition){code} > do{code}while(condition);
Having to scroll to the bottom of a while loop to see the condition is annoying.
Also why do you keep a bracket on the same line as int main()? It makes it harder to count brackets when your compiler gets angry with you for having too many or too few end brackets.
QUOTE]

do..while does it once without the initial test or "fooling" the while into thinking something already happened.

I put my braces there because that's they way I've done it since 1990.

System: Fishtoast
CPU
e6750 @ 3.2Ghz
Motherboard
DFI Blood Iron
Memory
4x1G GSkill HZs @960
Graphics Card
eVGA 8800GT 512 @675/950
Hard Drive
2xWD 320 AAKS Raid0
Sound Card
on-board. (linux!=xfi)
Power Supply
PP&C 610W Silencer
Case
Modded Rocketfish
CPU cooling
Apogee GTX Coppertop
GPU cooling
Dtek GFX2+Unisink
OS
Vista Ultimate 64
Monitor
Samsung 204BW
hometoast is offline I fold for Overclock.net Overclocked Account   Reply With Quote
Old 02-21-08   #15 (permalink)
AMD Overclocker
 
Starholdest's Avatar
 
amd nvidia

Join Date: Mar 2007
Location: █♣█
Posts: 939
Blog Entries: 1

Rep: 43 Starholdest is acknowledged by some
Unique Rep: 38
Hardware Reviews: 1
Trader Rating: 0
Default

Quote:
Originally Posted by hometoast View Post
most definitely! If it was something to run 100's ot 1000's of times you'd get a Stack Overflow error. Use
Code:
int main(int argc, char **argv) {

   do {
        /* your program */
   } while (again);

   return 0;
}
is while(again) right or do I need to change that to something else? Sorry but I'm really a beginner when it comes to C++
__________________

Volkswagen Enthusiasts of OCN


Aumotocnic "An unfortunate member of the overclock.net insomnia club"

Quebec Overclockers - 8019 in 3dMark06

s939 Manny 4600+ @ 2809Mhz @ 1.425V (9 hours Orthos blend test stable - 24/7 Usage) - http://valid.x86-secret.com/show_oc.php?id=289741
@ 2945Mhz @ 1.45V - http://valid.x86-secret.com/show_oc.php?id=276669

System: Beast from the East
CPU
AMD x2 4600+ @ 2.8Ghz
Motherboard
Asus A8N-SLI SE
Memory
2GB Kingston Value DDR400
Graphics Card
Asus 8800GTS 320MB
Hard Drive
80G Seagate SATA
Sound Card
SoundBlaster Audigy SE
Power Supply
Thermaltake Purepower 500W
Case
Antec 900 (modded)
CPU cooling
AS5 + Zalman CNPS9700 LED
GPU cooling
Stock
OS
Windows Vista Ultimate
Monitor
Samsung Syncmaster 730B (17" yay!)
Starholdest is offline Starholdest's Gallery   Reply With Quote
Old 02-21-08   #16 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,338

Rep: 306 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 234
Folding Team Rank: 448
Hardware Reviews: 1
Trader Rating: 3
Default

Quote:
Originally Posted by Starholdest View Post
is while(again) right or do I need to change that to something else? Sorry but I'm really a beginner when it comes to C++
What goes inside the parenthesis is a logical condition that evaluates to true when you want the while statement to loop. As an example:

int counter = 0;

while(counter <= 4)
{
++counter;
}

In this code example the while loop will keep incrementing counter by 1 as long as counter is less than or equal to 4. So this loop will run 5 times and counter will be equal to 5 at the end of this loop. (it loops one more time when counter is 4 because our loop condition is still true at 4)
__________________

A rocket powered land shark attached to a giant freakin' laser beam.
Congratulations! You have found the secret text! You get a cookie.

System: My System
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD
The Bartender Paradox is offline I fold for Overclock.net Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Old 02-21-08   #17 (permalink)
AMD Overclocker
 
Starholdest's Avatar
 
amd nvidia

Join Date: Mar 2007
Location: █♣█
Posts: 939
Blog Entries: 1

Rep: 43 Starholdest is acknowledged by some
Unique Rep: 38
Hardware Reviews: 1
Trader Rating: 0
Default

Quote:
Originally Posted by The Bartender Paradox View Post
What goes inside the parenthesis is a logical condition that evaluates to true when you want the while statement to loop. As an example:

int counter = 0;

while(counter <= 4)
{
++counter;
}

In this code example the while loop will keep incrementing counter by 1 as long as counter is less than or equal to 4. So this loop will run 5 times and counter will be equal to 5 at the end of this loop. (it loops one more time when counter is 4 because our loop condition is still true at 4)
My code is as follows:

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

float convert_money(float, float, char);
char quit;
float money;
char tax_option;
float tax_number;
bool restart();
float var_return[4];
    
int main ()   
{
    cout << "How much money do you want to convert?: ";
    cin >> money;
    cout << "Do you want to calculate tax? (y/n): ";
    cin >> tax_option;
    
    if(tax_option == 'y')
    {
           cout << "What % is the tax?: ";
           cin >> tax_number;  
    }

    convert_money(money, tax_number, tax_option);
    
    cout << "nnDo you want to restart? (y/n): ";
    cin >> quit;
    
    if(quit == 'y')
    {
            return 0;
    }
    else
    {
            system("cls");
            main();
    }
}

float convert_money (float a, float b, char c) 
{                   
      var_return[0] = (a * 0.984348);
      var_return[1] = (a / 0.984348);
   
           
      cout << "nConvert:n" << money << " CAD = " << var_return[0] << " US";
      cout << "n" << money << " US = " << var_return[1] << " CAD";
      
      if(c == 'y') 
      {
           var_return[2] = ((a * (b / 100)) + a);  
           cout << "nnTax calculation with " << b << "% tax:n";
           cout << money << " = " << var_return[2];
      }
}
I know it's probably crappy compared to some of your guys' standards, but I'm just starting. I need it so that it will only restart if quit == 'y' or 'yes'. BTW, I tried doing:

Code:
if(tax_option == 'y' || tax_option == 'yes')
and I get this error:

Quote:
[Warning] multi-character character constant
In function `int main()':
[Warning] comparison is always false due to limited range of data type
Thanks for all your help guys,
Elliott
__________________

Volkswagen Enthusiasts of OCN


Aumotocnic "An unfortunate member of the overclock.net insomnia club"

Quebec Overclockers - 8019 in 3dMark06

s939 Manny 4600+ @ 2809Mhz @ 1.425V (9 hours Orthos blend test stable - 24/7 Usage) - http://valid.x86-secret.com/show_oc.php?id=289741
@ 2945Mhz @ 1.45V - http://valid.x86-secret.com/show_oc.php?id=276669

System: Beast from the East
CPU
AMD x2 4600+ @ 2.8Ghz
Motherboard
Asus A8N-SLI SE
Memory
2GB Kingston Value DDR400
Graphics Card
Asus 8800GTS 320MB
Hard Drive
80G Seagate SATA
Sound Card
SoundBlaster Audigy SE
Power Supply
Thermaltake Purepower 500W
Case
Antec 900 (modded)
CPU cooling
AS5 + Zalman CNPS9700 LED
GPU cooling
Stock
OS
Windows Vista Ultimate
Monitor
Samsung Syncmaster 730B (17" yay!)
Starholdest is offline Starholdest's Gallery   Reply With Quote
Old 02-21-08   #18 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,338

Rep: 306 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 234
Folding Team Rank: 448
Hardware Reviews: 1
Trader Rating: 3
Default

A char type variable only stores one character. When you try to check tax_option is equal to 'yes' the compiler gets confused because tax_option should only be one character and you tried to compare it to three. There is other ways to get it to read the whole 'yes', but it probably will be easier just to have it check for y, or possibly for Y as well.
__________________

A rocket powered land shark attached to a giant freakin' laser beam.
Congratulations! You have found the secret text! You get a cookie.

System: My System
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD
The Bartender Paradox is offline I fold for Overclock.net Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Old 02-22-08   #19 (permalink)
AMD Overclocker
 
Starholdest's Avatar
 
amd nvidia

Join Date: Mar 2007
Location: █♣█
Posts: 939
Blog Entries: 1

Rep: 43 Starholdest is acknowledged by some
Unique Rep: 38
Hardware Reviews: 1
Trader Rating: 0
Default

Quote:
Originally Posted by The Bartender Paradox View Post
A char type variable only stores one character. When you try to check tax_option is equal to 'yes' the compiler gets confused because tax_option should only be one character and you tried to compare it to three. There is other ways to get it to read the whole 'yes', but it probably will be easier just to have it check for y, or possibly for Y as well.
Yea but if someone that isn't that great with this kind of stuff types yes instead of y...then I'd also like it to check for that too. Is there a data type that supports more than one character?
__________________

Volkswagen Enthusiasts of OCN


Aumotocnic "An unfortunate member of the overclock.net insomnia club"

Quebec Overclockers - 8019 in 3dMark06

s939 Manny 4600+ @ 2809Mhz @ 1.425V (9 hours Orthos blend test stable - 24/7 Usage) - http://valid.x86-secret.com/show_oc.php?id=289741
@ 2945Mhz @ 1.45V - http://valid.x86-secret.com/show_oc.php?id=276669

System: Beast from the East
CPU
AMD x2 4600+ @ 2.8Ghz
Motherboard
Asus A8N-SLI SE
Memory
2GB Kingston Value DDR400
Graphics Card
Asus 8800GTS 320MB
Hard Drive
80G Seagate SATA
Sound Card
SoundBlaster Audigy SE
Power Supply
Thermaltake Purepower 500W
Case
Antec 900 (modded)
CPU cooling
AS5 + Zalman CNPS9700 LED
GPU cooling
Stock
OS
Windows Vista Ultimate
Monitor
Samsung Syncmaster 730B (17" yay!)
Starholdest is offline Starholdest's Gallery   Reply With Quote
Old 02-22-08   #20 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,338

Rep: 306 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 234
Folding Team Rank: 448
Hardware Reviews: 1
Trader Rating: 3
Default

Quote:
Originally Posted by Starholdest View Post
Yea but if someone that isn't that great with this kind of stuff types yes instead of y...then I'd also like it to check for that too. Is there a data type that supports more than one character?
You can do it with just a char. Yes starts with a y. When you cin >> tax_option it writes just the first letter of what ever was entered to tax_option. So if the input is yes or y tax_option will still get the value y. Of course if the user entered yes, the e and the s would still be in the input buffer. Since you are restarting the program it would probably be beneficial to clear the input buffer. You can do that by using cin.ignore(5, '\n') where the 5 indicates how many characters you would like to ignore, and the '\n' is alternatively what character you would like to stop on. So cin.ignore(5, '\n') says ignore the next five characters or up until the newline character, which ever comes first. Hope that helps
__________________

A rocket powered land shark attached to a giant freakin' laser beam.
Congratulations! You have found the secret text! You get a cookie.

System: My System
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD
The Bartender Paradox is offline I fold for Overclock.net Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 03:29 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.