String Comparison in C++ with IF Statements? - c++

I'm very new to C++, just started learning using an online course about 30 minutes ago. I'm a little confused as to why this string comparison isn't working in a basic math script:
#include <iostream>
#include <string>
using namespace std;
int main() {
int one, two, answer;
char *oper;
cout << "Add two numbers\n\nEnter your first number" << endl;
cin >> one;
cout << "Choose an operator: + - * / %%" << endl;
cin >> oper;
cout << "Enter your second number" << endl;
cin >> two;
if (oper == "+") {
answer = one + two;
}
else if (oper == "-") {
answer = one - two;
}
else if (oper == "*") {
answer = one * two;
}
else if (oper == "/") {
answer = one / two;
}
else if (oper == "%%") {
answer = one % two;
}
cout << one << " " << oper << " " << two << " = " << answer << endl;
return 0;
}
The values for one, oper, and two are 1, "+", and 1 respectively, but in the end, 1 + 1 = 4201435 is printed out. None of the if/else if statements are being executed. What's causing this?

You're comparing char * using operator==. Either let oper be a std::string instead
std::string oper
To use the string comparison listed here: http://en.cppreference.com/w/cpp/string/basic_string/operator_cmp
or if you need to use a char * for some restriction, use strcmp:
if (!strcmp(oper, "+")) {
// ...
You also need to have your operand variable point at some buffer too, for the stream to read into. This is a little bit more complicated and I just recommend changing the type of oper to std::string.
The problem with the code you have is that it's comparing pointers to char arrays. What you get from your input methods is going to be a new string from the input stream and will never have the same address as the readonly strings in your program.
So since none of the condition is true, ans hasn't been assigned. So output it accounts for an undefined behavior.

Related

C++ Recursion function

Good afternoon everyone, I am new to recursion and am trying to create a program to take user input to build a math function. It works when doing simple things such as 3 + 4, but when trying something like (3 + 4) + 6 the main prints an empty string. The purpose of the program is to place nested numbers inside parenthesis to make them clear to read. I have tried following the code, but the recursion seems to be what i don't understand. Thanks for your time and help.
Code
#include <iostream>
#include <string>
using namespace std;
string buildEq(bool nested, string tab);
int main()
{
cout << "Welcome to the equation builder!" << endl << endl;
cout << "Each step can only have one operation between two numbers." << endl;
cout << "So the equation (3 + 4) + 6 would have one 'nested' operation." << endl << endl;
string equation = buildEq(false, "");
cout << endl << "The equation you have built is... " << equation << endl;
cout << endl << "Thanks for coming!" << endl;
return 0;
}
string buildEq(bool nested, string tab)
{
string equation;
string nesting;
cout << tab << "For this step, is there nesting? (y/n): ";
cin >> nesting;
if(nesting == "y")
{
nested = true;
tab += "\t";
buildEq(true, tab);
}
else
{
int number = 0;
int operation_count = 1;
while(number < 2)
{
if(nested == true)
{
equation += "(";
}
string num= "";
cout << tab << "What number would you like to enter: ";
cin >> num;
equation += num+= " ";
number++;
while(operation_count == 1)
{
string operation;
cout << tab << "What operation would you like to perform? (+, -, /, *): ";
cin >> operation;
equation += operation += " ";
operation_count++;
}
if(nested == true && number == 2)
{
equation += ")";
}
}
}
return equation;
}
Correct output
Welcome to the equation builder!
Each step can only have one operation between two numbers.
So the equation (3 + 4) + 6 would have one 'nested' operation.
For this step, is there nesting? (y/n): n
What number would you like to enter: 3
What operation would you like to perform? (+, -, /, *): +
What number would you like to enter: 4
The equation you have built is... 3 + 4
Thanks for coming!
Press <RETURN> to close this window...
Function results empty
Welcome to the equation builder!
Each step can only have one operation between two numbers.
So the equation (3 + 4) + 6 would have one 'nested' operation.
For this step, is there nesting? (y/n): y
For this step, is there nesting? (y/n): n
What number would you like to enter: 3
What operation would you like to perform? (+, -, /, *): +
What number would you like to enter: 4
The equation you have built is...
Thanks for coming!
Press <RETURN> to close this window...
(Probably an overkill, but I don't think that the question itself is an easy example for understanding recursions)
In order to understand the recursion part, we need to look at the general problem, and understand how we progress from one invocation to another (the recursive step), and what is our stopping point (the base case) . Your goal here is to create a valid equation, in order to do so, your input should follow certain guidelines. Specifically, in order to verify such a problem, you need to verify that each input is following a syntax which is called Context Free Grammar, denoted by the following rules (N stands for number or nested, O for operation, D for digit and $ for nothing):
N -> ( N ) O | D O
D -> 0-9
O -> + N | - N | * N | / N | $
There are two recursions here. In each stage we need to get a valid equation, and those rules make sure it stays like that.
The following code is creating a proper equation from the user.
Notice a few important notes -
I'm using std::stringstream, which is more efficient at creating strings and appending to the existing "string".
You should not over-use std::endl, since in addition to adding a line break, it also flushes to the stdout, which is expensive.
Using "Using namespace std;" isn't a good habit!
Look at how I pass the same stringstream, and each stage adds to this, in order to create the general string. If your code doesn't add to the "carried" value, it means that you are doing nothing in this recursive step.
The code:
#include <sstream>
#include <iostream>
#include <string>
#include <cctype>
#include <assert.h>
void get_num_or_nested(std::stringstream& eq);
void get_operation_or_stop(std::stringstream& eq);
bool is_number(const std::string& s)
{
int digit_count = 0;
for (const char& character : s)
{
if (std::isdigit(character))
{
++digit_count;
}
}
return !s.empty() && s.size() == digit_count;
}
bool is_operation(char c)
{
return (c == '+' || c == '-' || c == '*' || c == '/');
}
std::string get_input_from_user()
{
std::string input;
std::cin >> input;
return input;
}
void get_operation_or_stop(std::stringstream& eq)
{
std::cout << "Insert one of the following:\n";
std::cout << "An operation - [ + | - | * | / ]\n";
std::cout << "s for Stop" << std::endl;
std::string input = get_input_from_user();
if (input.size() == 1)
{
if (is_operation(input[0]))
{
eq << input;
get_num_or_nested(eq);
}
else if (input != "s")
{
assert(false);
}
// stops!
}
else
{
assert(false);
}
}
void get_num_or_nested(std::stringstream& eq)
{
std::cout << "Insert one of the following:\n";
std::cout << "A number\n";
std::cout << "n for Nested" << std::endl;
std::string input = get_input_from_user();
if (input == "n")
{
eq << "(";
get_num_or_nested(eq);
eq << ")";
get_operation_or_stop(eq);
}
else if (is_number(input))
{
eq << input;
get_operation_or_stop(eq);
}
else
{
assert(false);
}
}
int main()
{
std::cout << "Welcome to the equation builder!\n" << std::endl;
std::stringstream eq;
get_num_or_nested(eq);
std::cout << "The equation you have built is... " << eq.str() << std::endl;
std::cout << "Thanks for coming!" << std::endl;
}
The only thing that is wrong is when the user says yes to nesting. Instead of calling the function and discarding what it returned, you need to return what the function returned.
if(nesting == "y")
{
nested = true;
tab += "\t";
return buildEq(true, tab);
}

Math Calculator without 2 operands in C++

The value starts from 0 and can then be calculated using any operand of Mathematics. The code compiles successfully but doesn't work. The terminal windows shows 'Abort', 'Retry' and 'Cancel'. The rule is that you cannot use 2 operands but just keep adding the previous number to present operand.
#include <iostream>
#include <cmath>
using namespace std;
void Input(double input);
int main()
{
double sign, input;
cout << "This program calculates any given number upto 1 decimal place using the following operators:" << endl;
cout << " '+' - Addition." << endl;
cout << " '-' - Subtraction" << endl;
cout << " '*' - Multiplication." << endl;
cout << " '^' - Root." << endl;
cout << " '/' - Division." << endl;
Input(input);
return 0;
}
void Input(double IN)
{
char q;
char sign;
int Val = 0.0;
cin >> sign >> IN;
while (IN != q)
{
if (sign = '-')
Val -= IN;
if (sign = '+')
Val += IN;
if (sign = '*')
Val *= IN;
if (sign = '/')
Val /= IN;
cout << endl << "Result so far is " << IN;
IN++;
}
}
Your main problem is
q is undefined so the while(IN != q) will become undefined behavior
in C++, for any primitive datatype = means assignment operator, and not a comparator operator. To compare something use == instead.
Val is a variable with int datatype but assigned with value 0.0 which is a float or double.
What your program do in the if statement is : (for example this if statement)
if (sign = '-')
The program assign the value of - which is 45 to the sign variable
The if statement check the variable sign for value 0
if the value is 0 then the statement considered false and the block is skipped
if the value is other than 0 then the statement considered true and enter the block
The program run the code inside the if block
The program do all of those 3 thing for every if statement in your code, and I rarely run my own code in Windows so I can't very sure why the program giving the Run-Time Check Failure #3-T error
A little advice, use switch whenever you need to use multiple if statement, since it is easier to read.

c++ error c2015: too many characters in constant

Sorry if it's something simple, but I'm new to C++ and haven't really gotten a good hold on it, yet. I need to build a calculator whose only named variables are pointers, and this is what I have so far, but I keep getting errors and I can't figure out why. Every error that always related to my if construct, though.
int main()
{
//Creating variables
//Values to perform operations on
float *aptr = new(nothrow)float;
float *bptr = new(nothrow)float;
float *ansptr = new(nothrow)float;
int *endptr = new(nothrow)int;
char *operationptr = new(nothrow)char;
cout << "Simple Operation Calculator" << endl; //Displays program name
cout << "Performs +, -, *, or / on any two real operands." << endl; //Describes nature of program to user
*endptr = 1;
while(*endptr = 1) //Creates a loop so that the user may perform as many operations as desired
{
//Prompts the user for the first operand
cout << "First operand: " << endl;
cin >> *aptr;
//Prompts user for operator
cout << "Operator(+,-,*,/): " << endl;
cin >> *operationptr;
//Prompts user for second operand
cout << "Second operand: " << endl;
cin >> *bptr;
//Performs requested operation
if(*operationptr == '+' || *operationptr == 'plus')
{
*ansptr = *aptr + *bptr;
}
else if(*operationptr == '-' || *operationptr == 'minus')
{
*ansptr = *aptr - *bptr;
}
else if(*operationptr == '*' || *operationptr == 'times')
{
*ansptr = *aptr * *bptr;
}
else if(*operationptr == '/' || *operationptr == 'divided by')
{
if(*bptr = 0)
{
cout << "Cannot divide by zero. Terminating program." << endl;
*endptr = 2;
break;
}
*ansptr = *aptr / *bptr;
}
else
{
cout << "Invalid operand input. Terminating program." << endl;
*endptr = 2;
break;
}
//Displays results
cout << *aptr << *operationptr << *bptr << " = " << *ansptr << endl;
//Asks user if they wish to perform another operation. If so, they stay in loop. If not, then break from loop.
cout << "Do you wish to perform another operation? (1 = yes, 2 = no)" << endl;
cin >> *endptr;
//If 1, loop repeats. If 2, program ends.
if (*endptr == 2)
{
cout << "Thank you for using my program. Goodbye!" << endl;
}
} //end while loop
return 0;
}//end main function
There are character literals (with ') and string literals (with "). Character literals have one character. String literals are arrays of characters. You can't write something like 'plus' because it has more than one character (well technically you can, but it's a multi-character literal, but lets not go there).
Nonetheless, this wouldn't make any sense because operationptr points at a single char object. A single char can't contain the entire word plus.
If you want to be able to accept plus as input, then I suggest you start using strings. In fact, use std::string.
As a side note, you are using pointers and dynamic allocation far too often. You are also forgetting to delete the objects that you create with new - this is a memory leak. I imagine you have come from a language that uses new for all object creation. In C++, this is not necessary (and is not a good practice). Instead, you can declare objects like so:
float aptr;
There is no need to dereference this object. You can just use aptr directly as a float.
'plus'
is a character constant, and can't contain more than one character.
'+' is fine, since it's a single character in a constant.
As per the comment on this answer,
'plus' could be ok, if the compiler is not expecting a char.

How I can manage to keep white space in my String from user input

I'm coding a little console application in C++ and in it I take a string from the user:
cin >> themainstring;
int si = 0;
while (themainstring[si] != '+' || themainstring[si] != '-' ||
themainstring[si] != '*') {
if (themainstring[si] == '+' || themainstring[si] == '-' ||
themainstring[si] == '*') {
lmnopt = themainstring[si];
break; // while
}
si++;
}
int strlenthestring = themainstring.size();
lmnop1 = themainstring.substr(0, si);
lmnop2 = themainstring.substr(si + 1, strlenthestring);
So for example, when I give this input:
ilove+programming
I want to try and cut the string when I see +, - and *. which works fine.
However I want my code to do the same when I input:
ilove + programming (white spaces after and before arithmetical operator)
I have messed around with the WS but I couldn't understand the logic.
Actually the main problem of mine is about C++'s space logic. Why it thinks the space will explode the string input?
I'm not sure I've understood this question completely correctly, however I thought I'd pitch in with some help.
First off, when it comes to looking through strings, C++ has a great set of functions as standard that does that. Point your browser to: Basic String Library.
This contains all the functions you can carry out on a string in C++.
Secondly, something else you need to be aware of, is that you are using std::cin to read user input from the keyboard. By default, cin ignores white spaces, so for example, the following code:
string inputString;
cin >> intputString;
cout << "Input String is: " << inputString << endl;
and let's assume you entered Hello World in as your user input, the program would only output "Hello"
So what you need to do is to use getline. Which allows for whitespaces in your user inputs. And you use it as follows:
std::getline(cin, inputString);
So to give an example where it all gels together:
#include <iostream>
#include <sstream> // for istringstream
#include <string>
using namespace std;
int main(int argc, char * argv[])
{
string inputString;
cout << "Please Enter String: ";
getline(cin, inputString);
cout << "\n" << endl;
cout << "InputString is: " << inputString << endl;
// So you can do something like this
string searchTerm("+");
// Find first of is an operating you can carry out
// on a string so you don't have to use loops.
cout << "Position: " << inputString.find_first_of(searchTerm) << endl;
int pos = inputString.find_first_of(searchTerm);
string part1 = inputString.substr(0, pos);
string part2 = inputString.substr(pos + 1, inputString.length());
cout << "Position of + is " << pos << endl;
cout << "part 1 is: " << part1 << endl;
cout << "part 2 is: " << part2 << endl;
}
Now I know I've only done this with the + sign, but it should serve as a starting point to getting to where you want to be.
Hope all this helps.

Creating 2 arrays of 2 structs.

I have been given a problem domain. To write a program which asks the user which fruits he wants to purchase and in how much quantity. Based on the input from the user, it produces a bill.
Example interaction:
Do you want to purchase fruit? Yes
Which fruit? Orange
How many pounds ? 2
Do you want to purchase another fruit? Yes
Which fruit? Apple
How many pounds? 1.5
Do you want to purchase anothe fruit? No
Thanks for your business.
Here is your bill:
Fruit Quantity Price per Lb Price
Orange 2 Lb 1.5 3
Apple 1.5 Lb 2 3
Total Amount 6
#include <iostream>
#include <string>
using namespace std;
struct fruitData
{
string fruitName;
float price;
};
struct invoiceData
{
char fruit_ordered;
float price1;
float quantity_ordered;
float totalFruitprice;
} ;
int main()
char choice;
string fruit_choice;
float pounds_choice;
int count=0;
fruitData fruits[5];
invoiceData customers[5];
strcpy(fruits[0].fruitName, "Banana");
strcpy(fruits[1].fruitName, "Apple");
strcpy(fruits[2].fruitName, "Pears");
strcpy(fruits[3].fruitName, "Oranges");
strcpy(fruits[4].fruitName, "Papaya");
fruits[0].price=1;
fruits[1].price=2;
fruits[2].price=2.5;
fruits[3].price=1.5;
fruits[5].price=1.40;
strcpy(customers[0].fruitName, "Banana");
strcpy(customers[1].fruitName, "Apple");
strcpy(customers[2].fruitName, "Pears");
strcpy(customers[3].fruitName, "Oranges");
strcpy(customers[4].fruitName, "Papaya");
customers[0].price=1;
customers[1].price=2;
customers[2].price=2.5;
customers[3].price=1.5;
customers[4].price=1.40;
cout << "Welcome to the fruit market" << endl;
cout << "Would you like to purchase fruit?" << endl;
cin >> choice >> endl;
while (choice == 'Y')
{
cout << "Which fruit?" << endl;
cin >> fruit_choice >> endl;
cout << "How many pounds?" << endl;
cin >> pounds_choice >> endl;
if (fruit_choice == "Banana")
{
customers[0].quantity_ordered = pounds_choice;
customers[0].total_price = customers[0].quantity_ordered * customers[0].price;
}
else if (fruit_choice == "Apple")
{
customers[1].quantity_ordered = pounds_choice;
customers[1].total_price = customers[1].quantity_ordered * customers[1].price;
}
else if (fruit_choice == "Pears")
{
customers[2].quantity_ordered = pounds_choice;
customers[2].total_price = customers[2].quantity_ordered * customers[2].price;
}
else if (fruit_choice == "Oranges")
{
customers[3].quantity_ordered = pounds_choice;
customers[3].total_price = customers[3].quantity_ordered * customers[3].price;
}
else (fruit_choice == "Papaya")
{
customers[4].quantity_ordered = pounds_choice;
customers[4].total_price = customers[4].quantity_ordered * customers[4].price;
}
}
for (count = 1 ; count <=5 ; count++)
{
if (customers[0].total_price != 0)
{
cout << customers[0].fruit_name <<
cout << customers[0].quantity_ordered <<
cout << customers[0].price <<
cout << customers[0].total_price <<
}
}
}
I am getting a lot of errors. One of them being the string in "string fruit_chioce" is not turning blue as it should. And the compiler says char choice should be preceded by a ";" . I am also having trouble associating an array for these structs. Any help would be appreciated , thank you.
The best way is to use std::vector. For example
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Fruit {
string name;
float price;
};
int main() {
vector<Fruit> fruits;
while(true) {
Fruit fruit;
string answer;
cout << "Which fruit? ";
cin >> fruit.name;
cout << "How many pounds? ";
cin >> fruit.price;
fruits.push_back(fruit);
cout << "Do you want to purchase another fruit? ";
cin >> answer;
cout << endl;
if (answer=="No") break;
}
for(int i=0; i<(int)fruits.size(); ++i) {
cout << "Fruit " << i+1 << " -> Name: " << fruits[i].name << ", price: " << fruits[i].price << endl;
}
return 0;
}
After int main() there should be a { immediately following.
fruits is only 5 elements long, but you try to access the sixth here: (I guess it really should be 4 instead of 5)
fruits[5].price=1.40;
fruitName is a C++ string, you can't use strcpy with it (strcpy is for C strings, that is char*-based strings). Simply assign:
fruits[0].fruitName = "Banana";
Your while loop will never end, because you never ask again for input to choice.
Your last else here
else (fruit_choice == "Papaya")
should also be a else if.
Your for loop goes from zero to five (sixth element), but the array only has 5 elements, so it should be:
for (count = 1 ; count < 5 ; count++)
The last output lines should end with an operand (some variable to output) instead of <<. Also like every other statement they have to end with ;. For example with line break:
cout << customers[count].fruit_name << endl;
I also replaced 0 with count, because you surely don't want to always output the first array element.
In the strcuture fruitData
struct fruitData
{
string fruitName;
float price;
};
data member fruitName is defined as having type std::string. You may not use standard C function strcpy with objects of type std::string because they are not character arrays. You could use a simple assignment. For example
fruits[0].fruitName = "Banana";
But it would be better to initialize the array when it was defined. For example
fruitData fruits[5] =
{
{ "Banana", 1 }, { "Apple", 2 }, { "Pears", 2.5 },
{ "Oranges", 1.5 }, { "Papaya", 1.40 }
};
Also in structure invoiceData you defined data member fruit_ordered as
char fruit_ordered;
(a very strange decision) that is it can contain only one character. Moreover I think you meant fruitName instead of fruit_ordered because below you wrote
strcpy(customers[0].fruitName, "Banana");
but the structure does not declare this name. So again you may not use standard C function strcpy as you did because fruitName is not a character array.
Moreover you did not include header <cstring> where C functions are declared.
This statement (and similar statements)
cin >> choice >> endl;
is invalid. You may not use std::endl with std::cin
Also this loop
for (count = 1 ; count <=5 ; count++)
{
if (customers[0].total_price != 0)
{
cout << customers[0].fruit_name <<
cout << customers[0].quantity_ordered <<
cout << customers[0].price <<
cout << customers[0].total_price <<
}
}
has no any sense.
I think there are other errors but it is enogh what i already showed. Take into account that valid range of indexes for an array having 5 elements is 0 - 4
In this code
fruits[0].price=1;
fruits[1].price=2;
fruits[2].price=2.5;
fruits[3].price=1.5;
fruits[5].price=1.40;
you have a typo. Here the indexes are 0, 1, 2, 3, 5 but must be 0, 1, 2, 3, 4.
Shortly speaking the code is full of errors.