Here's the script so far:
// Calculator.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
using namespace std;
float GetNumber()
{
float GetNumber=0;
printf("Please input a number:\n");
scanf_s("%c", &GetNumber);
getchar();
return(GetNumber);
}
char GetOperator()
{
char GetOperator=0;
printf("Please input an operator:\n");
scanf_s("%c", &GetOperator);
getchar();
return(GetOperator);
}
int main()
{
printf("Welcome to my Calculator!\n");
printf("Please do what is said below:\n\n");
float a = GetNumber();
printf("\n\n");
getchar();
char b = GetOperator();
printf("\n\n");
float c = GetNumber();
float result;
if(b == '+')
{
result = a + c;
}
else if(b == '-')
{
result = a - c;
}
else if(b == '*')
{
result = a * c;
}
else if(b == '/')
{
result = a / c;
}
printf("\n\nThe result of these is: ");
string sresult;
sprintf(sresult, "%f", result);
printf(sresult);
getchar();
return 0;
}
It is very, VERY basic since I only started C a few days ago, with the help of a friend to learn a few of these functions etc.
I am struggling, since I'm trying to make it so that you write in a number, then an operator, then a number. I know I've probably gone wrong somewhere else, or I've made it very easy to break, or I could make it more efficient by ..., but I'm just trying to fix this error at the moment:
1>********\visual studio 2008\projects\calculator\calculator\calculator.cpp(58) : error C2664: 'sprintf' : cannot convert parameter 1 from 'std::string' to 'char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>********\visual studio 2008\projects\calculator\calculator\calculator.cpp(59) : error C2664: 'printf' : cannot convert parameter 1 from 'std::string' to 'const char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
I didn't have any changing method before, just it trying to use printf on a float, but since it gave an error for that, I went looking about. I had a look at quite a few things. So, that's what I have at the moment.
Please can someone help me to fix these errors?
Thanks in advance,
A new C coder,
Nineza.

New Topic/Question
Reply




MultiQuote






|