Genesi Posted August 5, 2004 Share Posted August 5, 2004 #include <iostream> #include <string> using namespace std; void main() { int c, nb, nt, nl; nb = 0; nt = 0; nl = 0; while (c = getchar() != EOF) { if (c == ' ') ++nb; if (c == '\t') ++nt; if (c == '\n') ++nl; } } If I hit enter then it keeps going, never terminates. Link to comment Share on other sites More sharing options...
0 Oogle Posted August 5, 2004 Share Posted August 5, 2004 It could be an order of operations problem. Try this... ... while ( (c = getchar()) != EOF ) ...etc... Link to comment Share on other sites More sharing options...
0 Lycan Posted August 5, 2004 Share Posted August 5, 2004 honestly dont understand the expression in the while loop, anyway your forget to the "return 0 " before the last bracket Link to comment Share on other sites More sharing options...
0 Genesi Posted August 5, 2004 Author Share Posted August 5, 2004 its a void main in which case you shouldn't need the return 0. My understanding is if your main is something like: int main() { return 0; } Then you will need it. Link to comment Share on other sites More sharing options...
0 Lycan Posted August 5, 2004 Share Posted August 5, 2004 ow yeh thats true, still void main() not many used nowadays :hmmm: Link to comment Share on other sites More sharing options...
0 Kestrel Posted August 5, 2004 Share Posted August 5, 2004 Oogle has answered the question already, but just a point of order: Don't use: void main() { } ... the only way you should ever declare your main method is as: int main(int argc, char* argv[]) { } or int main(int argc, char** argv) { } Variants that return void or have empty parameter lists are just poor style. Link to comment Share on other sites More sharing options...
0 smurfiness Posted August 5, 2004 Share Posted August 5, 2004 Variants that return void or have empty parameter lists are just poor style. Why? Unless you're explicitly interested in the return code (and you explicitly write return codes), it doesn't make any difference whether something gets returned or not. And if the app doesn't take any parameters, what's the point of having meaningless parameters passed into your program? Link to comment Share on other sites More sharing options...
0 Genesi Posted August 5, 2004 Author Share Posted August 5, 2004 ow yeh thats true, still void main() not many used nowadays :hmmm: I'm old school :blush: Getting in to new school though still growing old. Link to comment Share on other sites More sharing options...
0 Genesi Posted August 5, 2004 Author Share Posted August 5, 2004 Just whenever I use EOF the program never works. I can use '\n' at times but sometimes I need to count that so that somewhat defeats the purprose. Link to comment Share on other sites More sharing options...
0 Schmoove Posted August 5, 2004 Share Posted August 5, 2004 Why? Unless you're explicitly interested in the return code (and you explicitly write return codes), it doesn't make any difference whether something gets returned or not. And if the app doesn't take any parameters, what's the point of having meaningless parameters passed into your program? Exactly!! Only use parameters and return values if you actually need them. Else "void main()" is fine and even preferred. There is absolutely no reason to use something else if you don't even need it in the first place. Link to comment Share on other sites More sharing options...
0 bwx Posted August 5, 2004 Share Posted August 5, 2004 Just whenever I use EOF the program never works. I can use '\n' at times but sometimes I need to count that so that somewhat defeats the purprose. Do what Oogle said and your code should work fine. The reason you need to wrap it in parenthesis is because '!=' has higher precedence than '='. Therefore, when the expression while (c = getchar() != EOF) is evaluated, the result of getchar() != EOF is assigned to c instead of the result of getchar() being assigned to c which is obviously what you did not intend to do. Also, EOF is triggered when you press CTRL+Z, the ENTER key only triggers a newline (\r\n in windows). Hope that clears some things up. Link to comment Share on other sites More sharing options...
0 Genesi Posted August 5, 2004 Author Share Posted August 5, 2004 Do what Oogle said and your code should work fine. The reason you need to wrap it in parenthesis is because '!=' has higher precedence than '='. Therefore, when the expression while (c = getchar() != EOF) is evaluated, the result of getchar() != EOF is assigned to c instead of the result of getchar() being assigned to c which is obviously what you did not intend to do. Also, EOF is triggered when you press CTRL+Z, the ENTER key only triggers a newline (\r\n in windows). Hope that clears some things up. Awsome, thanks ;) So is it \r\n both together or one or the other. If it is bot together then how can you say that? Also, if it helps I am using a console program (win32) on visual c++. Link to comment Share on other sites More sharing options...
0 Kestrel Posted August 5, 2004 Share Posted August 5, 2004 Exactly!!Only use parameters and return values if you actually need them. Else "void main()" is fine and even preferred. There is absolutely no reason to use something else if you don't even need it in the first place. Err... no... that is completely incorrect advice. "void main() { }" is NEVER a preferred option and the vast majority of developers will gladly tell you so. The language spec defines main as: int main(int argc, char* argv[]) { } anything else is BAD STYLE and sloppy. On the other point, your apps should ALWAYS return a numeric value. Link to comment Share on other sites More sharing options...
0 bwx Posted August 5, 2004 Share Posted August 5, 2004 Awsome, thanks ;) So is it \r\n both together or one or the other. If it is bot together then how can you say that? Also, if it helps I am using a console program (win32) on visual c++. They usually come in pairs, so if you detect '\n' only, you should have no problems catching the correct number of newlines. Link to comment Share on other sites More sharing options...
0 Schmoove Posted August 5, 2004 Share Posted August 5, 2004 Err... no... that is completely incorrect advice. "void main() { }" is NEVER a preferred option and the vast majority of developers will gladly tell you so. The language spec defines main as: int main(int argc, char* argv[]) { } anything else is BAD STYLE and sloppy. On the other point, your apps should ALWAYS return a numeric value. I've been programming for 6 years now in various languages, but mostly in C++. There is absolutly no reason not to use "void main()" when you don't need the result or the startup parameters. Using it is fine. Claiming that you can better use "int main(int argc, char* argv[])" is the biggest bullsh*t I have ever heard.... simply because there is absolutly no benefit. Link to comment Share on other sites More sharing options...
0 Genesi Posted August 5, 2004 Author Share Posted August 5, 2004 Can't we all just be programming nerds Link to comment Share on other sites More sharing options...
0 Genesi Posted August 5, 2004 Author Share Posted August 5, 2004 They usually come in pairs, so if you detect '\n' only, you should have no problems catching the correct number of newlines. Oogles idea didn't work :(, also '\r' only works, not '\n'. Basically, I'm working for a way to count spaces (tabs, newlined, etc). Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted August 5, 2004 Share Posted August 5, 2004 To settle this "main" argument, go to the master's site. http://www.research.att.com/~bs/bs_faq2.html#void-main Link to comment Share on other sites More sharing options...
0 Genesi Posted August 5, 2004 Author Share Posted August 5, 2004 I didn't mean to start a argument, it wasn't even my question :( My Apologies to every one. Link to comment Share on other sites More sharing options...
0 bwx Posted August 5, 2004 Share Posted August 5, 2004 Oogles idea didn't work :(, also '\r' only works, not '\n'.Basically, I'm working for a way to count spaces (tabs, newlined, etc). This should give you an idea on how to do it: #include <iostream> #include <iomanip> #include <stdio.h> int main (int argc, char **argv) { ? ?char c; ? ?int numSpaces ? ? ? ? ?= 0, ? ? ? ?numHorizontalTabs ?= 0, ? ? ? ?numVerticalTabs ? ?= 0, ? ? ? ?numNewLines ? ? ? ?= 0, ? ? ? ?numBackSpaces ? ? ?= 0, ? ? ? ?numCarriageReturns = 0; ? ?while ( (c=getchar()) != EOF ) ? ?{ ? ? ? ?switch (c) ? ? ? ?{ ? ? ? ?case ' ': ? ? ? ? ? ?++numSpaces; ? ? ? ? ? ?break; ? ? ? ?case '\t': ? ? ? ? ? ?++numHorizontalTabs; ? ? ? ? ? ?break; ? ? ? ?case '\v': ? ? ? ? ? ?++numVerticalTabs; ? ? ? ? ? ?break; ? ? ? ?case '\n': ? ? ? ? ? ?++numNewLines; ? ? ? ? ? ?break; ? ? ? ?case '\r': ? ? ? ? ? ?++numCarriageReturns; ? ? ? ? ? ?break; ? ? ? ?case '\b': ? ? ? ? ? ?++numBackSpaces; ? ? ? ? ? ?break; ? ? ? ?} ? ?} ? ? ? ?std::cout << "Reached EOF\n\n"; ? ? ? ?int width = 25; ? ?std::cout << std::setw(width) << "Spaces: " << std::setw(width) << numSpaces << '\n'; ? ?std::cout ?<< std::setw(width) << "Horizontal Tabs: " << std::setw(width) << numHorizontalTabs << '\n'; ? ?std::cout << std::setw(width) << "Vertical Tabs: " << std::setw(width) << numVerticalTabs << '\n'; ? ?std::cout << std::setw(width) << "New Lines: " << std::setw(width) << numNewLines << '\n'; ? ?std::cout << std::setw(width) << "BackSpaces: " << std::setw(width) << numBackSpaces << '\n'; ? ?std::cout << std::setw(width) << "Carriage Returns: " << std::setw(width) << numCarriageReturns << '\n'; ? ?std::cout << '\n'; ? ? ? ?int total = numSpaces + numHorizontalTabs + numVerticalTabs + numNewLines + numBackSpaces + ? ? ? ?numCarriageReturns; ? ?std::cout << std::setw(width) << "Total: " << std::setw(width) << total << '\n'; ? ?return 0; } Hope that helps. Link to comment Share on other sites More sharing options...
0 Genesi Posted August 5, 2004 Author Share Posted August 5, 2004 What compiler are you using? Still doesn't work on Mircrosoft Visual Studio. At least when I hit enter it goes to the next line -- it doesn't terminate the program. Link to comment Share on other sites More sharing options...
0 bwx Posted August 5, 2004 Share Posted August 5, 2004 What compiler are you using? Still doesn't work on Mircrosoft Visual Studio. At least when I hit enter it goes to the next line -- it doesn't terminate the program. The loop stops at EOF. In windows, to generate an EOF character, hold down CTRL then press Z. Hope that helps. Link to comment Share on other sites More sharing options...
0 Genesi Posted August 6, 2004 Author Share Posted August 6, 2004 :argh: I give up that still doesn't work. Well back to the drawing board but I do appreciate the help guys. EDIT: Nevermind I hit control+enter not Z. Thanks man that worked :) Link to comment Share on other sites More sharing options...
0 Oogle Posted August 6, 2004 Share Posted August 6, 2004 Nevermind I hit control+enter not Z. Thanks man that worked PEBKAC bugs are always the hardest to fix. ;) Link to comment Share on other sites More sharing options...
0 Kestrel Posted August 7, 2004 Share Posted August 7, 2004 I've been programming for 6 years now in various languages, but mostly in C++.There is absolutly no reason not to use "void main()" when you don't need the result or the startup parameters. Using it is fine. Claiming that you can better use "int main(int argc, char* argv[])" is the biggest bullsh*t I have ever heard.... simply because there is absolutly no benefit. Good for you - I've been programming for 21 years now mostly in C and C++. Your point? The fact is that void main() is considered bad style in the same vein as the use of goto. See Weenur's post for more info. Link to comment Share on other sites More sharing options...
0 vcv Posted August 7, 2004 Share Posted August 7, 2004 Good for you - I've been programming for 21 years now mostly in C and C++. Your point?The fact is that void main() is considered bad style in the same vein as the use of goto. See Weenur's post for more info. If someone knows what they are doing, what is the big deal? You do realize that all branch structures in all compiled languages end up as "goto"'s anyways, I would hope. Aside from "style", what purpose does including the return type and arguments if they are not used?? Please, share this with me. Link to comment Share on other sites More sharing options...
Question
Genesi
#include <iostream> #include <string> using namespace std; void main() { int c, nb, nt, nl; nb = 0; nt = 0; nl = 0; while (c = getchar() != EOF) { if (c == ' ') ++nb; if (c == '\t') ++nt; if (c == '\n') ++nl; } }If I hit enter then it keeps going, never terminates.
Link to comment
Share on other sites
39 answers to this question
Recommended Posts