-1

I know the function has to return an integer, but in it, I print letter by letter in capital, and it also prints out the 0 that I am forced to return. I tried referencing void capital(string& _name) but this just gave me tons of errors. I think I need to return something inside the for, but I have no more ideas. What can I do?

int capital(string& _name){

    locale loc;

    for(std::string::size_type i = 0; i < _name.length(); i++){
        cout << std::toupper(_name[i], loc);
    }

    return 0;
}

int main(){

    string name = "robbie";
    cout << capital(name) << endl;

    system("pause");
    return 0;
}
sampjr
  • 103
  • 1
  • 2
  • 14
  • what error do you get when you make its return `void`? – chris Jan 28 '15 at 02:37
  • 3
    Please get rid of [system("pause")](http://www.gidnetwork.com/b-61.html). It is gratuitously non-portable and dangerous. (What if I tried this program on my machine, and on my machine, the `pause` command pauses the cooling system on my home's nuclear reactor?) – David Schwartz Jan 28 '15 at 02:39
  • @chris it was accusing me of using the wrong operator, cause I was using void instead of returning an int. – sampjr Jan 28 '15 at 02:44
  • @DavidSchwartz, yes. Many apologies. – sampjr Jan 28 '15 at 02:44
  • @DavidSchwartz Agreed ! But why would you _knowingly_ issue a `pause` on this system in the first place :D ? – P0W Jan 28 '15 at 02:44
  • @P0W, well I copied and pasted for time restrictions, I must've forgot. – sampjr Jan 28 '15 at 02:50
  • @P0W In this case, anyone even casually looking at the code can see the `system("pause");` and know it won't work on a system where `pause` doesn't do what you expect. The problem arises if someone doesn't look at the code, if it's in the middle of a lot of code, or if the behavior is somewhat obscured because the code isn't all in one place. – David Schwartz Jan 28 '15 at 02:57

3 Answers3

1

Change this:

    cout << capital(name) << endl;

to this:

    capital(name);
    cout << endl;
ruakh
  • 175,680
  • 26
  • 273
  • 307
1

Make it return a string instead. Note that I don't output the string inside capital, just construct a new capitalized one and return it, but otherwise the logic is very similar to the original version.

#include <string>
#include <iostream>

std::string capital(const std::string& _name){

    std::locale loc;
    std::string name_copy = _name;

    for(std::string::size_type i = 0; i < name_copy.length(); i++){
        name_copy[i] = std::toupper(name_copy[i], loc);
    }

    return name_copy;
}

int main(){

    std::string name = "robbie";
    std::cout << capital(name) << std::endl;

    return 0;
}
Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
  • Thank you! This totally worked, I don't know how I didn't think of returning a string. – sampjr Jan 28 '15 at 02:46
  • now, i get an error saying "not all control paths return a value" did I do something wrong? it just returns the first letter capitalized. – sampjr Jan 28 '15 at 02:55
  • I'm unable to get the error you mention. I've updated the code so that it will compile as is. – Michael Anderson Jan 28 '15 at 03:42
0

You can definitely change the function's return type to void. Your issue is how you're calling the function in the main method.

Your capital() method is already doing the cout, so in your main(), change:

 cout << capital(name) << endl;

to:

capital(name) << endl;
domdomcodecode
  • 2,355
  • 4
  • 19
  • 27