You are viewing our Forum Archives. To view or take place in current topics click here.
Can someone help me with my C++ code?
Posted:
Can someone help me with my C++ code?Posted:
Status: Offline
Joined: Mar 29, 201212Year Member
Posts: 248
Reputation Power: 11
Status: Offline
Joined: Mar 29, 201212Year Member
Posts: 248
Reputation Power: 11
Hey guys, i'm learning c++ and i'm trying to set a value to a char string. Here's my code:
But i'm getting errors on the parts where it's "calc = "value here";"
Here are the errors:
[ Register or Signin to view external links. ]
Thanks for the help guys, and to make you smile, here's a picture of my friend who does drugs:
[ Register or Signin to view external links. ]
Drugs are bad guys, don't do them...
#include <iostream>
using namespace std;
char calc;
int main()
{
int num1;
int num2;
cout << "Welcome to Functions! Please enter a number.\n" << endl;
cin >> num1;
cout << "The number you entered was " << num1 << endl;
cout << "Now what way of calculating would you like to do?\n+ = Add\n- = Subtract\n* = Multiply\n/ = Divide" << endl;
cin >> calc;
checkCalc();
cout << "You want to " << calc << endl;
}
void calculate()
{
}
void checkCalc()
{
if ( calc == '+') {
calc = "add";
}
else if ( calc == '-' ) {
calc = "subtract";
}
else if ( calc == '*' ) {
calc = "multiply";
}
else if ( calc == '/' ) {
calc = "divide";
}
}
But i'm getting errors on the parts where it's "calc = "value here";"
Here are the errors:
[ Register or Signin to view external links. ]
Thanks for the help guys, and to make you smile, here's a picture of my friend who does drugs:
[ Register or Signin to view external links. ]
Drugs are bad guys, don't do them...
#2. Posted:
Status: Offline
Joined: Jul 12, 201013Year Member
Posts: 3,612
Reputation Power: 173
Status: Offline
Joined: Jul 12, 201013Year Member
Posts: 3,612
Reputation Power: 173
You initialized calc as a char, so you can't set it to something with more than one letter, which is where the error is coming from. As a char, can only hold one character at a time. So, create another variable with a type of std::string and output that instead.
std::string Operation;
if ( calc == '+') {
Operation = "add";
}
else if ( calc == '-' ) {
Operation = "subtract";
}
else if ( calc == '*' ) {
Operation = "multiply";
}
else if ( calc == '/' ) {
Operation = "divide";
}
- 1useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Mar 29, 201212Year Member
Posts: 248
Reputation Power: 11
Status: Offline
Joined: Mar 29, 201212Year Member
Posts: 248
Reputation Power: 11
RDCA wrote You initialized calc as a char, so you can't set it to something with more than one letter, which is where the error is coming from. As a char, can only hold one character at a time. So, create another variable with a type of std::string and output that instead.
std::string Operation;
if ( calc == '+') {
Operation = "add";
}
else if ( calc == '-' ) {
Operation = "subtract";
}
else if ( calc == '*' ) {
Operation = "multiply";
}
else if ( calc == '/' ) {
Operation = "divide";
}
alright i just tried that but it's not recognizing it as an std::string. Is it maybe because i have "using namespace std;" ?
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Mar 29, 201212Year Member
Posts: 248
Reputation Power: 11
Status: Offline
Joined: Mar 29, 201212Year Member
Posts: 248
Reputation Power: 11
Nevermind got it to work, had sts, missed the d :b Thanks alot man
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.