another C++ input question -- char to int???
| spiroth10 (17) | |||||
| I'm trying to write a simple console calculator app -- the inner workings of the program are pretty easy, C/C++ will pretty much do the core math work for me... its the input thats getting me -- I've written calculators before, but with clunky UIs -- this one I want just to be a welcome message and a console -- type in the math problem and get an answer or type in exit and get the program to return 0;... however, the code I have wont even compile afaik, and I doubt it would even work if it did -- im using char input[128]; to store user input, checking if it says exit, and using atoi(input); to convert it to an integer and post it on screen if it doesnt say 'exit'.... and its all in a do while loop... I think Im misusing atoi and Im probably going to get the # value of the characters as chars... I realize my current methods probably wont work, but once I get my input function working, I can probably work from there and get things up quick.... anyway heres my code right now:
the error messages I get in VC++ are
I really appreciate any help you guys can give me... | |||||
| helios (1515) | |||
| Line 11: if (input == 'exit') To compare C strings: http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html Single quotes are for single characters only. The same goes for line 18. | |||
| Saneticus (6) | |||
| I'm going to try to help you learn in this one so we'll look at the errors in turn. The first one is... main.cpp(11) : error C2446: '==' : no conversion from 'int' to 'char *' look at your usage of the == operator in line 11. The compiler is trying to tell you it can't compare an integer (int) to a pointer to character (char *). Think about this one and then try to figure out what the compiler is trying to tell you for the other errors. NOTE: There are some other errors so if you figure these out don't expect your program to work just yet. Keep posting and we'll try to help you learn along the way. Good luck! | |||
| Saneticus (6) | |||
| Or Helios could just spell it out for you LOL :) s'all good! | |||
| spiroth10 (17) | |||
| edit: no I didnt expect it to work right off the bat -- I said that before... Im actually working on simple projects like this to improve my knowledge of C++ fundamentals... last time, I dove right into SDL, OpenGL, win32API GTK+ and that kinda stuff, and while I was able to get some stuff done, I always hit rock walls... I really do appreciate the help. _______________________________________________________ well, that got the thing compiling, but its less functional than I imagined... if I type in exit it seems to bypass my return 0, and changing it to exit(0); still wont kill the program... it'll spit back numbers give it, but if I type in really long ones it gives me different numbers (probably because its not a float or double), and obviously as I foresaw, typing in signifiers (+,-,/,*) does nothing and theyarent recognized... everything other than a number results in 0, so at least its not converting them to # values at least... | |||
| helios (1515) | |||
| strcmp() returns 0 if the strings are equal. That may be it. Of course. You need to write the parser and evaluator yourself for that kind of functionality. You can also look into Bison, although that's a bit more advanced. | |||
| spiroth10 (17) | |||
| I'd rather write my own for educational purposes... but any idea on how I would go about writing one? Im getting stuck on ideas on how to implement one in my code. id appreciate if someone could help without spelling everything out for me -- the more I can do on my own, the better I'll end up understanding it in the long run. | |||
| Duoas (1595) | |||
| Google "recursive descent" and "lexical analysis". You have a good amount of learning ahead. Good luck! | |||
| helios (1515) | |||
| You can start by parsing the easy operators, like + and -, then add more, like *, /, and %. Or, you can make up your own syntax: +(1,2,3,4,-(4,1)) But it's a lot of code no matter how you look at it. | |||
| spiroth10 (17) | |||
| yeah, so I got that, but now I find that getting input is actually more difficult... I dont think a simple cin.getline can hold 3 separate values like I need it to (at least in its current state)... before I get to parsing the operators, is there any way I can get user input into an array? I mean, this is more of a UI project, and I want the user to type everything into one line, and then the computer take that line, split it into 3 values, and parse those values. I realize this is a very difficult task, but Im learning a lot from it and thats a good thing... right now Im simply trying to get something like
is this even possible? right now I feel its way above my head, and it probably is... is it worth the educational value to struggle on and push onward? I mean I wanna get this stuff down as best as possible, I wanna apply as a computer science major in college/uni next year, and I wanna really have a handle on it before they even begin teaching it in class... | |||
| helios (1515) | |||
I like to use this function to text input:
That method of parsing is okay as long as all the operators involved have he same precedence and there are no parenthesis. If that's not the case, you are just doing a preparsing, Since there's more to do than tell operators and operands apart. The order of operations matters. It's possible, yes (ignoring your logic errors). And yes, it is worth it. Parsing expressions is the first step towards parsing languages. | |||
This topic is archived - New replies not allowed.
