| View previous topic :: View next topic |
| Author |
Message |
mydogjax Master Cheater
Reputation: 0
Joined: 11 Aug 2007 Posts: 314
|
Posted: Fri Apr 10, 2009 9:09 pm Post subject: My C++ code |
|
|
How does this look for my first code?
| Code: | #include <iostream>
using namespace std;
int main()
{
int thisisanumber;
cout<<"Pick a number between 1 and 10: ";
cin>> thisisanumber;
cin.ignore();
cout<<"You entered: "<< thisisanumber <<"\n";
if (thisisanumber == 5)
cout<<"Ha ha, you guessed correctly!";
else if (thisisanumber < 5)
cout<<"Too low, nub. Try again";
if (thisisanumber > 5)
cout<<"too high!";
cin.get();
}
|
_________________
i loled hard at this
 |
|
| Back to top |
|
 |
talkerzero Grandmaster Cheater
Reputation: 1
Joined: 24 Jul 2008 Posts: 560 Location: California
|
Posted: Fri Apr 10, 2009 10:04 pm Post subject: |
|
|
1. You may want it to loop. If it's going to end ri ght away then telling them that they guessed the wrong number won't help them (well in this case it will, because the correct number is always 5, but they don't know that). You can loop using while or for, but if it's your first code you may not have gotten as far as loops yet.
2. | Code: | cin>> thisisanumber;
cin.ignore(); |
Since you already have cin>> thisisanumber there is no need for the cin.ignore.
3. You should always return a value. (with return)
|
|
| Back to top |
|
 |
VolatileAce Cheater
Reputation: 0
Joined: 19 Mar 2009 Posts: 30 Location: 0001001100110111
|
Posted: Fri Apr 10, 2009 10:07 pm Post subject: |
|
|
Pretty good.
You forgot to put 'return 0;'.
Its better to use one or two word variable names then this-is-a-number.
You should put a else before ' if (thisisanumber > 5) ';[/code]
|
|
| Back to top |
|
 |
talkerzero Grandmaster Cheater
Reputation: 1
Joined: 24 Jul 2008 Posts: 560 Location: California
|
Posted: Fri Apr 10, 2009 10:31 pm Post subject: |
|
|
| Code: | #include <iostream>
using namespace std;
int main()
{
int nVariable = 0;
for(;;) //infinite loop
{
cout << "Pick a number between 1 and 10: ";
cin >> nVariable;
cout << "You entered " << nVariable << "\n";
if(nVariable == 5)
cout << "Ha ha, you guessed correctly!";
else if(nVariable < 5)
cout << "Too loo, nub. Try again";
else if(nVariable > 5)
cout << "too high!";
cout << "\n"
}
return EXIT_SUCCESS;
} |
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Fri Apr 10, 2009 11:12 pm Post subject: |
|
|
| Code: | #include <iostream>
using namespace std;
int main()
{
int nVariable = 0;
cout << "Pick a number between 1 and 10: ";
for(;;) //infinite loop
{
cin >> nVariable;
cout << "You entered " << nVariable << ".\n";
if(nVariable == 5)
cout << "Ha ha, you guessed correctly! \n";
else if(nVariable < 5)
cout << "Too loo, nub. Try again \n";
else if(nVariable > 5)
cout << "Too high! Try again \n";
}
return EXIT_SUCCESS; // same as return 0;
}
|
Just a minor conceptual change to talker0's
|
|
| Back to top |
|
 |
VolatileAce Cheater
Reputation: 0
Joined: 19 Mar 2009 Posts: 30 Location: 0001001100110111
|
Posted: Sat Apr 11, 2009 12:06 am Post subject: |
|
|
| manc wrote: | | Code: | #include <iostream>
using namespace std;
int main()
{
int nVariable = 0;
cout << "Pick a number between 1 and 10: ";
for(;;) //infinite loop
{
cin >> nVariable;
cout << "You entered " << nVariable << ".\n";
if(nVariable == 5)
cout << "Ha ha, you guessed correctly! \n";
else if(nVariable < 5)
cout << "Too loo, nub. Try again \n";
else if(nVariable > 5)
cout << "Too high! Try again \n";
}
return EXIT_SUCCESS; // same as return 0;
}
|
Just a minor conceptual change to talker0's |
You don't need to initialize nVariable for more optimized code.
Also weren't you suppose to exit the loop once you get 5?
| Code: | void main(){
int i;
cout << "Pick a number between 1 and 10: " << endl;
do{
cin >> i;
cout << "You entered " << i << "." << endl << (i - 5 ? i < 5 ? "Too low, try again:" : "Too high, try again:" : "Ha ha, you guessed correctly!") << endl;
}while(i^5);
} |
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Sat Apr 11, 2009 12:42 am Post subject: |
|
|
holy shit
thats badass
mind explaining real quick?
| Code: | cout << (i - 5 ? i < 5 ? "Too low, try again:" : "Too high, try again:" : "Ha ha, you guessed correctly!");
|
I didnt know you could do that three ways, i had only known of true or false.
What are you three statements from
sorry kinda burnt right now
how do you say this condition?
while i is ??? to 5 ?
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sat Apr 11, 2009 1:11 am Post subject: |
|
|
Well, since we are having a contest to see who can write the best code, I decided to take it in another direction. Mine is invariably the best because the main code section resembles a pokeball*. At the end it then prints itself out in recognition of what I have done.
| Code: | #include <ctime>
#include <cstdio>
#include <iostream>
char *lol[] = { \
" using std::cin;", \
" int main(){int t=time(0);", \
" ::srand(t);int a,i,r=(::rand()%10)+1; ", \
" std::cout<<\"Pick a number between 1 and 1\";", \
" std::cout << \"0\\n\";do{std::cin>>a;std::cout<<\"Y\";", \
" std::cout << \"ou entered \" << a << \".\" <<std::endl; ", \
" if(a==r){std::cout<<\"Ha ha, you guessed correctly!\\n\";", \
" }else if(a < r){std::cout <<\"Too low, idiot. You \" ;", \
" //////////////////////// /////////////////////////", \
" std::cout<<\"wh\";std::cout << \"ited out! =(\" << \"\\n\"; ", \
" } else if(a>r){ std::cout <<\"T\"; std::cout << \"oo h\";", \
" std::cout << \"igh! You whi\"; std::cout << \"ted o\";", \
" std::cout << \"ut!\";std::cout << \"=(\" << \"\\n\";}}", \
" while(a!=r);for(int i=0;i<11+1+1+1+1+1;i++){", \
" std::cout << lol[i] << std::endl; }", \
" cin.sync();std::cin.ignore(); ", \
" return EXIT_SUCCESS; }" };
using std::cin;
int main(){int t=time(0);
::srand(t);int a,i,r=(::rand()%10)+1;
std::cout<< "Pick a number between 1 and 1";
std::cout << "0\n";do{std::cin>>a;std::cout<<"Y";
std::cout << "ou entered " << a << "." <<std::endl;
if(a==r){std::cout<<"Ha ha, you guessed correctly!\n";
}else if(a < r){std::cout <<"Too low, idiot. You " ;
//////////////////////// /////////////////////////
std::cout<<"wh";std::cout << "ited out! =(" << "\n";
} else if(a>r){ std::cout <<"T"; std::cout << "oo h";
std::cout << "igh! You whi"; std::cout << "ted o";
std::cout << "ut!";std::cout << "=(" << "\n";}}
while(a !=r);for(i=0; i<11+1+1+1+1+1; i++){
std::cout << lol[i] << std::endl; }
cin.sync();std::cin.ignore();
return EXIT_SUCCESS; } |
*best viewed in size 11 consolas.
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Sat Apr 11, 2009 1:28 am Post subject: |
|
|
You give me a reason to browse this board
|
|
| Back to top |
|
 |
VolatileAce Cheater
Reputation: 0
Joined: 19 Mar 2009 Posts: 30 Location: 0001001100110111
|
Posted: Sat Apr 11, 2009 6:01 am Post subject: |
|
|
@Flyte: Win, lmao.
@manc | Code: | cout << (i - 5 ? i < 5 ? "Too low, try again:" : "Too high, try again:" : "Ha ha, you guessed correctly!");
|
In C++, any non-zero value is considered "true".
The ? : is a ternary operator, basically similar to a shortened if else that returns some sort of value.
(Note: The i - 5.. just to make it look pretty, i == 5 will produce faster code)
Example:
| Code: | | int i = x < y ? x : y; |
That would assign the smaller of the value (x and y) to i.
While i Xor 5.
Just using Xor's basic properties. Any value Xor'ing itself will return 0.
Xor'ing should be faster then cmp. (I could be wrong)
Hope this helps.
|
|
| Back to top |
|
 |
->:Key: Advanced Cheater
Reputation: 0
Joined: 27 Sep 2008 Posts: 74 Location: Information Not For Disclosure
|
Posted: Sat Apr 11, 2009 7:32 am Post subject: |
|
|
o: I has Bordom
| Code: |
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
void main()
{
int rNum;
srand((unsigned)time(0));
rNum = (rand()%100)+1;
cout << "Think Of a Number" << endl;
cin.get();
system("CLS");
cout << "Now Times It By 2" << endl;
cin.get();
system("CLS");
cout << "Now Add: " << rNum << endl;
cin.get();
system("CLS");
cout << "Now Divide It By 2" << endl;
cin.get();
system("CLS");
cout << "Minus The Number You Though Up" << endl;
cin.get();
system("CLS");
rNum /= 2;
cout << "The Number You are Thinking Of is: " << rNum << endl;
cin.get();
}
|
Cred - Gramps for trick
me for somthing
|
|
| Back to top |
|
 |
mydogjax Master Cheater
Reputation: 0
Joined: 11 Aug 2007 Posts: 314
|
Posted: Sat Apr 11, 2009 9:02 am Post subject: |
|
|
This thread has gone beyond my knowledge of C++
_________________
i loled hard at this
 |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Sat Apr 11, 2009 9:10 am Post subject: |
|
|
| mydogjax wrote: | This thread has gone beyond my knowledge of C++  |
Ask, and you shall recieve
|
|
| Back to top |
|
 |
mydogjax Master Cheater
Reputation: 0
Joined: 11 Aug 2007 Posts: 314
|
Posted: Sat Apr 11, 2009 9:37 am Post subject: |
|
|
Ok, so if how do i make it loop? Whats the syntax for it?
_________________
i loled hard at this
 |
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Sat Apr 11, 2009 9:55 am Post subject: |
|
|
| mydogjax wrote: | | Ok, so if how do i make it loop? Whats the syntax for it? |
You could use for, while, or goto. That's all I know of
|
|
| Back to top |
|
 |
|