Compiling this on Codepad:
#include <iostream>
using namespace std;
void main (void)
{
char ch[2];
int value;
cout<<"Enter two integers between 0-9"<<endl;
cin.getline(ch,2);
//testing with char array
//(...)
//how could I do operations like '*', '+', '-', or '/' to the char arrays
}
Gives:
Line 4: error: ‘::main’ must return ‘int’
compilation terminated due to -Wfatal-errors.
For example:
Lets say ch[0]='5' and ch[1]='3'
what do I need to do so I can do ch[0] – ch[1] = 2 and store into an int value
There are a few different problems with the code you’ve posted.
mainneeds to have a return type ofint.Your call to
cin.getlinewill only fill a single character in the array you’ve declared, because the function call will null terminate the array. You needAfter that, if
array[0]contains ‘5’ andarray[1]contains ‘3’, you can simply doto get the integer result 2.
If you need to deal with numbers outside the range [0..9] you’ll need to convert them to their numeric representation. This can be done using
std::stringtreamoratoi.