You don't need to store the whole input at once, though if you did it would have to be as a string. As for the rest of it, just use the stream extraction operator (std::cin >> ...) to input the first value. I don't really know why std::cin.peek is required, but you could just test for the operator after taking in the first number and using std::cin.ignore to get rid of the useless whitespace.
int a, b;
std::cout << "Please enter an expression: ";
std::cin >> a;
while (std::cin.peek() == ' ')
std::cin >> op;
std::cin >> b;
// process operator and values...
Of course, you would need to modify this to work for things like 'ln 5', but you get the basic idea. Maybe you can think of a way to make std::cin.peek necessary, too...
Maybe you can use std::cin.peek to test to see if the first character is a number, and process the equation differently otherwise.
> yes but my professor wants me to input the following code
This is what your professor is attempting to do:
1 2 3 4
a. skip over leading white-space
b. peek at the next (first non-white-space) character
if it is alphabetic => read operation, number: eg. 'ln 5'elseif it is a digit => read number, operation, second number: eg. '4 * 5'
double number, second_number ;
std::string operation ;
std::cout << "Please enter an expression (like `4 * 5` or `ln 5`): ";
// skip over leading white-space
char c ;
std::cin >> c ; // read the first non-whitespace character
std::cin.putback(c) ; // and put it back
// look at the first non-whitespace character
if( std::isdigit(c) ) // if the first char is a digit
// input is of the form 'number operator number';
// read it into number, operation, second_number
std::cin >> number >> operation >> second_number ;
std::cout << "number==" << number << " operation=='" << operation
<< "' second_number==" << second_number << '\n' ;
elseif( std::isalpha(c) ) // if the input starts with an alphabet
// input is of the form 'operator number';
// read it into operation, number
std::cin >> operation >> number ;
std::cout << "operation=='" << operation << "' number==" << number << '\n' ;
// else // error in input: first non-whitespace character is neither a digit nor a letter
// writing this line here would be better
//instead of inside main().
// for beautiful coding
//always add your declarations
//to the beginning.
// it will keep looping as long
//as x isnt equal to -1.
while(x != -1)
cout << "enter a number: \n";
cin >> x;
cout << "\n";
cout << "\n";
cout << "1 = multiplication ";
cout << "2 = division ";
cout << "3 = addition ";
cout << "4 = subtraction ";
cout << "-----------------------------------------------------------------------------------------\n";
cout << "enter your choice: \n";
cin >> o;
cout << "enter your last number: \n";
cin >> y;
//it should be == instead of =
//haha sorry i didnt realized it :D
if ((o == 1))
cout << x * y << endl;
elseif ((o == 2))
cout << x / y << endl;
elseif ((o == 3))
cout << x + y << endl;
elseif ((o == 4))
cout << x - y << endl;
cout << "you entered an invalid operator\a";
There's a few problems wrong with that jdogsis.
1) it does not meet his requirements
2)your operator is a character and not a string
3) all your if statements are wrong characters have single quotes around them(' ')
4) they can enter just an operator and 1 number example ln 5 or log 10
5) What if they want to do something like -1 * 100 (anyways you never explicitly told the user what the exit key was)