| View previous topic :: View next topic |
| Author |
Message |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Fri May 18, 2007 12:42 am Post subject: My programs i make while learning C++ thread |
|
|
Ok i will post up the programs and code I make while learning c++ so here is the first code it does simple multiplication problems. And has a check to see if you tried to put in a non postive number in which case it tells you and makes you close the program.
| Code: |
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
double d1;
double d2;
double dA;
cout << "Enter the first postive number you would like to multiply: ";
cin >> d1;
if (d1 > 0)
{
cout << "Enter the postive number you would like to multiply the first by: ";
cin >> d2;
if (d2 > 0)
{
dA = d1*d2;
cout << "The answer to " << d1 << " times " << d2 << " equals " << dA << endl;
system("PAUSE");
}
else
{
cout << "That is not a postive number." << endl;
system("PAUSE");
}
}
else
{
cout << "That is not a postive number." << endl;
system("PAUSE");
}
}
|
As always im open to any suggestions on how i could improve my code or what i did wrong.
And also this thread could help others learning c++ by looking at how my code works.
Edit: I'm adding onto this post with a revised verision of my multiplication program
| Code: |
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int formula()
{
double d1, d2,dA;
cout << "Enter the first postive number you would like to multiply: "; cin >> d1;
cout << "Enter the postive number you would like to multiply the first by: "; cin >> d2;
if(d1+d2 <= 0 )
{
cout << "This is not a postive number";
system("PAUSE");
}
else
{
dA = d1*d2;
return dA;
}
}
int main()
{
cout << "The answer to the problem is " << formula() << endl;
system("PAUSE");
}
|
_________________
Earthbound = 31337
Last edited by Losplagos on Fri May 18, 2007 6:42 pm; edited 2 times in total |
|
| Back to top |
|
 |
TheSorc3r3r I post too much
Reputation: 0
Joined: 06 Sep 2006 Posts: 2404
|
Posted: Fri May 18, 2007 4:38 am Post subject: |
|
|
You should have the user enter both, then do a check:
| Code: | cout << "\nEnter first :";
cin >> d1;
... // get user input for second
if (!(d1 & d2))
{
cout << "\nYou entered a negative number. Byebye!";
// return 1 (or a # greater than 1) instead of system("pause");
return 1;
}
cout << "\nAnswer is: " << d1*d2 << endl;
return 1; |
and you can declare multiple variables on the same line, like:
double d1, d2, d3;
you don't need any header except iostream
You should use \n or endl to keep everything off the same line. _________________
Don't laugh, I'm still learning photoshop! |
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Fri May 18, 2007 5:05 am Post subject: |
|
|
| TheSorc3r3r wrote: | You should have the user enter both, then do a check:
| Code: | cout << "\nEnter first :";
cin >> d1;
... // get user input for second
if (!(d1 & d2))
{
cout << "\nYou entered a negative number. Byebye!";
// return 1 (or a # greater than 1) instead of system("pause");
return 1;
}
cout << "\nAnswer is: " << d1*d2 << endl;
return 1; |
and you can declare multiple variables on the same line, like:
double d1, d2, d3;
you don't need any header except iostream
You should use \n or endl to keep everything off the same line. |
Ok thanks and i just learned about writing out the variables on one line like that. Also make it harder on myself using longer code so with all the common mistakes i make i can easly read the code and find out what i did wrong. But my project with arrays and functions is not quite working yet. Since i am having a little trouble with loops and the break; command. _________________
Earthbound = 31337 |
|
| Back to top |
|
 |
tigerite How do I cheat?
Reputation: 0
Joined: 16 May 2007 Posts: 9
|
Posted: Fri May 18, 2007 5:13 am Post subject: |
|
|
| I personally don't like nested if's like that where they can be avoided; they're poor programming practice, you should probably use a switch(). |
|
| Back to top |
|
 |
supea Master Cheater
Reputation: 0
Joined: 29 Aug 2006 Posts: 323 Location: Finland
|
Posted: Fri May 18, 2007 5:20 am Post subject: |
|
|
why do you do else two times?
Make check it do you put number. It does something crazy if you input letter... |
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Fri May 18, 2007 7:13 am Post subject: |
|
|
| tigerite wrote: | | I personally don't like nested if's like that where they can be avoided; they're poor programming practice, you should probably use a switch(). |
tigerite could explain how to use the switch() and also how it is implemented since i very vaquely know about the switch() command.
Actually this is all I know about switch() as a example
| Code: |
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int choice;
cout << "Enter 4 to exit 5 to do multiplication or 6 to add: ";
cin >> choice;
switch(choice)
{
case 4:
//end
system("PAUSE");
break;
case 5:
//multiplication
double d1, d2,d3;
cout << "What is the first number ";
cin >> d1;
cout << "What would you like to multiply the first number by ";
cin >> d2;
//doing the math
d3 = d1*d2;
//ending
cout << "The answer to " << d1 << " multiplied by " << d2 << " is " << d3
<< endl;
system("PAUSE");
break;
case 6:
int d4, d5, d6;
cout << "What is the first number: " << endl;
cin >> d4;
cout << "What is the number you would like to add to it" << endl;
cin >> d5;
d6= d4+d5;
cout << "The sum of " << d4 << " and " << d5 << " is " << d6 << endl;
system("PAUSE");
default:
int enter;
cout << "You did not enter 4, 5, or 6" << endl;
system("PAUSE");
break;
}
return 0;
}
|
Which should work correctly now that i corrected a minor lapse of my brain that is functioning on no sleep. _________________
Earthbound = 31337 |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Mon May 28, 2007 3:15 pm Post subject: |
|
|
Wtf. If your just trying to do positive numbers use this code that I made:
| Code: |
#include <iostream>
using namespace std;
int mult ( int x, int y );
int main()
{
int x;
int y;
cout<<"Please input one number:\n";
cin>> x;
cin.ignore();
cout<<"Please input another number:\n";
cin>> y;
cin.ignore();
cout<<"The product is "<< mult ( x, y ) <<".\n";
cin.get();
}
int mult ( int x, int y )
{
return x * y;
}
|
If it you want a decimal as well, then declare x and y as float _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Ouch! How do I cheat?
Reputation: 0
Joined: 09 Jan 2007 Posts: 0
|
Posted: Tue May 29, 2007 5:32 am Post subject: |
|
|
Wanna see a REAL program: [/sarcasm]
| Code: |
#include <iostream>
using namespace std;
int main()
{
//Introduction
cout << "Welcome to the Gay Test!\n\n";
//Enter Name
char f_NAME[20];
cout << "Please enter your first name: ";
cin >> f_NAME;
//Check Name For Mine
char c_NAME[20];
c_NAME = strupr(f_NAME);
bool take_TEST = true;
if(c_Name == "ZAED") { //My Name
cout << "\nDon't Kid Yourself " << f_NAME << ", theres no way you could be gay";
take_TEST = false; }
//Check Size of Name
int s_NAME = strlen(f_NAME);
if(s_NAME > 9) {
cout << "Holy shit dude, your name is " << (s_Name-1) << " characters long!\n\nAnyway, "; }
//Begin Test
if(take_TEST == true) {
cout << "It's time to take the test!\n\n";
char Answer;
Question:
cout << "Question 1: Are You Gay? (Answer 'y' or 'n'): ";
cin >> Answer;
//Check Answer
if(Answer == 'n') {
cout << "\nWhat! I can't believe your gay! You must gave answered incorrectly by accident, try again\n\n";
goto Question; }
else if(Answer == 'y') {
cout << "\nHaha " << f_NAME << ", Your Gay!"; }
else {
cout << "\nInvalid Answer, try again\n\n";
goto Question; }
}
cin.ignore();
return 0;
}
|
Ps- Not tried _________________
Haha, You Actually Bothered To Read This!
____________
  |
|
| Back to top |
|
 |
SF I'm a spammer
Reputation: 119
Joined: 19 Mar 2007 Posts: 6028
|
Posted: Tue May 29, 2007 1:27 pm Post subject: |
|
|
| I'd like to suggest tabbing stuff in, like the post above me. It makes it easier to read and understand. |
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Tue May 29, 2007 3:00 pm Post subject: |
|
|
| SaviourFamily wrote: | | I'd like to suggest tabbing stuff in, like the post above me. It makes it easier to read and understand. |
like the post above you? That code was horribly indented, not to mention it doesn't make the least of efforts to make the program readable or efficient (declaring variables all over the place, even when there is no need and a bunch of other stuff).
To properly indent the thing you were mentioning, it would look something like this:
| Code: | #include <iostream>;
using namespace std;
int main ()
{
//Introduction
cout << "Welcome to the Gay Test!\n\n";
//Enter Name
char f_NAME [20];
cout << "Please enter your first name: ";
cin >> f_NAME;
//Check Name For Mine
char c_NAME [20];
c_NAME = strupr (f_NAME);
bool take_TEST = true;
if (c_Name == "ZAED")
{ //My Name
cout << "\nDon't Kid Yourself " << f_NAME << ", theres no way you could be gay";
take_TEST = false;
}
//Check Size of Name
int s_NAME = strlen (f_NAME);
if (s_NAME > 9)
{
cout << "Holy shit dude, your name is " << (s_Name - 1) << " characters long!\n\nAnyway, ";
}
//Begin Test
if (take_TEST == true)
{
cout << "It's time to take the test!\n\n";
char Answer;
Question:
cout << "Question 1: Are You Gay? (Answer 'y' or 'n'): ";
cin >> Answer;
//Check Answer
if (Answer == 'n')
{
cout << "\nWhat! I can't believe your gay! You must gave answered incorrectly by accident, try again\n\n";
goto Question;
}
else if (Answer == 'y')
{
cout << "\nHaha " << f_NAME << ", Your Gay!";
}
else
{
cout << "\nInvalid Answer, try again\n\n";
goto Question;
}
}
cin.ignore ();
return 0;
} |
_________________
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 |
|
 |
Ouch! How do I cheat?
Reputation: 0
Joined: 09 Jan 2007 Posts: 0
|
Posted: Tue May 29, 2007 10:33 pm Post subject: |
|
|
it was a 10 second code, which i made up as i went along. Oh and tab does not work while posting (i wroted that in the code box), it skips to the [add attatchment] button. I had to keep pressing space. _________________
Haha, You Actually Bothered To Read This!
____________
  |
|
| Back to top |
|
 |
|