Practicing C++ and here is two of the practice problems I pulled from the FAQ And practice post. Just want feedback and issues with the way I wrote these.
Simple Calculator
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;
int main(void)
{
//cout << string(50, '\n');
cout << "Welcome to Simple Calc" << endl;
cout << "Program will ask for two numbers and then a operation" << endl;
string input = "";
string input2 = "";
char input3 = '+';
int sum = 0;
while (true)
{
cout << "Numbers Only please, enter x at any time to quit." << endl;
cout << "Valid Operations are +(Addition), -(Subtraction), *(Multiplication), /(Division)" << endl;
cout << "Please enter first number:";
cin >> input;
if (input == "x") {break;}
cout << endl << "Please enter Second number:";
cin >> input2;
if (input2 == "x") {break;}
cout << endl << "Please enter Operation(default +):";
cin >> input3;
if (input3 == 'x') {break;}
while (input3 != '+' && input3 != '-' && input3 != '*' && input3 != '/')
{
cout << "Current Char is: " << input3;
cout << endl << "Please enter + - * / please:";
cin >> input3;
if (input3 == 'x') {break;}
}
if (input3 == 'x') {break;}
cout << endl << endl << "The total of "
<< input << " "
<< input3 << " "
<< input2 << " is: ";
if ( input3 == '+' ) {cout << ( atoi( input.c_str() ) + atoi( input2.c_str() ) );}
else if ( input3 == '-') {cout << ( atoi( input.c_str() ) + atoi( input2.c_str() ) );}
else if ( input3 == '*') {cout << ( atoi( input.c_str() ) * atoi( input2.c_str() ) );}
else if ( input3 == '/') {
if ( input2 == "0" ) {cout << "Sorry you cannot divide by 0" << endl;}
else {cout << ( atoi( input.c_str() ) / atoi( input2.c_str() ) );}
}
cout << endl << endl << endl;
}
return 0;
}
Palindrome Checker
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string input;
string inputReversed;
cout << "Welcome to the palindrome checker!" << endl;
while (true) {
cout << "Enter x to quit." << endl;
cout << "Please enter a word you would like to check: ";
cin >> input;
if (input == "x") {break;}
cout << "Checking " << input << " to see if its a palindrome." << endl;
for (int i = input.length() - 1; i >= 0; i--)
{
inputReversed = inputReversed + input.at(i);
}
if (input == inputReversed) {cout << inputReversed << " Is a palindrome." << endl << endl;}
else {cout << inputReversed << " Is not a palindrome." << endl << endl;}
inputReversed = "";
}
return 0;
}