Please Help me out!! After many tries I'm unable to return a string from a function. Please reply with the corrected code! I want it for my High School. Thanks in advance!
I've shortened the code here.

#include<iostream>
#include<iomanip>
#include<conio.h>
#include<cstdlib>
#include<fstream>
#include<string.h>
#include<stdio.h>
using namespace std;
string logdisplay(int log)
    {string logname[40];
    switch(log)
    {case 1:*logname="BlueDart";
            break;
    case 2:*logname="FedEx";
            break;
    case 3:*logname="Delhivery";
            break;
    case 4: *logname="DTDC/FirstFlight";
            break;
    case 5: *logname="Ecom Express";
            break;
    case 6: *logname="India Post";
            break;
    case 7: *logname="ATS";
            break;
    case 8: *logname="Ekart";
                break;
    case 9: *logname="Aramex";
            break;
    default: cerr<<"Wrong Choice!!"; exit(0);}
return *logname;
    }

    int main()
{ 
int category;
cout<<"Enter the category number corresponding to its serial number"<<endl<<"1. Baby & Mom"<<endl<<"2. Books & Magazines"<<endl<<"3. Cameras & Optics"<<endl<<"4. Automotive"<<endl<<"5. stringity"<<endl<<
    "6. Clothing & Accessories"<<endl<<"7. Coins & Notes"<<endl<<"8. Collectibles"<<endl<<"9. Fitness & Sports"<<endl<<"10. Fragrances, Beauty & Health"<<endl;
    cout<<"11. Fun Stuff"<<endl<<"12. Games & Accessories"<<endl<<"13. Home and Living"<<endl<<"14. Home & Kitchen Appliances"<<endl<<"15. Jewellery & Precious Coins"<<endl<<"16. Kitchen & Dining"<<endl<<"17. Laptops & Computer Peripherals"<<endl<<"18. LCD, LED & Televisions"<<endl<<"19. Memory Cards, Pen Drives & HDD"<<endl;
    cout<<"20. Mobile Accessories"<<endl<<"21. Mobile Phones"<<endl<<"22. Gaming Consoles"<<endl<<"23. Movies & Music"<<endl<<"24. Musical Instruments"<<endl<<"25. Services & Real Estate"<<endl<<"26. Shoes"<<endl<<"27. Stamps"<<endl<<"28. Stationery & Office Supplies"<<endl<<"29. Tablets & Accessories"<<endl;
    cout<<"30. Hardware & Electricals"<<endl<<"31. Toys, Games & School Supplies"<<endl<<"32. Travel"<<endl<<"33. Watches"<<endl<<"34. Warranty Services"<<endl<<"35. Wearable Devices"<<endl<<"36. Audio & Home Entertainment"<<endl<<"37. Everything Else"<<endl;
cin>>category;

    string categn1[40];
    categn1=categdisplay(category);


        cout<<"Category - "; cout<<categn1;


cout<<" Note: Above calculations are not 100% accurate. These are approximate figures only and may vary based on exact product weight and packaging dimensions.";
getch();
}

Recommended Answers

All 3 Replies

You seem to be using C++ ... so use C++ string ...
(and this question should then be in the C++ forum.)

Another way to get the string you need is to use an array of strings (a table) ...
See the example below:

// return_a_string.cpp//

#include <iostream>
#include <iomanip> // re. setw
#include <string>  // use C++ string in C++ //
#include <sstream> // re. stringstream objects //
#include <cctype> // re. tolower //

using namespace std;


const string DELIVERY[] =
{
    "UNKOWN", "BlueDart", "FedEx", "Delhivery", "DTDC/FirstFlight",
    "Ecom Express", "India Post", "ATS", "Ekart", "Aramex"
} ;
const int NUM_DELIVERY_TYPES = sizeof DELIVERY / sizeof *DELIVERY;

const string CHOICES[] =
{
    "", "Baby & Mom", "Books & Magazines", "Cameras & Optics", "Automotive",
    "stringity", "Clothing & Accessories", "Coins & Notes", "Collectibles",
    "Fitness & Sports", "Fragrances, Beauty & Health", "Fun Stuff", "Games & Accessories",
    "Home and Living", "Home & Kitchen Appliances", "Jewellery & Precious Coins", "Kitchen & Dining",
    "Laptops & Computer Peripherals", "LCD, LED & Televisions", "Memory Cards, Pen Drives & HDD", "Mobile Accessories",
    "Mobile Phones", "Gaming Consoles", "Movies & Music",  "Musical Instruments", "Services & Real Estate",
    "Shoes", "Stamps", "Stationery & Office Supplies", "Tablets & Accessories",
    "Hardware & Electricals", "Toys, Games & School Supplies", "Travel", "Watches", "Warranty Services", "Wearable Devices",
    "Audio & Home Entertainment", "Everything Else"
} ;
const int NUM_CHOICES = sizeof CHOICES / sizeof *CHOICES;


template< typename T >
T takeIn( const string& msg )
{
    T val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
            break;
        else
        {
            cout << "Invalid input ... numbers only!\n";
            cin.clear(); // clear error flasgs
            cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}

int takeInChr( const string& msg = "" )
{
    cout << msg << flush;
    string reply;
    getline( cin, reply );
    if( reply.size() )
        return reply[0];
    // else ...
    return 0;
}

bool more()
{
    if( tolower( takeInChr( "More (y/n) ? " )) == 'n' )
        return false;
    // else ...
    return true;
}



int showMenuGetChoice( )
{
    int category = 0;
    while( true )
    {

        for( int i = 1; i < NUM_CHOICES; ++ i )
        {
            stringstream ss;
            ss << setw(2) << i << ". " << CHOICES[i];
            cout << left << setw(37) << ss.str() << ' ';
            if( i % 2 == 0 && i != NUM_CHOICES-1 )
                cout << endl;
        }
        cout << right << endl;

        stringstream ss;
        ss << "Your choice in range 1.." << NUM_CHOICES-1 << " : ";
        category = takeIn< int >( ss.str() );
        if( 1 <= category && category <= NUM_CHOICES )
            break;
        // else
        cout << "Try again ... category " << category << " is NOT known here.\n";
        cout << "Press 'Enter' to continue ... " << flush;
        string dummy;
        getline( cin, dummy );

    }
    return category;
}


int getDelivery()
{
    int type = 0;
    while( true )
    {
        for( int i = 1; i < NUM_DELIVERY_TYPES; ++ i )
        {
            stringstream ss;
            ss << setw(2) << i << ". " << DELIVERY[i];
            cout << left << setw(37) << ss.str() << ' ';
            if( i % 2 == 0 && i != NUM_DELIVERY_TYPES-1 )
                cout << endl;
        }
        cout << endl;

        stringstream ss;
        ss << "Your choice in range 1.." << NUM_DELIVERY_TYPES-1 << " : ";
        type = takeIn< int >( ss.str() );
        if( 1 <= type && type < NUM_DELIVERY_TYPES )
            break;
        // else
        cout << "Try again ... type " << type << " is NOT knpwn here.\n";
    }
    return type;
}



int main()
{
    do
    {
        int choice = showMenuGetChoice();
        cout << "Your choice was '" << CHOICES[choice] << "'\n";

        cout << endl;
        int i = getDelivery();
        cout << "Ok, you selected '" << DELIVERY[i] << "'\n";

        cout << endl;
    }
    while( more() );
}

To get a string from a method called categdisplay, you will first and foremost need a method called categdisplay.

And probably return a new string from it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.