I'm currently stuck on having to write a simulator for a real, old calculator (like those one dollar calculators, the ones with the buttons, in which you must enter your numbers one push-of-a-button at a time, can't write the Whole number directly and for now it is only in integers). I know for sure that there has to be a switch case for the 0-9 and one for the operators (including '='). Should break the cycle and print the result of the calculation, but I can use as many operators as I want in the each "session" (ex: 123+7-89/6*8+3). I am finding trouble in connecting the two loops and in printing the result. Thank you so much in advance.
#include <iostream>
using namespace std;
int execute(int n1, int n2, char c);
int main()
{
int acc, num=0;
char c, op;
cin>>c;
while (1) {
switch (c) {
case '0': cout << "0";
case '1': cout << "1";
case '2': cout << "2";
case '3': cout << "3";
case '4': cout << "4";
case '5': cout << "5";
case '6': cout << "6";
case '7': cout << "7";
case '8': cout << "8";
case '9': cout << "9";
num=num*10+static_cast<int>(c-'0');
acc=num;
op=c;
acc= execute(acc, num, op);
}
}
return 0;
}
int execute(int n1, int n2, char c) {
do {
switch (c) {
case '+': return (n1+n2);
case '-': return (n1-n2);
case '*': return (n1*n2);
case '/': return (n1/n2);
case '=': return true;
}
} while (!'=')
}
Related
Code for a blackjack card counting program.
the issue is that it does not exit the while loop upon receiving no cin input from the user.
for example)
User would input x chars and then hit enter to exit the while loop.
#include<iostream>
using namespace std;
int main(){
int count = 0;
char currcard;
cout<<"Enter cards seen on table: "<<endl;
while (cin>>currcard)
{
switch (currcard)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
count++;
break;
case '7':
case '8':
case '9':
break;
case 'A':
case 'J':
case 'Q':
case 'K':
count--;
break;
default:
cout<<"Invalid Entry";
break;
}
}
cout <<"Current count: "<< count << endl;
//user enter cards seen on table and if below 7 increment
//based on count the program returns if you should hit or quit
return 0;
}
Expecting program to exit when enter is hit by user
You can use cin.get() like this.
while (cin>>currcard)
{
// your logic
if (cin.get() == '\n') {
break;
}
}
In this way, your input is supposed to be something like 1 2 3 4 A J Q ending with Enter.
EDIT
As OP wants undetermined length of input, I suggest to switch the input itself from char to std::string.
This way access is gained to more intuitive and effective I\O operations:
#include <iostream> // std::cin, cout, endl
#include <string> // std::string: can omit this line
#include <cctype> // isspace(): can omit
int main(){
int count = 0;
std::string currcard{""};
std::cout << "Enter cards seen on table: "<< std::endl;
std::getline(std::cin, currcard);
for (char c : currcard) {
if (isspace(c))
continue;
switch (c) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
++count;
break;
case '7':
case '8':
case '9':
break;
case 'A':
case 'J':
case 'Q':
case 'K':
--count;
break;
default:
//std::cout << "Invalid Entry\n";
break;
}
}
std::cout <<"Current count: "<< count << std::endl;
//user enter cards seen on table and if below 7 increment
//based on count the program returns if you should hit or quit
return 0;
}
Notice I have added a check for white spaces, and removed the message for invalid entries: both simply get ignored. But if needed that line can be uncommented.
Old solution
You can use cin.getline() as suggested in the comments, in conjunction with a flag that triggers exit from the loop once three inputs are given:
#include <iostream>
int main(){
static int count = 0, flag = 0;
char currcard;
std::cout << "Enter cards seen on table: "<< std::endl;
while(std::cin.getline(&currcard, 3)){
switch (currcard)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
++count;
break;
case '7':
case '8':
case '9':
break;
case 'A':
case 'J':
case 'Q':
case 'K':
--count;
break;
default:
std::cout << "Invalid Entry\n";
--flag;
break;
}
++flag;
if (flag == 3)
break;
}
std::cout <<"Current count: "<< count << std::endl;
//user enter cards seen on table and if below 7 increment
//based on count the program returns if you should hit or quit
return 0;
}
There is also a flag decrement for invalid entries.
I'm trying to create a function that converts input words to numeric digits like a mobile numpad. It is for an assignment. I cannot use cstring or character arrays.
Can someone please identify and correct the error in my code? It currently gives the error: ISO C++ forbids comparison between pointer and integer [-fpermissive].
I am not using any pointer variables. I do have used the strlen() function to determine the exact place of a character in a string. Any help is greatly appreciated.
#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdio.h>
using namespace std;
void Letter_correspondence();
int main()
{
Letter_correspondence();
return 0;
}
void Letter_correspondence()
{
cout<<"Enter the letters of the word you want to convert to numbers: ";
char a[]="Hello";
char b[]="world";
int len=strlen(a);
int lenb=strlen(b);
int n;
int l=a[n];
for (n=0;n<=7;n++)
{
while (n<=len)
{
if (l=="a"||l=="b"||l=="c")
{
if (n==2)
{
cout<<"-";
}
cout<<"2";
}
else if (l=="d"||l=="e"||l=="f")
{
if (n==2)
{
cout<<"-";
}
cout<<"3";
}
else if (l=="g"||l=="h"||l=="i")
{
if (n==2)
{
cout<<"-";
}
cout<<"4";
}
else if (l=="j"||l=="k"||l=="l")
{
if (n==2)
{
cout<<"-";
}
cout<<"5";
}
else if (l=="m"||l=="n"||l=="o")
{
if (n==2)
{
cout<<"-";
}
cout<<"6";
}
else if (l=="p"||l=="q"||l=="r"||l=="s")
{
if (n==2)
{
cout<<"-";
}
cout<<"7";
}
else if (l=="t"||l=="u"||l=="v")
{
if (n==2)
{
cout<<"-";
}
cout<<"8";
}
else if (l=="w"||l=="x"||l=="y"||l=="z")
{
if (n==2)
{
cout<<"-";
}
cout<<"9";
}
}
}
}
If I understood this right, you're trying to map characters to different ones and print them. You said you cannot use cstring or character arrays, but you do that here:
char a[]="Hello";
char b[]="world";
Instead, I would just use std::string:
std::string a = "Hello";
You can then iterate through it using a range-based for loop. The best way to print your string would probably be using a switchstatement:
for (char &c : a)
{
switch (tolower(c)) {
case 'a':
case 'b':
case 'c':
std::cout << 2;
break;
case 'd':
case 'e':
case 'f':
std::cout << 3;
break;
case 'g':
case 'h':
case 'i':
std::cout << 4;
break;
case 'j':
case 'k':
case 'l':
std::cout << 5;
break;
case 'm':
case 'n':
case 'o':
std::cout << 6;
break;
case 'p':
case 'q':
case 'r':
case 's':
std::cout << 7;
break;
case 't':
case 'u':
case 'v':
std::cout << 8;
break;
case 'w':
case 'x':
case 'y':
case 'z':
std::cout << 9;
break;
default:
std::cout << c;
}
}
If you're using an old version of C++ that doesn't support range based for loops, change this
for (char &c : a)
{
switch (tolower(c)) {
To this
for (size_t i = 0; i < a.size(); i++)
{
switch (tolower(a[i])) {
And std::cout << c; to std::cout << a[i];.
The ConvertHexToBinary function returns the proper binary string....followed by a bunch extra 0 & 1s
Cant figure out why the extra is being tacked on.
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
string ConvertHexToBinary(string str) {
string ToReturn;
for (int i = 2; i < str.size(); i++) {
switch (str[i]) {
case '0':
ToReturn.append("0000");
case '1':
ToReturn.append("0001");
case '2':
ToReturn.append("0010");
case '3':
ToReturn.append("0011");
case '4':
ToReturn.append("0100");
case '5':
ToReturn.append("0101");
case '6':
ToReturn.append("0110");
case '7':
ToReturn.append("0111");
case '8':
ToReturn.append("1000");
case '9':
ToReturn.append("1001");
case 'A':
ToReturn.append("1010");
case 'B':
ToReturn.append("1011");
case 'C':
ToReturn.append("1100");
case 'D':
ToReturn.append("1101");
case 'E':
ToReturn.append("1110");
case 'F':
ToReturn.append("1111");
}
}
return ToReturn;
}
int main()
{
int x = 25;
int *p = &x;
cout << p << endl;
std::ostringstream str;
str << p;
std::cout << str.str() << endl;
cout << ConvertHexToBinary(str.str()) << endl;
}
I'm new to c++ and I have to write a program that takes a user 4-digit number and convert it to words i.e. 7238 would be wrote as seven two three eight. Yet it writes every number as unknown. Any advice for a noob would be greatly appreciated.
#include iostream
using namespace std;
int main() {
char number;
cout << "Please enter a 4 digit number: ";
cin >> number;
switch(number){
case 1 :
cout<< "one";
break;
case 2 :
cout<< "two";
break;
case 3 :
cout<< "three";
break;
case 4 :
cout<< "four";
break;
case 5 :
cout<< "five";
break;
case 6 :
cout<< "six";
break;
case 7 :
cout<< "seven";
break;
case 8 :
cout<< "eight";
break;
case 9 :
cout<< "nine";
break;
case 0 :
cout<< "zero";
break;
default :
cout << "UNKNOWN.";
}
}
Sounds like homework but here are some tips. Change your number variable to type of int You can break the number out into individual variables with division and modulus. I would stuff those into an integer array.
int array[4];
arr[0] = (number / 1000) % 10; // Thousands
....... // You do the hundreds and tens
arr[3] = (number % 10); // Ones
Then use a loop around your switch statement where your counter is less than 4 (the length of the array). Make sure to increase your counter at the end of each loop. Oh, and it's #include <iostream>.
With to_string and range based for:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number;
cout << "Enter the number: ";
cin >> number;
string strnum = to_string(number);
for (auto c : strnum)
{
switch (c)
{
case '0': cout << "zero "; break;
case '1': cout << "one "; break;
case '2': cout << "two "; break;
case '3': cout << "three "; break;
case '4': cout << "four "; break;
case '5': cout << "five "; break;
case '6': cout << "six "; break;
case '7': cout << "seven "; break;
case '8': cout << "eight "; break;
case '9': cout << "nine "; break;
default: cout << "non-digit"; break;
}
}
return 0;
}
You need to put ascii values in your case statements. Currently you are comparing the ascii values for digits with numbers 0 - 9.
Values can be found here : http://www.asciitable.com/
Your variable is of type char. A char stores a character, usually ASCII encoded. If the user inputs a '1', for example, that would usually translate to an integer value of 49, not 1. Either read into an int or change your case labels to use character literals:
case '1':
cout << "one";
break;
You could then use a loop to read multiple digits.
First up, yes, this is homework that I'm struggling with, so help would be appreciated. We're making a calculator in C++ that is supposed to function differently on the + and - operators.
With '+' it is supposed to to add the two numbers together (i.e., 45 + 54 = 4554).
With '-' it is supposed to remove the first digit of the first element from the second element (i.e., 1217 - 1 = 27) We're supposed to do this by overloading the + and - operators, which I seem to be struggling with. Thanks in advance for the help!
class WhackyRPN
{
public:
int value;
int operator+ (WhackyRPN a[]);
int operator- (WhackyRPN s[]);
int getValue();
void setValue(int);
};
void WhackyRPN::setValue(int val){
value = val;
}
int WhackyRPN::getValue(){
return value;
}
int WhackyRPN::operator+ (WhackyRPN a[]){
string combinedNum = to_string(a[1].getValue()) + to_string(a[0].getValue());
int finalNum = stoi(combinedNum);
return finalNum;
}
int WhackyRPN::operator- (WhackyRPN s[]){
int minusNum;
string firstNum = to_string(s[0].getValue());
string secondNum = to_string(s[1].getValue());
string minusString = to_string(minusNum);
for (int i = 0; i < firstNum.length(); i++){
if (firstNum.at(0) != secondNum.at(i)){
minusString.at(i) += secondNum.at(i);
}
}
minusNum = stoi(minusString);
return minusNum;
}
int main()
{
WhackyRPN stackPos[4];
string indent = " ";
string userInput;
stackPos[0].setValue(0);
stackPos[1].setValue(0);
stackPos[2].setValue(0);
stackPos[3].setValue(0);
while (1){
system("cls");
cout << "---STACK---" << endl;
cout << indent << stackPos[3].getValue() << endl;
cout << indent << stackPos[2].getValue() << endl;
cout << indent << stackPos[1].getValue() << endl;
cout << indent << stackPos[0].getValue() << endl;
cout << "CMD: ";
cin >> userInput;
if (userInput == "exit" || userInput == "Exit" || userInput == "EXIT"){
exit(0);
}
switch (userInput[0]){
case 'q':
case 'Q':
exit(0);
case 'p':
case 'P':
stackPos[0] = stackPos[1];
stackPos[1] = stackPos[2];
stackPos[2] = stackPos[3];
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
stackPos[3].setValue(stackPos[2].getValue());
stackPos[2].setValue(stackPos[1].getValue());
stackPos[1].setValue(stackPos[0].getValue());
stackPos[0].setValue(stoi(userInput));
break;
case '+': //combine pos[1] and pos[0];
int finalNum = stackPos[1] + stackPos[0];
stackPos[3].setValue(stackPos[2].getValue());
stackPos[2].setValue(stackPos[1].getValue());
stackPos[1].setValue(stackPos[0].getValue());
stackPos[0].setValue(finalNum);
break;
case '-': //remove pos[0].firstNum from pos[1]
int minusNum = stackPos[0] - stackPos[1];
stackPos[3].setValue(stackPos[2].getValue());
stackPos[2].setValue(stackPos[1].getValue());
stackPos[1].setValue(stackPos[0].getValue());
stackPos[0].setValue(minusNum);
break;
case '/': //divide pos[1] by pos[0]
if (stackPos[0].getValue() == 0){
cout << "Cannot divide by 0" << endl;
system("pause");
break;
}
int endQuotient = stackPos[1].getValue() / stackPos[0].getValue();
stackPos[3].setValue(stackPos[2].getValue());
stackPos[2].setValue(stackPos[1].getValue());
stackPos[1].setValue(stackPos[0].getValue());
stackPos[0].setValue(endQuotient);
break;
case '*': //multiply pos[1] by pos[0]
int endProduct = stackPos[1].getValue() * stackPos[0].getValue();
stackPos[3].setValue(stackPos[2].getValue());
stackPos[2].setValue(stackPos[1].getValue());
stackPos[1].setValue(stackPos[0].getValue());
stackPos[0].setValue(endProduct);
break;
default:
break;
}
}
system("pause");
return 0;
}
You get the error that you do because there really is no overload of operator+ which stackPos[1] + stackPos[0] could resolve to. The only overload you have is of type WhackyRPN::operator+(WhackyRPN*);(it is a pointer even though you have written an array - read here and here. But that isn't relevant to this question.) The signature should be WhackyRPN::operator+(WhackyRPN). More idiomatic would be WhackyRPN::operator+(const WhackyRPN&). For more, read this great answer.
replace
int finalNum = stackPos[1] + stackPos[0];
with
int finalNum = stackPos[1].getValue() + stackPos[0].getValue();
In your program, you have the array of objects stackPos[], which has functions setValue() and getValue() which take and return integer respectively. You need to use getValue() here as the array elements themselves are not integer, they are objects.
This is exactly what your error statement is saying as well. But you seem to already know that because you have implemented it in * and / operations.
Hope this helps.