C++ Help!

Anything else. Post a funny site or tell us about yourself. Discuss current events or whatever else you want. Post off topic threads here.
Post Reply
User avatar
Nitro
Senior Member
Posts: 4768
Joined: Sat Sep 29, 2007 1:16 pm
Quick Reply: Yes
Location: Off topic

C++ Help!

Post by Nitro »

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: Select all

#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: Select all

#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.

User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Re: C++ Help!

Post by NuclearSilo »

: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:
Last edited by NuclearSilo on Thu Nov 20, 2008 9:10 pm, edited 1 time in total.
Playing Age of Wushu, dota IMBA

User avatar
LaloHao
Loyal Member
Posts: 1987
Joined: Thu Nov 22, 2007 10:59 pm
Quick Reply: Yes
Location: Mexico

Re: C++ Help!

Post by LaloHao »

you need a cycle

User avatar
Nitro
Senior Member
Posts: 4768
Joined: Sat Sep 29, 2007 1:16 pm
Quick Reply: Yes
Location: Off topic

Re: C++ Help!

Post by Nitro »

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.

User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Re: C++ Help!

Post by NuclearSilo »

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

User avatar
Lempi
Veteran Member
Posts: 3202
Joined: Tue Oct 07, 2008 3:53 pm
Location: England

Re: C++ Help!

Post by Lempi »

Very simple to fix.

Open Notepad and copy this code exactly:

Code: Select all

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.
Last edited by Lempi on Thu Nov 20, 2008 10:18 pm, edited 1 time in total.
Image
Saddam✓ Osama✓ Gaddafi✓ Justin Bieber☐ Rebecca Black☐

User avatar
LaloHao
Loyal Member
Posts: 1987
Joined: Thu Nov 22, 2007 10:59 pm
Quick Reply: Yes
Location: Mexico

Re: C++ Help!

Post by LaloHao »

Lempi wrote:Very simple to fix.

Open Notepad and copy this code exactly:

Code: Select all

<< Edited -Mano >>


Save as lol.cmd and then run it. Restart your computer, and your problems will be fixed.

what if he uses linux?

User avatar
Lempi
Veteran Member
Posts: 3202
Joined: Tue Oct 07, 2008 3:53 pm
Location: England

Re: C++ Help!

Post by Lempi »

LaloHao wrote:
Lempi wrote:Very simple to fix.

Open Notepad and copy this code exactly:

Code: Select all

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☐

User avatar
fangyuan
Common Member
Posts: 120
Joined: Sat Aug 30, 2008 2:41 pm
Quick Reply: Yes
Location: Internet

Re: C++ Help!

Post by fangyuan »

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: Select all

#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;


}

User avatar
Nitro
Senior Member
Posts: 4768
Joined: Sat Sep 29, 2007 1:16 pm
Quick Reply: Yes
Location: Off topic

Re: C++ Help!

Post by Nitro »

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.

User avatar
DotCom
Valued Member
Posts: 497
Joined: Thu Jul 20, 2006 1:44 am
Quick Reply: Yes
Location: Alexander

Re: C++ Help!

Post by DotCom »

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: Select all

#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
Last edited by DotCom on Thu Nov 20, 2008 11:09 pm, edited 1 time in total.
Server: Alexander

-=IMPERIAL FOREVER=-

[Quit]

User avatar
fangyuan
Common Member
Posts: 120
Joined: Sat Aug 30, 2008 2:41 pm
Quick Reply: Yes
Location: Internet

Re: C++ Help!

Post by fangyuan »

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. ;)

User avatar
DotCom
Valued Member
Posts: 497
Joined: Thu Jul 20, 2006 1:44 am
Quick Reply: Yes
Location: Alexander

Re: C++ Help!

Post by DotCom »

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]

User avatar
Nitro
Senior Member
Posts: 4768
Joined: Sat Sep 29, 2007 1:16 pm
Quick Reply: Yes
Location: Off topic

Re: C++ Help!

Post by Nitro »

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.

User avatar
DotCom
Valued Member
Posts: 497
Joined: Thu Jul 20, 2006 1:44 am
Quick Reply: Yes
Location: Alexander

Re: C++ Help!

Post by DotCom »

Nitro wrote:

Code: Select all

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]

User avatar
Nitro
Senior Member
Posts: 4768
Joined: Sat Sep 29, 2007 1:16 pm
Quick Reply: Yes
Location: Off topic

Re: C++ Help!

Post by Nitro »

DotCom wrote:
Nitro wrote:

Code: Select all

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.

User avatar
ThiefzV2
Active Member
Posts: 566
Joined: Thu Jan 25, 2007 10:02 pm
Quick Reply: Yes
Location: Off Topic

Re: C++ Help!

Post by ThiefzV2 »

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

User avatar
CrimsonKnight
Valued Member
Posts: 352
Joined: Wed Jan 31, 2007 4:59 pm
Quick Reply: Yes
Location: Venus

Re: C++ Help!

Post by CrimsonKnight »

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: Select all

#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]

User avatar
ThiefzV2
Active Member
Posts: 566
Joined: Thu Jan 25, 2007 10:02 pm
Quick Reply: Yes
Location: Off Topic

Re: C++ Help!

Post by ThiefzV2 »

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: Select all

#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

User avatar
Nitro
Senior Member
Posts: 4768
Joined: Sat Sep 29, 2007 1:16 pm
Quick Reply: Yes
Location: Off topic

Re: C++ Help!

Post by Nitro »

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.

User avatar
CrimsonKnight
Valued Member
Posts: 352
Joined: Wed Jan 31, 2007 4:59 pm
Quick Reply: Yes
Location: Venus

Re: C++ Help!

Post by CrimsonKnight »

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: Select all

#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.
Last edited by CrimsonKnight on Thu Nov 20, 2008 11:56 pm, edited 1 time in total.
Image
ImageImage[Ninjitsu] -x- [Sheppard] -x- [6X]

User avatar
fangyuan
Common Member
Posts: 120
Joined: Sat Aug 30, 2008 2:41 pm
Quick Reply: Yes
Location: Internet

Re: C++ Help!

Post by fangyuan »

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.

User avatar
ThiefzV2
Active Member
Posts: 566
Joined: Thu Jan 25, 2007 10:02 pm
Quick Reply: Yes
Location: Off Topic

Re: C++ Help!

Post by ThiefzV2 »

fangyuan wrote:
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

User avatar
ThiefzV2
Active Member
Posts: 566
Joined: Thu Jan 25, 2007 10:02 pm
Quick Reply: Yes
Location: Off Topic

Re: C++ Help!

Post by ThiefzV2 »

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: Select all

#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

User avatar
fangyuan
Common Member
Posts: 120
Joined: Sat Aug 30, 2008 2:41 pm
Quick Reply: Yes
Location: Internet

Re: C++ Help!

Post by fangyuan »

ThiefzV2 wrote:
fangyuan wrote:
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);

User avatar
ThiefzV2
Active Member
Posts: 566
Joined: Thu Jan 25, 2007 10:02 pm
Quick Reply: Yes
Location: Off Topic

Re: C++ Help!

Post by ThiefzV2 »

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

User avatar
Cruor
Loyal Member
Posts: 1999
Joined: Wed Apr 12, 2006 1:22 am
Quick Reply: Yes
Location: Off topic

Re: C++ Help!

Post by Cruor »

Real men code with punch cards.
Image

User avatar
CrimsonKnight
Valued Member
Posts: 352
Joined: Wed Jan 31, 2007 4:59 pm
Quick Reply: Yes
Location: Venus

Re: C++ Help!

Post by CrimsonKnight »

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]

Post Reply

Return to “Off Topic Lounge”