Hey i'm going to keep this short and brief, so basically I have started my attempt to lea c++ using Microsoft Visual Studio 2015 Community. As I come from a background of java, I thought that i'd have an easier time leaing the language than I would having not had this background. Anyways, I have noticed a couple of things strange about c++. For instance, the data type string can not be directly compared or equated to another string variable or anything of the sorts, and apparently the only way to compare the likeness of two strings is to use chararrays (or so i've been told). So anyways, I have been experimenting with some basic c++ programs that i've crafted up and I keep ruing into an error. The error is here: (Note: I am a complete noob, please don't hate me)
#include <iostream>
#include <Windows.h>
using namespace std;
int sum(int a, int b, int s)
{
int c = 0;
switch (s)
{
case 1: c = (a + b);
break;
case 2: c = (a - b);
break;
case 3: c = (a * b);
break;
case 4: c = (a / b);
}
retu c;
}
int main()
{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
int w;
double a, b;
cout << "What operation would you like to perform? " << endl;
cin >> w;
cout << "Your selected operation to perform is: " << std::string(checkOperation(w)) << endl; //What? :(
cout << "What numbers would you like to use for this calculation? " << endl;
cin >> a;
cout << "First number: " << a << endl;
cin >> b;
cout << "Second number: " << b << endl;
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY);
cout << "Your answer is: " << sum(a, b, w);
system("pause");
retu 0;
}
string checkOperation(int i)
{
string s;
switch (i)
{
case 1: s = "Add";
break;
case 2: s = "Subtract";
break;
case 3: s = "Multiply";
break;
case 4: s = "Divide";
}
retu s;
}
So I wasn't very clear, but the error is on line 28. I made a function that retus a string that is dependent on a given integer. I can't think of any reason why this isn't working, so i'm tuing to the knowledgeable and friendly people on this website for advice.
Quick note before I wrap this up, if there is a simpler or easier way to make a basic calculator like this where you input two numbers and an operation (preferably in string form, but integer form is more feasible, please inform me) also any pointers on what I should expect on my jouey to complete c++ dominance would be greatly appreciated! Thank you for your time in reading and I really am glad that I can ask for help. Sorry for my lack of knowledge and general etiquette, Thank you!!
برچسب:
نویسنده: استخدام کار