another C++ input question -- char to int???

closed account (2EwbqMoL)
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main(){
	char input[128];
	int num;

	cout << "Welcome to Calculator, please input math problems or type exit to quit\n";
	do {
		cin.getline(input,128);
		if (input == 'exit'){
			return 0;
		}
		else { 
			num = atoi(input);
			cout << num;
		}
	} while (input != 'exit');
	return 0;
}


the error messages I get in VC++ are

1
2
3
4
5
6
7
8
9
10
11
1
1>Compiling...
1>main.cpp
1>c:\users\josh\documents\visual studio 2008\projects\calculator\calculator\main.cpp(11) : error C2446: '==' : no conversion from 'int' to 'char *'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\josh\documents\visual studio 2008\projects\calculator\calculator\main.cpp(11) : error C2040: '==' : 'char [128]' differs in levels of indirection from 'int'
1>c:\users\josh\documents\visual studio 2008\projects\calculator\calculator\main.cpp(18) : error C2446: '!=' : no conversion from 'int' to 'char *'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\josh\documents\visual studio 2008\projects\calculator\calculator\main.cpp(18) : error C2040: '!=' : 'char [128]' differs in levels of indirection from 'int'
1>Build log was saved at "file://c:\Users\Josh\Documents\Visual Studio 2008\Projects\Calculator\Calculator\Debug\BuildLog.htm"
1>Calculator - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I really appreciate any help you guys can give me...
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.
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!
Or Helios could just spell it out for you LOL :) s'all good!
closed account (2EwbqMoL)
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...
Last edited on
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.
Last edited on
closed account (2EwbqMoL)
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.
Google "recursive descent" and "lexical analysis". You have a good amount of learning ahead.

Good luck!
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.
closed account (2EwbqMoL)
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
1
2
3
4
5
char input[128]
/*
then to get input on each element of the array -- instead of just getting one char value I want it to have the same effect as me assigning it
*/
char input[128] = {"16", '+', "32"} //in the code or something else along those lines then parse it so its in a recognisable form 



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...
Last edited on
I like to use this function to text input:
1
2
3
4
5
6
7
8
9
char *inputstr(long max=1024){
	char *txt=new char[max+1];
	char b;
	long a;
	for (a=0;a<max && (b=std::cin.get())!=10;a++)
		txt[a]=b;
	txt[a]=0;
	return txt;
}


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.
Topic archived. No new replies allowed.