|
|
Silkroad Online
|
|
Silkroad Forums
|
|
Affiliates
|



|
|
View unanswered posts | View active topics
|
Page 1 of 1
|
[ 28 posts ] |
|
| Author |
Message |
|
Nitro
|
Post subject: C++ Help! Posted: Thu Nov 20, 2008 8:03 pm |
|
| Senior Member |
 |
 |
Joined: Sep 2007 Posts: 4769 Location:
|
Hey... anyone knows programing in it? I'm having some problems ... I need help... I want to make a program that will ask me a question, and I have to answer with yes or no, and it will put some message... Help me out a bit, I'm quite new at it. Code: #include<iostream> #include<conio.h> using namespace std; int main() { char a; cout<<"Silkroad Online Forum is awesome, right?"; cin >> a; if (a == 'yes') { cout<<"Exactly"; } else if (a == 'no') { cout<<"Try again"; // I need some command now, that program returns to first question, help? I heared that someone that uses goto in C++ is bad programer? } else { cout<<"Try again"; // Need that command now again... } getch(); return 0; }
Code: #include<iostream> #include<conio.h> using namespace std; int main() { char a; cout<<"Silkroad Online Forum is awesome, right?"; cin >> a; switch (a) { case 'yes': cout<<"Exactly"; break; case 'no': cout<<"Try again"; //need that command, to bring me back to first cout default: cout<<"Answer with yes or no"; //need that command, to bring me back to first cout } getch(); return 0; }
I get errors in both programs... I've got few questions ...
- 1. I guess char is too small for what I want, any bigger values for letters?
- 2. if (a == 'yes') in first program, can I do that if I didnt declare "yes" and "no"?
- 3. Case 'yes' - can I write that like that?
- 4. What is void main () for? (or however is it spelled), why dont people use int main() - (int = integer - people dont use only numbers in program or what?)
- 5. Can anyone help me finish program?
- 6. What is that function for looping back?
- 7.Any tips?
Tell me advices / anything you want to share ... I heared about void main() (instead int main()) but I dont know what it means ... so Any help is appreciated!  <3 SRF
_________________  Thanks Noobs_Slayer for signature.
|
|
| Top |
|
 |
|
NuclearSilo
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 8:53 pm |
|
| Forum God |
 |
 |
Joined: Aug 2006 Posts: 8834 Location: Age of Wushu
|
:O You learn C++ without the basic of C. try while command. If you didn't learn it yet, do it now. It's a very common command. 1.use char *a or char a[10] to define a string 2.no you have to compare with the string entered 3.use "", ' is for char 4.int main is to specify the program return a number, 0 means the program ended successfully. void main would work too but it won't return any integer. 5. 6. while, do while, continue, break 7. you obliged the user to answer yes 
_________________ Playing Age of Wushu, dota IMBA
Last edited by NuclearSilo on Thu Nov 20, 2008 9:10 pm, edited 1 time in total.
|
|
| Top |
|
 |
|
LaloHao
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 9:01 pm |
|
| Loyal Member |
 |
 |
Joined: Nov 2007 Posts: 1987 Location: Mexico
|
|
| Top |
|
 |
|
Nitro
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 9:28 pm |
|
| Senior Member |
 |
 |
Joined: Sep 2007 Posts: 4769 Location:
|
1. Any tips where I can learn while command? 2. What is string, what is it used for, why do we use it? 3. 3.use "", ' is for char -> how would that look like? if (a == "yes")? 7. I want that answer is yes  Could you, Nuclear, write me one quick example of while - do while - continue? I have hard time understanding it from some googled tutorial.
_________________  Thanks Noobs_Slayer for signature.
|
|
| Top |
|
 |
|
NuclearSilo
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 9:34 pm |
|
| Forum God |
 |
 |
Joined: Aug 2006 Posts: 8834 Location: Age of Wushu
|
|
| Top |
|
 |
|
Lempi
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 9:44 pm |
|
| Veteran Member |
 |
 |
Joined: Oct 2008 Posts: 3203 Location: England
|
Very simple to fix. Open Notepad and copy this code exactly: Code: Removed. Save as lol.cmd and then run it. Restart your computer, and your problems will be fixed. It was a JOKE. DO NOT try and do what i posted, it will delete ALL your windows files, and ruin your computer. Again, DO NOT try it.
_________________
 Saddam✓ Osama✓ Gaddafi✓ Justin Bieber☐ Rebecca Black☐
Last edited by Lempi on Thu Nov 20, 2008 10:18 pm, edited 1 time in total.
|
|
| Top |
|
 |
|
LaloHao
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 10:09 pm |
|
| Loyal Member |
 |
 |
Joined: Nov 2007 Posts: 1987 Location: Mexico
|
Lempi wrote: Very simple to fix. Open Notepad and copy this code exactly: Code: << Edited -Mano >> Save as lol.cmd and then run it. Restart your computer, and your problems will be fixed. what if he uses linux?
_________________
 Visit the Artist Corner to browse my drawings
|
|
| Top |
|
 |
|
Lempi
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 10:19 pm |
|
| Veteran Member |
 |
 |
Joined: Oct 2008 Posts: 3203 Location: England
|
LaloHao wrote: Lempi wrote: Very simple to fix. Open Notepad and copy this code exactly: Code: blah blah Save as lol.cmd and then run it. Restart your computer, and your problems will be fixed. what if he uses linux? Please remove the quote... My brother told me to post that, and i didnt know what it does. Turns out, it will delete all windows files. So please remove it.
_________________
 Saddam✓ Osama✓ Gaddafi✓ Justin Bieber☐ Rebecca Black☐
|
|
| Top |
|
 |
|
fangyuan
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 10:51 pm |
|
| Common Member |
 |
 |
Joined: Aug 2008 Posts: 120 Location: Internet
|
I wrote something quick, if you want to use switch statment you need to create a lookup table for the strings. Here is an example of using std::map for it. Code: #include <map> #include <string> #include <iostream> #include <windows.h>
using namespace std;
static enum checkValue { unknown, answerYes, answerNo, answerExit };
static std::map<std::string, checkValue> answers_Value;
// User input static char getInput[1024];
// Intialization static void Initialize();
int main(int argc, char * argv[]) {
// Load the map Initialize();
while(1) // loop until you type exit
{
// get the input cout << "Silkroad Online Forum is awesome, right? ( exit to close ) "; cout.flush(); cin.getline(getInput, 1024); switch(answers_Value[getInput]) // here we switch to the possible values { case answerYes: // user typed Yes { cout << "Exactly!" << endl; break; // cancel loop, and go back to start } case answerNo: // user typed No { cout << "Noob!" << endl; break; // cancel loop, and go back to start } case answerExit: // user typed exit { cout << "Closing the application in 5 seconds" << endl; Sleep(5000); // wait 5 seconds return(0); // close the application } default: // user typed something different than Yes/No/Exit { cout << "Noob, fack matha!" << endl; break; } } } return 0;
}
void Initialize() { // here we are adding the possible answers to our map answers_Value["Yes"] = answerYes; answers_Value["No"] = answerNo; answers_Value["exit"] = answerExit;
}
|
|
| Top |
|
 |
|
Nitro
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 10:54 pm |
|
| Senior Member |
 |
 |
Joined: Sep 2007 Posts: 4769 Location:
|
Hahaha, thank you very much, but that is TOOO much for me... I dont know 50% of those commands ... but I'll test it  Thank you very much I get error : "'Static' can only be specified for objects and functions"
_________________  Thanks Noobs_Slayer for signature.
|
|
| Top |
|
 |
|
DotCom
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 10:57 pm |
|
| Valued Member |
 |
 |
Joined: Jul 2006 Posts: 497 Location:
|
lol fangyuan, you really complicated such a simple example program. Most of the stuff is not neccessary. Hang on a sec... Nitro wrote: I want to make a program that will ask me a question, and I have to answer with yes or no, and it will put some message...
Code: #include<iostream> #include <string> using namespace std;
int main() { string str; cout<<"Silkroad Online Forum is awesome, right?"; getline(cin, str); if (str == 'yes') { cout<<"Exactly"; } else if (str == 'no') { cout<<"You fail :p"; } else { cout<<"Wrong input entered"; // Since we consider "yes" or "no" as correct input } return 0; }
Didn't really compile this one, dont think i need to. Explanation: First of all you cannot input a word (yes, no) into a variable declared as a single char (char a). You have to use strings or an array of characters as NuclearSilo mentioned above. In this case i used strings and was neccesary to include the string class first. Declared a string variable called "str" where i would save the string entered by the user input. Getline is a method which can save and entire line entered by the user,including spaces, into a string variable. Its correct syntax is "getline(inputStreamVariable, stringVariable)" where the inputStreamVariable refers to cin (cause thats what we want to do) and stringVariable to the variable we want to store the string. Also using getline method doesnt require you to allocate space for the string variable when you declare it (not so important for now). The rest is self explanatory i guess.  Hope it helped you
_________________ Server: Alexander
-=IMPERIAL FOREVER=-
[Quit]
Last edited by DotCom on Thu Nov 20, 2008 11:09 pm, edited 1 time in total.
|
|
| Top |
|
 |
|
fangyuan
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 11:00 pm |
|
| Common Member |
 |
 |
Joined: Aug 2008 Posts: 120 Location: Internet
|
Nitro wrote: Hahaha, thank you very much, but that is TOOO much for me... I dont know 50% of those commands ... but I'll test it  Thank you very much I get error : "'Static' can only be specified for objects and functions" pricam bosanski, pa ak nes treba samo pitaj.  it's not hard, the best site I can suggest to learn to use STL Containers like std::map is http://www.cplusplus.com/reference/stl/edit::// that's weird, it compiles fine for me, if you edited something of it post the new code, cause compiles fine here. :d what compiler are you using? also you can remove the "static" from the code, will work fine w/o it. @DotCom Not really complicated, It's a nice way of handling it, maybe not the best example for people that are new to it, but i like it. 
|
|
| Top |
|
 |
|
DotCom
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 11:13 pm |
|
| Valued Member |
 |
 |
Joined: Jul 2006 Posts: 497 Location:
|
fangyuan wrote: @DotCom Not really complicated, It's a nice way of handling it, maybe not the best example for people that are new to it, but i like it.  There are lots of ways to handle it, but not the most efficient one. You just complicated a simple program lol  You even managed to include a couple of functions when Nitro doesnt even know how to do a simple loop.
_________________ Server: Alexander
-=IMPERIAL FOREVER=-
[Quit]
|
|
| Top |
|
 |
|
Nitro
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 11:20 pm |
|
| Senior Member |
 |
 |
Joined: Sep 2007 Posts: 4769 Location:
|
DotCom wrote: lol fangyuan, you really complicated such a simple example program. Most of the stuff is not neccessary. Hang on a sec... Nitro wrote: I want to make a program that will ask me a question, and I have to answer with yes or no, and it will put some message...
--------------------------------------------------------------------------- #include<iostream> #include <string> #include <conio.h>using namespace std; int main() { string str; cout<<"Silkroad Online Forum is awesome, right?"; getline(cin, str); if (str == "yes ") { cout<<"Exactly"; } else if (str == "no ") { cout<<"You fail :p"; } else { cout<<"Wrong input entered"; // Since we consider "yes" or "no" as correct input } getch();return 0; } --------------------------------------------------------------------------- Didn't really compile this one, dont think i need to. Explanation: First of all you cannot input a word (yes, no) into a variable declared as a single char (char a). You have to use strings or an array of characters as NuclearSilo mentioned above. In this case i used strings and was neccesary to include the string class first. Declared a string variable called "str" where i would save the string entered by the user input. Getline is a method which can save and entire line entered by the user,including spaces, into a string variable. Its correct syntax is "getline(inputStreamVariable, stringVariable)" where the inputStreamVariable refers to cin (cause thats what we want to do) and stringVariable to the variable we want to store the string. Also using getline method doesnt require you to allocate space for the string variable when you declare it (not so important for now). The rest is self explanatory i guess.  Hope it helped you Yeah it helped alot, few mistakes... I bolded them; thanks  Oh, well, the thing I wanted is to limit the program on answer yes; When someone would enter no, or anything else than no, the program would return us to first "cin". I'm working in devC++ btw Once again thanks, that made sense, I'm starting to understand strings a bit now.
_________________  Thanks Noobs_Slayer for signature.
|
|
| Top |
|
 |
|
DotCom
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 11:28 pm |
|
| Valued Member |
 |
 |
Joined: Jul 2006 Posts: 497 Location:
|
Nitro wrote: Code: include <conio.h> getch();
I'm working in devC++ btw Your dos compiler sucks. I suggest you get Visual studio 6, its a really good compiler for C++ and has a good debugger too. And you wont need to include those old C language syntax which C++ managed to get rid off.
_________________ Server: Alexander
-=IMPERIAL FOREVER=-
[Quit]
|
|
| Top |
|
 |
|
Nitro
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 11:30 pm |
|
| Senior Member |
 |
 |
Joined: Sep 2007 Posts: 4769 Location:
|
DotCom wrote: Nitro wrote: Code: include <conio.h> getch();
I'm working in devC++ btw Your dos compiler sucks. I suggest you get Visual studio 6, its a really good compiler for C++ and has a good debugger too. And you wont need to include those old C language syntax which C++ managed to get rid off. Too bad we are programing in that in school ... I need to get used to it...
_________________  Thanks Noobs_Slayer for signature.
|
|
| Top |
|
 |
|
ThiefzV2
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 11:35 pm |
|
| Active Member |
 |
 |
Joined: Jan 2007 Posts: 566 Location:
|
1)conio.h is deprecated... dont ever use that preprocessor directive anymore. 2)a char accepts only 1 char... if you want to test to see if it's "yes" or "no", u want to use a string since the two words has more than 1 char. 3)use a do-while loop... it's best for what u are trying to do 4)dont put all those uncessary curly braces... its only needed for compound statements. for 1, it's unnecssary. 5) for the love of god, dont use getch() Here's a much better version of your code. study the code and u will see why it's much better. CORRECT WAY wrote: #include <iostream> #include <string> using namespace std; int main() { string a; cout<<"Silkroad Online Forum is awesome, right?"; do { cin >> a; if (a == "yes") cout<<"Exactly\n"; else cout<<"Try again n1gger\n"; } while ( a != "yes"); return 0; } fanyang probably use a programming wizard that generate a template... lots of unnecessary code/do nothing code that no human that knows C++ would type 
_________________

|
|
| Top |
|
 |
|
CrimsonKnight
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 11:39 pm |
|
| Valued Member |
 |
 |
Joined: Jan 2007 Posts: 352 Location:
|
This is one way you could do it using strings. In your original code, you had 'yes' instead of "yes". The while command is a loop that continues until the condition is false. It works just like a normal sentence in English. In this implementation, I declare an integer validityFlag to keep track of whether or not an input statement is valid or not. When the statement is valid, I change validityFlag to 1. When the while loop reaches the end, it checks the condition again and realizes that validityFlag is no longer 0 and exits. The while loop always reads through the entire code inside it, then goes back to the beginning of the code and checks the condition again to decide whether or not to go back inside the loop or exit out. Also, note that this the program is case-sensitive. "Yes" is not the same as "yes". If you want to take either you can use the || operator which means "or". Code: #include <iostream> #include <string> using namespace std;
void main() { int validityFlag = 0; string a; while (validityFlag == 0) {//Checks if answer is valid cout<<"Silkroad Online Forum is awesome, right?"<<endl; cin>>a; if (a == "yes") { cout<<"Exactly"<<endl; validityFlag = 1; //Correct answer, validityFlag changes, condition is false, while loop exits } else { cout<<"Try again"<<endl; //Incorrect answer, validityFlag doesn't change, condition is still true, while loop restarts } } }
_________________

 [Ninjitsu] -x- [Sheppard] -x- [6X]
|
|
| Top |
|
 |
|
ThiefzV2
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 11:43 pm |
|
| Active Member |
 |
 |
Joined: Jan 2007 Posts: 566 Location:
|
CrimsonKnight wrote: This is one way you could do it using strings. In your original code, you had 'yes' instead of "yes". The while command is a loop that continues until the condition is false. It works just like a normal sentence in English. In this implementation, I declare an integer validityFlag to keep track of whether or not an input statement is valid or not. When the statement is valid, I change validityFlag to 1. When the while loop reaches the end, it checks the condition again and realizes that validityFlag is no longer 0 and exits. The while loop always reads through the entire code inside it, then goes back to the beginning of the code and checks the condition again to decide whether or not to go back inside the loop or exit out. Also, note that this the program is case-sensitive. "Yes" is not the same as "yes". If you want to take either you can use the || operator which means "or". Code: #include <iostream> #include <string> using namespace std;
void main() { int validityFlag = 0; string a; while (validityFlag == 0) { cout<<"Silkroad Online Forum is awesome, right?"<<endl; cin>>a; if (a == "yes") { cout<<"Exactly"<<endl; validityFlag = 1; } else { cout<<"Try again"<<endl; validityFlag = 0; } } } int main() is the technically correct heading for the main function heading..... not void main() why on earth do you want to use a boolean flag and make? this is C++, not C \n is better and more efficient than endl
_________________

|
|
| Top |
|
 |
|
Nitro
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 11:50 pm |
|
| Senior Member |
 |
 |
Joined: Sep 2007 Posts: 4769 Location:
|
ThiefzV2 wrote: 1)conio.h is deprecated... dont ever use that preprocessor directive anymore. 2)a char accepts only 1 char... if you want to test to see if it's "yes" or "no", u want to use a string since the two words has more than 1 char. 3)use a do-while loop... it's best for what u are trying to do 4)dont put all those uncessary curly braces... its only needed for compound statements. for 1, it's unnecssary. 5) for the love of god, dont use getch() Here's a much better version of your code. study the code and u will see why it's much better. CORRECT WAY wrote: #include <iostream> #include <string> using namespace std; int main() { string a; cout<<"Silkroad Online Forum is awesome, right?"; do { cin >> a; if (a == "yes") cout<<"Exactly\n"; else cout<<"Try again n1gger\n"; } while ( a != "yes"); return 0; } Tried it, and works awesome... about getch() and <conio.h> ; I simply cannot do it without it, since we are programming in dev C++.... (in school)
_________________  Thanks Noobs_Slayer for signature.
|
|
| Top |
|
 |
|
CrimsonKnight
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 11:52 pm |
|
| Valued Member |
 |
 |
Joined: Jan 2007 Posts: 352 Location:
|
ThiefzV2 wrote: CrimsonKnight wrote: This is one way you could do it using strings. In your original code, you had 'yes' instead of "yes". The while command is a loop that continues until the condition is false. It works just like a normal sentence in English. In this implementation, I declare an integer validityFlag to keep track of whether or not an input statement is valid or not. When the statement is valid, I change validityFlag to 1. When the while loop reaches the end, it checks the condition again and realizes that validityFlag is no longer 0 and exits. The while loop always reads through the entire code inside it, then goes back to the beginning of the code and checks the condition again to decide whether or not to go back inside the loop or exit out. Also, note that this the program is case-sensitive. "Yes" is not the same as "yes". If you want to take either you can use the || operator which means "or". Code: #include <iostream> #include <string> using namespace std;
void main() { int validityFlag = 0; string a; while (validityFlag == 0) { cout<<"Silkroad Online Forum is awesome, right?"<<endl; cin>>a; if (a == "yes") { cout<<"Exactly"<<endl; validityFlag = 1; } else { cout<<"Try again"<<endl; validityFlag = 0; } } } int main() is the technically correct heading for the main function heading..... not void main() why on earth do you want to use a boolean flag and make? this is C++, not C \n is better and more efficient than endl AFAIK, int main() and void main() are pretty much the same depending on how you want the program to end. I used the boolean to show how while works with the condition. *Edit* I copied that part from an old C file I had and added some C++ into it, which is why it's there. While (!=) is much better. I learned \n in C and endl in C++ which is why I used that here. I can't comment on their efficiency.
_________________

 [Ninjitsu] -x- [Sheppard] -x- [6X]
Last edited by CrimsonKnight on Thu Nov 20, 2008 11:56 pm, edited 1 time in total.
|
|
| Top |
|
 |
|
fangyuan
|
Post subject: Re: C++ Help! Posted: Thu Nov 20, 2008 11:55 pm |
|
| Common Member |
 |
 |
Joined: Aug 2008 Posts: 120 Location: Internet
|
Quote: fanyang probably use a programming wizard that generate a template... lots of unnecessary code/do nothing code that no human that knows C++ would type  What? You can tell me what's wrong and "un-human" with the code. The STL Containers are very powerful, and people should get used to it, even for "simple" programs. I'm sure everyone in his school will have the same code like him, hence I suggested std::map, because it will make his different, and outstanding.
|
|
| Top |
|
 |
|
ThiefzV2
|
Post subject: Re: C++ Help! Posted: Fri Nov 21, 2008 12:02 am |
|
| Active Member |
 |
 |
Joined: Jan 2007 Posts: 566 Location:
|
fangyuan wrote: Quote: fanyang probably use a programming wizard that generate a template... lots of unnecessary code/do nothing code that no human that knows C++ would type  What? You can tell me what's wrong and "un-human" with the code.The STL Containers are very powerful, and people should get used to it, even for "simple" programs. I'm sure everyone in his school will have the same code like him, hence I suggested std::map, because it will make his different, and outstanding. you've got to be kidding me right? u think using a template library that adds 585747 lines of code to a program is human when a simple 10 line program can do the exact thing better and faster?
_________________

|
|
| Top |
|
 |
|
ThiefzV2
|
Post subject: Re: C++ Help! Posted: Fri Nov 21, 2008 12:17 am |
|
| Active Member |
 |
 |
Joined: Jan 2007 Posts: 566 Location:
|
Nitro wrote: ThiefzV2 wrote: 1)conio.h is deprecated... dont ever use that preprocessor directive anymore. 2)a char accepts only 1 char... if you want to test to see if it's "yes" or "no", u want to use a string since the two words has more than 1 char. 3)use a do-while loop... it's best for what u are trying to do 4)dont put all those uncessary curly braces... its only needed for compound statements. for 1, it's unnecssary. 5) for the love of god, dont use getch() Here's a much better version of your code. study the code and u will see why it's much better. CORRECT WAY wrote: #include <iostream> #include <string> using namespace std; int main() { string a; cout<<"Silkroad Online Forum is awesome, right?"; do { cin >> a; if (a == "yes") cout<<"Exactly\n"; else cout<<"Try again n1gger\n"; } while ( a != "yes"); return 0; } Tried it, and works awesome... about getch() and <conio.h> ; I simply cannot do it without it, since we are programming in dev C++.... (in school) why exactly do u need that? CrimsonKnight wrote: ThiefzV2 wrote: CrimsonKnight wrote: This is one way you could do it using strings. In your original code, you had 'yes' instead of "yes". The while command is a loop that continues until the condition is false. It works just like a normal sentence in English. In this implementation, I declare an integer validityFlag to keep track of whether or not an input statement is valid or not. When the statement is valid, I change validityFlag to 1. When the while loop reaches the end, it checks the condition again and realizes that validityFlag is no longer 0 and exits. The while loop always reads through the entire code inside it, then goes back to the beginning of the code and checks the condition again to decide whether or not to go back inside the loop or exit out. Also, note that this the program is case-sensitive. "Yes" is not the same as "yes". If you want to take either you can use the || operator which means "or". Code: #include <iostream> #include <string> using namespace std;
void main() { int validityFlag = 0; string a; while (validityFlag == 0) { cout<<"Silkroad Online Forum is awesome, right?"<<endl; cin>>a; if (a == "yes") { cout<<"Exactly"<<endl; validityFlag = 1; } else { cout<<"Try again"<<endl; validityFlag = 0; } } } int main() is the technically correct heading for the main function heading..... not void main() why on earth do you want to use a boolean flag and make? this is C++, not C \n is better and more efficient than endl AFAIK, int main() and void main() are pretty much the same depending on how you want the program to end. I used the boolean to show how while works with the condition. *Edit* I copied that part from an old C file I had and added some C++ into it, which is why it's there. While (!=) is much better. I learned \n in C and endl in C++ which is why I used that here. I can't comment on their efficiency. main() is an int returning function. using void is wrong but a lot of compilers will accept it but it doesn't match the parameter. many strict compiler wont even compile a void main() function and others will give warnings. there are tons of C++ books out there that use void main()... but if i ever hire some1... looking at a code and seeing a void main() will not be a good impression. \n is a newline statement, while endl is an output manipulator that's part of the iostream packing so there's uncessary function calling/passing = waste of cpu cycles. using endl is n00bish.... when someone use endl.. then u know they are a begineer. lol.
_________________

|
|
| Top |
|
 |
|
fangyuan
|
Post subject: Re: C++ Help! Posted: Fri Nov 21, 2008 12:32 am |
|
| Common Member |
 |
 |
Joined: Aug 2008 Posts: 120 Location: Internet
|
ThiefzV2 wrote: fangyuan wrote: Quote: fanyang probably use a programming wizard that generate a template... lots of unnecessary code/do nothing code that no human that knows C++ would type  What? You can tell me what's wrong and "un-human" with the code.The STL Containers are very powerful, and people should get used to it, even for "simple" programs. I'm sure everyone in his school will have the same code like him, hence I suggested std::map, because it will make his different, and outstanding. you've got to be kidding me right? u think using a template library that adds 585747 lines of code to a program is human when a simple 10 line program can do the exact thing better and faster? Feel free to add a check to see if it's really faster, like I said, it may be more code, but it will make it outstanding. Everyone in his school will have the same code, which is boring and I doubt the teacher wants them to use the same approach. Also it's not that much code, it's ~50 lines of code total, and it would be usable for future projects, cause the teacher may want them in the next project to use more than just few strings, I doubt it'd look good with so many if statements. @CrimsonKnight Why should he use the || operator? Since it's being processed as string, you can simply use std::transform and then tolower() or toupper() e.g std::transform(a.begin(), a.end(), a.begin(), tolower);
|
|
| Top |
|
 |
|
ThiefzV2
|
Post subject: Re: C++ Help! Posted: Fri Nov 21, 2008 12:34 am |
|
| Active Member |
 |
 |
Joined: Jan 2007 Posts: 566 Location:
|
fangyuan wrote: ThiefzV2 wrote: fangyuan wrote: What? You can tell me what's wrong and "un-human" with the code.
The STL Containers are very powerful, and people should get used to it, even for "simple" programs. I'm sure everyone in his school will have the same code like him, hence I suggested std::map, because it will make his different, and outstanding. you've got to be kidding me right? u think using a template library that adds 585747 lines of code to a program is human when a simple 10 line program can do the exact thing better and faster? Feel free to add a check to see if it's really faster, like I said, it may be more code, but it will make it outstanding. Everyone in his school will have the same code, which is boring and I doubt the teacher wants them to use the same approach. Also it's not that much code, it's ~50 lines of code total, and it would be usable for future projects, cause the teacher may want them in the next project to use more than just few strings, I doubt it'd look good with so many if statements. u honestly believe a 85747 line code is faster than 10 line code? whateva makes u sleep better at night
_________________

|
|
| Top |
|
 |
|
Cruor
|
Post subject: Re: C++ Help! Posted: Fri Nov 21, 2008 12:43 am |
|
| Loyal Member |
 |
 |
Joined: Apr 2006 Posts: 1999 Location:
|
|
Real men code with punch cards.
_________________
|
|
| Top |
|
 |
|
Page 1 of 1
|
[ 28 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 8 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|