| |
| |
| |
|
Page: 1 2 3 4 5 6 7
Comments:
<0> :-D my shell is so nice :-D <0> Quantum`` thx a lot man <1> The program works, but it's got a problem <2> http://www.noidea128.org/sourcefiles/16054.html <3> 2 laptops for sale must go today price 500. message me if interested <2> boo yah!!! <2> Thanks guys. <0> PM2 well.. i could have done all the garbage collection manually. <4> skadet: c++ arrays are 0-based <0> but i'm too lazy to type free(****) every now and then. <1> 7: if(argc < 2) <1> 8: { <1> 9: printf("0\n"); <1> 10: return 0; <1> 11: } <1> 12:
<4> skadet: and you can use std::accumulate to add an iterator sequence (which an array can give you) <1> DOn't put that zxy, it's useless <0> aight aight :-D <0> later <1> See ya! <2> Eh, it's C <4> The C programming language will work; however, since you are in a C++ programming channel, the ***umption is made that you want a good C++ solution. <1> skadet : Why this? hold = (atoi(argv[i]) + hold); ????? <1> skadet : use += <2> PM2, heh rihgt <2> nice catch <5> PM2, what problem? <1> skadet : And remove the if(argc < 2), you don't need that, 'cuz if there are no arguments, it won't go into the for loop, so the default value of hold will be printed, which is 0 in this case ... <2> PM2, it printed (null) <1> Quantum`` : I have to redirect input into this program : prog < input.txt <2> oi <2> no you're right <2> ha <2> it was before... nm you're right <1> skadet : have you initialized hold? You have to ... <1> skadet : If you don't initialize it, it will print null <2> yeah when declaring <2> int hold = 0 <1> Quantum`` : It works, it does take the input, BUT at the EOF, it loops with the last read characters... <2> i had just int hold before <1> skadet : :) <5> it loops with the last read characters... What? <1> Quantum`` : Whatever is in the file input.txt <1> Quantum`` : Let's say you have this in the file input.txt <1> 1 <1> Starbuck Cafe <1> 12:00 <1> 60 <3> 2 laptops for sale must go today price 500. message me if interested <1> 3 <4> skadet: #include <numeric> #include <whatever_for_atoi> #include <iostream> int main( int argc, char* argv[] ) { std::cout << std::accumulate( argv+1, argv+argc, 0, &std::atoi ) << '\n'; return 0; } <5> atoi is not a member of std <1> It's going to use option 1, answer Starbuck Cafe to the first question, then 12:00 and 60, return to the menu and use option 3 the display the meetings <4> Quantum``: yes it is <1> atoi is C <4> PM2: and? <4> ah, it's <cstdlib> <1> Isn't STD C++? <4> PM2: yes, and? <2> PM2: what about input checks... what if they enter a word instead of a number?! <2> uh oh <4> <cstdlib> is a c++ header <5> ok, i see sorry, c headers <1> Well if atoi is C, and STD is C++ ... how could atoi be STD? <4> PM2: because c++ inherits atoi from c <1> skadet : try{} catch(){} ... <1> skadet : have fun! lol :) <5> Kniht, but that wont work <1> skadet : actually, it might work without the try ... <4> Quantum``: why not? <5> Kniht, because atoi is not a binary op, and will definitely not addup numbers <1> skadet : it doesn't crash, it just doesn't add the word to the rest ... <2> PM2, yeah I see that... <2> damn i'd like some input checking thouogh... <4> ah, I did miss that <1> skadet : But there should be a function to verify if the variable is a digit or not
<1> skadet : Check www.cppreference.com for that <2> Ok <4> #include <numeric> #include <cstdlib> #include <iostream> template< typename T > T add_atoi( T a, char const* b ) { return a + atoi( b ); } int main( int argc, char* argv[] ) { std::cout << std::accumulate( argv+1, argv+argc, 0, &f<int> ) << '\n'; return 0; } <1> skadet : http://www.cppreference.com/stdstring/isdigit.html <2> hehe i was just looking at that ;) <4> doh, I renamed it in one place and not another <1> skadet : Actually, that won't work with numbers, only with digits ... <2> I could run it against argv[], right? <2> they're digits before atoi()? <4> http://rafb.net/paste/results/rCNOyg38.html <1> skadet : Well, if I understand the definition of isdigit() correctly, it will only works on digits from 0 to 9 ..., not 23 for example <1> Quantum`` : Any idea about my problem? <5> Knith, probably one more problem... <5> Knith, you will probably want to change the order of the arguments <4> Quantum``: only the same problems as using atoi in the first place <4> no, I don't <5> add_atoi( char const* b, T a ) <5> ah <5> you are right <5> my bad <5> :) <4> if that was a problem, it wouldn't have compiled and ran correctly :P <5> agreed <5> sorry <1> skadet : http://www.noidea128.org/sourcefiles/16055.html <4> PM2: that's rather poor checking, use strtol or strtoul <4> or lexical_cast <1> strtol? <4> yes, strtol, or std::strtol if you prefer ;) <1> That's C++ no? <4> strtol is in C too <1> Yup, just saw that <1> stdlib.h <1> Would anyone be willing to take 5 minutes and test this on his/her computer ? <1> http://www.noidea128.org/sourcefiles/16056.html <1> You have all the code to create the 4 necessary files, including the test file <4> http://rafb.net/paste/results/3Q7vPy28.html <4> sentry2 = If you have dir1/foo.h and dir2/foo.h, then using FOO_H in both will quickly make you run out of aspirin. Instead prefer something more specific, such as AUTHOR_20060123_0937 (yyyymmdd_hhnn) -- this way you only have to think about headers you created in the past 60 seconds to know if you clash or not. <4> same applies for H_FOO <4> global using declarations in headers are worse than global variables <4> you don't test if input succeeds (line 328) <4> what's the problem with it? <1> Are you using Unix or Windows? <4> why are you writing a linked list by hand instead of using a std::list or similar container? <4> both <1> I can't use anything STD whatsoever ... <4> why? <1> Ask my prof... <1> lol <4> you're already using the iostream and string headers <1> I had written the first draft using vectors ... then I read what the prof wrote ... no STL <4> and in that case, I'd write my own cl*** which is eerily similar to std::list, and even name it Kniht::List <1> Anyways, the list works :) <4> there's no excuse for mixing the two logics like that <4> if it works, why are you asking about it? <1> Problem is when I redirect input.txt into the application like this : apps < input.txt <1> It loops with the last character in the input.txt file ... <4> like I said, you don't check if input succeeds <1> Try it, you'll see <1> Ok so how do I modify this code to do it? <1> So that when it has reached the EOF of input.txt, it returns to the menu and wait for user input <1> OR just goes to the menu and wait for user input if there is no input.txt direction <1> redirection <4> for (;;) { std::cout << "Menu" "\n"; int option; if ( std::cin >> option ) { /* use option */ } else if ( std::cin.eof() ) { break; } else { std::cout << "Invalid option." "\n"; std::cin.ignore( INT_MAX, '\n' ); std::cin.clear(); } } <4> if you're redirected input.txt to stdin, you'll have to use some system-dependent way of getting interactive input, as cin simply uses stdin <1> So what you're saying is there is no way to do both? <1> At least not at the same time ... <4> sure there is, but it's system-dependent <1> So I can't code something that would both work on Unix and Windows OSes? <4> you can specify input.txt as a parameter to the program, and then use both it and stdin however you like <4> sure you can, but not without system-specific parts <1> I can't, the prof really specified using input.txt with redirection <1> Inputting a file is another part of the application <4> then it is stdin, and you'll need some system call to either change stdin or get user input
Return to
#c++ or Go to some related
logs:
#linuxhelp #linux set focus to hidden field #linux #teens #linux Apache The server's certificate chain is incomplete #squid #chatzone vmware one speaker problem
|
|