I am playing around some in C++, just for fun. I have just begun to use functions, and I want to make an if-statement that goes to different functions based upon if the user is trying to multiply by 1 or not.
Here is the code:
//Test för att se om jag förstår funktioner
#include <iostream>
#include <string>
using namespace std;
//Deklarerar variblar som jag ska använda
int a,b,x;
string s;
//Skriver ut funktionen för att multiplicera
int multi(int a, int b)
{
x = a * b;
return x;
}
int ans(void)
{
using std::string;
string s = "Lol";
return s;
}
//Samlar in värde från användaren, skickar den till funtktionen "multi" som multiplicerar den, sedan skickar den tillbaks den till main via return. Main visar sedan
//resultatet för användaren
int main( void )
{
using std::cout;
using std::cin;
using std::string;
cout << "Ange ett nummber som du vill multiplicera: \n\n";
cin >> a;
cout << "\n";
cout << "Ange det andra nu: \n";
cin >> b;
cout << "\n";
if(a == 1)
{
multi(a,b);
cout << "Svaret är: " << x << "\n";
}
else
{
ans;
cout << s;
}
return 0;
}
The only thing that happens is that it returns the message;
adrian@adrian-HP-ProBook-4525s:~/Documents/code$ g++ test10.cpp -o test test10.cpp: In function ‘int ans()’: test10.cpp:26:10: error: cannot convert ‘std::string {aka std::basic_string}’ to ‘int’ in return
It is so weird because from all I can see I am not trying to convert it back, am I?
//Regards