Silkroad Online Forums

A community forum for the free online game Silkroad Online. Discuss Silkroad Online, read up on guides, and build your character and skills.

Faq Search Members Chat  Register Profile Login

All times are UTC




Post new topic Reply to topic  [ 28 posts ] 
Author Message
 Post subject: C++ Help!
PostPosted: Thu Nov 20, 2008 8:03 pm 
Senior Member
User avatar
Offline

Joined: Sep 2007
Posts: 4769
Location:
Off Topic
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! :love:

<3 SRF

_________________
                                                       Image
                                                           Thanks Noobs_Slayer for signature.


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 8:53 pm 
Forum God
User avatar
Offline

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 :roll:

_________________
Playing Age of Wushu, dota IMBA


Last edited by NuclearSilo on Thu Nov 20, 2008 9:10 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 9:01 pm 
Loyal Member
User avatar
Offline

Joined: Nov 2007
Posts: 1987
Location: Mexico
you need a cycle

_________________
Image
Visit the Artist Corner to browse my drawings


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 9:28 pm 
Senior Member
User avatar
Offline

Joined: Sep 2007
Posts: 4769
Location:
Off Topic
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 :P

Could you, Nuclear, write me one quick example of while - do while - continue?
I have hard time understanding it from some googled tutorial.

_________________
                                                       Image
                                                           Thanks Noobs_Slayer for signature.


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 9:34 pm 
Forum God
User avatar
Offline

Joined: Aug 2006
Posts: 8834
Location: Age of Wushu
while http://www.cs.cf.ac.uk/Dave/C/node6.htm ... 0000000000

string http://www.cs.cf.ac.uk/Dave/C/node7.htm ... 0000000000

This is a char : 'a'
This is a string: "a"

A string is a list (array) of chars: "a" is equivalent to {'a','\0'}
Character null to end a string

Like 'word' and 'letter'

_________________
Playing Age of Wushu, dota IMBA


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 9:44 pm 
Veteran Member
User avatar
Offline

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.

_________________
Image
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
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 10:09 pm 
Loyal Member
User avatar
Offline

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?

_________________
Image
Visit the Artist Corner to browse my drawings


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 10:19 pm 
Veteran Member
User avatar
Offline

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.

_________________
Image
Saddam✓ Osama✓ Gaddafi✓ Justin Bieber☐ Rebecca Black☐


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 10:51 pm 
Common Member
User avatar
Offline

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
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 10:54 pm 
Senior Member
User avatar
Offline

Joined: Sep 2007
Posts: 4769
Location:
Off Topic
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"

_________________
                                                       Image
                                                           Thanks Noobs_Slayer for signature.


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 10:57 pm 
Valued Member
User avatar
Offline

Joined: Jul 2006
Posts: 497
Location:
Alexander
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. :P 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
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 11:00 pm 
Common Member
User avatar
Offline

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
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 11:13 pm 
Valued Member
User avatar
Offline

Joined: Jul 2006
Posts: 497
Location:
Alexander
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 :P 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
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 11:20 pm 
Senior Member
User avatar
Offline

Joined: Sep 2007
Posts: 4769
Location:
Off Topic
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. :P 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.

_________________
                                                       Image
                                                           Thanks Noobs_Slayer for signature.


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 11:28 pm 
Valued Member
User avatar
Offline

Joined: Jul 2006
Posts: 497
Location:
Alexander
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
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 11:30 pm 
Senior Member
User avatar
Offline

Joined: Sep 2007
Posts: 4769
Location:
Off Topic
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...

_________________
                                                       Image
                                                           Thanks Noobs_Slayer for signature.


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 11:35 pm 
Active Member
User avatar
Offline

Joined: Jan 2007
Posts: 566
Location:
Off Topic
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 :roll:

_________________
Image :)
Image


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 11:39 pm 
Valued Member
User avatar
Offline

Joined: Jan 2007
Posts: 352
Location:
Venus
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
      }
   }
}

_________________
Image
ImageImage[Ninjitsu] -x- [Sheppard] -x- [6X]


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 11:43 pm 
Active Member
User avatar
Offline

Joined: Jan 2007
Posts: 566
Location:
Off Topic
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

_________________
Image :)
Image


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 11:50 pm 
Senior Member
User avatar
Offline

Joined: Sep 2007
Posts: 4769
Location:
Off Topic
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)

_________________
                                                       Image
                                                           Thanks Noobs_Slayer for signature.


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 11:52 pm 
Valued Member
User avatar
Offline

Joined: Jan 2007
Posts: 352
Location:
Venus
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.

_________________
Image
ImageImage[Ninjitsu] -x- [Sheppard] -x- [6X]


Last edited by CrimsonKnight on Thu Nov 20, 2008 11:56 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Thu Nov 20, 2008 11:55 pm 
Common Member
User avatar
Offline

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 :roll:


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
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Fri Nov 21, 2008 12:02 am 
Active Member
User avatar
Offline

Joined: Jan 2007
Posts: 566
Location:
Off Topic
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 :roll:


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?

_________________
Image :)
Image


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Fri Nov 21, 2008 12:17 am 
Active Member
User avatar
Offline

Joined: Jan 2007
Posts: 566
Location:
Off Topic
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.

_________________
Image :)
Image


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Fri Nov 21, 2008 12:32 am 
Common Member
User avatar
Offline

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 :roll:


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
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Fri Nov 21, 2008 12:34 am 
Active Member
User avatar
Offline

Joined: Jan 2007
Posts: 566
Location:
Off Topic
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? :roll:

whateva makes u sleep better at night

_________________
Image :)
Image


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Fri Nov 21, 2008 12:43 am 
Loyal Member
User avatar
Offline

Joined: Apr 2006
Posts: 1999
Location:
Off Topic
Real men code with punch cards.

_________________
Image


Top
 Profile  
 
 Post subject: Re: C++ Help!
PostPosted: Fri Nov 21, 2008 12:44 am 
Valued Member
User avatar
Offline

Joined: Jan 2007
Posts: 352
Location:
Venus
I am taking C++ right now and the book uses endl.

Here is a quote from it regarding \n and endl:

"The main difference deals with the fact that output is often written to a special place called a buffer, rather than to the actual output device. When the buffer becomes full, the output is "flushed" from the buffer to the actual output device. Such buffering can cause problems if a program crashes before the buffer is flushed. The buffer may contain some output that never shows up on the output device. To avoid this problem, whenever you write the endl object, the output buffer is automatically flushed. However, simply writing the newline character does not automatically flush the output buffer."

So, endl is slower by one step, the flush. While \n is definitely faster, I think endl is better for debugging.

_________________
Image
ImageImage[Ninjitsu] -x- [Sheppard] -x- [6X]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 28 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 16 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

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group