| View previous topic :: View next topic |
| Author |
Message |
Up2Admin I'm a spammer
Reputation: 126
Joined: 17 Oct 2007 Posts: 6548 Location: Texas
|
Posted: Sun Aug 02, 2009 11:11 pm Post subject: [C++] Practices [2] + Source |
|
|
Well, I'm getting real into C++, totally hooked. Just started today and I've come up with 2 crappy practice programs.
If anyone wishes to provide me with any help, I'm available on MSN.
[email protected]
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1. First Project
Source as Text
What's it do?
The user enters a number into the dialog and presses the enter key, then the dialog displays:
| Code: | | "20+*user number*=*sum of the integers* |
It then displays whether the sum is greater than, less than, or equal to 100.
The exe can be found in the Debug folder.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
2. Calculator
Source as Text
What's it do?
What do you think...
The exe can be found in the Debug folder.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: _________________
|
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Mon Aug 03, 2009 4:59 am Post subject: |
|
|
At the end of both your sources you do: "return main();" to restart the program. You shouldn't do that because after it has been run a lot of times this will cause a stack overflow. Instead, if you want your app to repeatedly do those actions you should put it in a loop like this:
| Code: | #include "stdafx.h"
#include <iostream>
using namespace std;
int main(void){
bool Exit = false;
while(Exit == false){
int a = 20;
int c;
int final;
//The variables are defined
cout << "Enter a number:";
cin >> c;
final = c+a;
//User input is created and the variable 'final' is assigned a new value
cout << a << "+" << c << "=" << c + a << "\n";
//Outputs: 20 plus the users input value and what it's equal to
if(final>100){
cout << final << " is more than 100" << "\n";
}else if(final==100){
cout << final << " is equal to 100" << "\n";
}else{
cout << final << " is less than 100" << "\n";
}
//Determines whether the final value is less than, equal to, or greater than 100
cout << "\n";
//Creates a new line
cout << "Enter 1 to rerun the program, or any other number to exit." << "\n";
cin >> final;
//Used the 'final' variable here since it's not used anymore so you don't have to make a new variable.
if( final != 1 ){
Exit = true;
//You can also use break; here to exit the loop
}
}
} |
|
|
| Back to top |
|
 |
Up2Admin I'm a spammer
Reputation: 126
Joined: 17 Oct 2007 Posts: 6548 Location: Texas
|
Posted: Mon Aug 03, 2009 12:16 pm Post subject: |
|
|
Thanks for the info! I'll use that method from now on  _________________
|
|
| Back to top |
|
 |
|