Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6819789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:20:53+00:00 2026-05-26T21:20:53+00:00

I am playing around some in C++, just for fun. I have just begun

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:20:53+00:00Added an answer on May 26, 2026 at 9:20 pm
       int ans(void)  
       ^^^^^
    

    You are trying to return s which is std::string in the function body, but your function signature says function is returning an int. So, the compiler tries to convert the std::string to int which is an invalid conversion and hence the error.

    It should be:

    std::string ans(void)
    ^^^^^^^^^^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been playing around with some animations, and it just so happens that I
So, I was just playing around making some page for fun. I cannot seem
friends I was just playing around with some pointer programs and realized that the
i have some sample code i have been playing around with. Basically im just
just have a general question that came up while I was playing around with
I am just playing around with some HTML, PHP and JS and have run
I'm just playing around with some code but it seemed to have the specific
I am just playing around with some java programming and so I wanted a
I was playing around with some Javascript snippets today and noticed that this code
Just playing around with some of the APIs in .NET and I can't seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.