• Advertisement
Sign in to follow this  

C++ Problem

This topic is 3715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey, i'm just starting to use C++ and i'm trying to get this code to work, it compiles but for some reason the second function doesn't work, heres the code:
 #include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string input;
string output;

int b;

int options()
{
    int b;
    
     cout << "1. DFO\n";
     cout << "2. WoW\n";
     cout << "3. WC3\n\n";
     cin >> b;
     
     return(b);
}

string func(int a)
{      
     if(a == 1)
     {
         string output = "DFO";
     } else if ( a == 2) {
            
            output = "WoW";
     } else if (a == 3)  {
            
            output = "WC3";
     }
     return (output);
}

int main()
{
    int choice;
    string out;

    choice = options();
    cout << func(choice);
    cin >> out;
}
    


any ideas?

Share this post


Link to post
Share on other sites
Advertisement
The only part of the second function that won't work is when a == 1. Declare output at the start of the function instead of in the "if" statement.

Share this post


Link to post
Share on other sites
thanks :) it works now. and that was my first time using functions :D i think i'm gettin better :)

thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement