Like Tree5Likes

simple check.

This is a discussion on simple check. within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by rogster001 i am intrigued - without wanting to divert this thread would like to hear your thinking ...

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    Quote Originally Posted by rogster001 View Post
    i am intrigued - without wanting to divert this thread would like to hear your thinking on that.
    Using namespace directives potentially cause ambiguity. For example, if there is a name in the global (unnamed) namespace that is the same as a name within namespace std, then any usage of that name will always be ambiguous after a "using namespace std" directive.

    Once that ambiguity occurs, there are only two possible options to resolve it, since the effect of a "using namespace" directive can't be undone by any means except removing the directive.

    The first approach to resolving such an ambiguity is to fully qualifying all names (i.e. prefixing all names with their namespace). That negates the point of the existing "using namespace" directive for all names where ambiguity has been introduced.

    The second approach is to remove the "using namespace" directive, which then makes it necessary to ensure all names (except, optionally, those in the global namespace) are fully qualified.

    Practically, for anything except the tiniest code samples, I find the effort of resolving such an ambiguity upon encountering it exceeds the effort of avoiding it in the first place (i.e. not employing any "using namespace" directives, and fully qualifying all names except - optionally - those that are local to a file or in the global namespace).

    And since I generally write code with a view to maintaining it, using it in a larger system, or extending it ...... I find it better to simply avoid "using namespace" directives in real code. The only time I employ a "using namespace" directive is actually in code samples I post in forums.

    I'm not fussed if people choose to employ a "using namespace" directive in their own code. They wear the impact. But a "using namespace" directive in a header file is a no-no, since it forces me to manage impacts whether I wish to or not. If I encounter a "using namespace" directive in a header file for a library, I do not use that library. Period.
    Last edited by grumpy; 07-16-2012 at 05:18 AM.
    Right 98% of the time, and don't care about the other 3%.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Quote Originally Posted by rogster001 View Post
    Yes! that is the standard - Elysia has posted some good links on this if i recall, maybe she will drop one in this thread for you.
    Actually, no.
    The C++ standard says that main will implicitly return 0 if you don't add it yourself, so there is no need.
    Whether it is good or practice omitting it is discussable, and I'm sure there are people on both sides.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    I prefer just saying "using std::cout; using std::cin;" o3o

    Quote Originally Posted by Elysia View Post
    Actually, no.
    The C++ standard says that main will implicitly return 0 if you don't add it yourself, so there is no need.
    Whether it is good or practice omitting it is discussable, and I'm sure there are people on both sides.
    I had no idea about that. I usually scramble to make sure main returns something. Thank you O:
    How to ask smart questions

    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesWritten);

  4. #19
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    Now, compare it with my version. What does it do that your code does not (other than some minor changes of what the program outputs, and that I've removed the cin.get()/cin.ignore() closing)? How easy it it to extend?
    firstly, thank you very much grumpy for taking the time to do this.. now to answer your question, it seems to me that your code allows me to enter new candidates/options without adding other sections of else if statements. also I wouldn't have to go back and change the condition under which the loop ends, and if i did add new candidates the option to choose them would be of no work, seeing as adding a candidate also makes it an option.

    i truly appreciate your help, but just to put it out there, I am very early on in learning.. Therefore I don't know of the terms vector or label.. and I will have to study this code for some time to fully understand each step. (there is alot going on that I am very unfamiliar with) However It DOES show me a more compact way of doing this and that's a start, even if I don't fully get it... YET

    I will look into those FAQs Rogster, thanks much!

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    Quote Originally Posted by Mike Beal View Post
    i truly appreciate your help, but just to put it out there, I am very early on in learning.. Therefore I don't know of the terms vector or label.. and I will have to study this code for some time to fully understand each step. (there is alot going on that I am very unfamiliar with) However It DOES show me a more compact way of doing this and that's a start, even if I don't fully get it... YET
    Fair enough. I accept you're on the learning curve.

    vector and string, as I used them in the last sample, are names of type in the C++ standard library, and you'll find them eventually discussed in any decent textbook or reference material on C++. When you learn about templates (an important part of the C++ language, but there are plenty of things to learn before you can learn properly) you'll better understand what the syntax vector<string> means. For now, just look at "vector<string> label;" as declaring a variable named label that is a vector of strings. "label" is just an arbitrary variable name. A vector is a logical construct that contains a group of multiple elements.
    Right 98% of the time, and don't care about the other 3%.

  6. #21
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Actually, no.
    The C++ standard says that main will implicitly return 0 if you don't add it yourself, so there is no need.
    Whether it is good or practice omitting it is discussable, and I'm sure there are people on both sides.
    Yes I saw that i ahd got mixed up with the int main() / void main() thing - but i still didnt know about any implicit return value so nice one.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #22
    Registered User
    Join Date
    Aug 2011
    Posts
    91

    Smile

    one way i can think of to reduce all the if statements are
    Code:
    #include<conio.h>
    #include<iostream.h>
     int main()
     {
     	int obama=0,romney=0,bank=0,answer=0;
     	while(obama<5&&romney<5&&bank<5)
     	{
    		cout<<endl<<"who do you want to see as president?";
     		cout<<endl<<"1.obama\n"<<"2.romney\n"<<"3.bank..";
     		cin>>answer;
     		switch(answer)
     		{
    	 		case 1:obama++;
     					break;
    	 		case 2:romney++;
    	 				break;
    			case 3:bank++;
    					break;
    			default:cout<<endl<<"what the??";
    		
    	 	}	
    	 }
    	 cout<<endl<<"obama->"<<obama<<endl<<"romney->"<<romney<<endl<<"bank->"<<bank;
    	 getch();
     }
    the braces can be avoidedlthis way
    do give your feedback
    cin.ignore() can be replaced by getch().


    just keep this in mind when you move forward and become a high performance server programmer one day...a series of nested if statements is faster than a switch statement. Compared to if, a switch statement is only faster if there are 100 or more conditions.
    Last edited by theju112; 07-22-2012 at 07:09 AM. Reason: no specific..

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by theju112
    cin.ignore() can be replaced by getch()
    getch() is non-standard, as is <conio.h>. Furthermore, the correct header is <iostream>, not <iostream.h>
    Elysia and manasij7479 like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by theju112 View Post
    just keep this in mind when you move forward and become a high performance server programmer one day...a series of nested if statements is faster than a switch statement. Compared to if, a switch statement is only faster if there are 100 or more conditions.
    Spoken like a true noob. Are you a troll or are you really that clueless?

    Okay, let me put it clearly...
    Anyone giving advice that ridiculous is not qualified to make ANY performance recommendations whatsoever! Kapish?!
    Elysia likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple check/deposit exercise in C
    By edishuman in forum C Programming
    Replies: 10
    Last Post: 09-15-2011, 10:49 AM
  2. Simple login/password check
    By ceevee in forum C++ Programming
    Replies: 26
    Last Post: 01-16-2005, 11:05 PM
  3. Simple check program
    By Shadow in forum C Programming
    Replies: 3
    Last Post: 06-05-2002, 06:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21