Reputation: 59
I want to build a program that support multi-language but if I included the language selection code into the main function it will be messy, so I make another function called language then I wrote the code below:
#include <iostream>
int language() {
std::cout << "1.English\n2.中文";
}
int main() {
std::cout << language();
}
Then my Code::Blocks IDE gave me a warning:
*warning: no return statement in function returning non-void [-Wreturn-type]|*
I still can compile the code using MinGW via command prompt, but the compiled program gave me the output below:
1.English
2.中文4683872
Then I add return 0; after the std::cout << "1.English\n2.中文"; but it display 0 instead of 4683872 above.
I am still learning C++ and I have no idea what happened to my code, and is there is any ways to remove those numbers?
Upvotes: 2
Views: 229
Reputation: 212949
If you want the user to select a language then your code needs to be more like this:
#include <iostream>
int language() {
int choice;
std::cout << "1.English\n2.中文"; // print menu of language choices
std::cin >> choice; // get selection from user
// NB: real code would have error checking here to make sure that `choice` is valid
return choice; // return selection
}
int main() {
std::cout << language();
}
Upvotes: 1
Reputation: 27567
You've got to get your return values sorted out! You want the language
function to return an output stream, since you're feeding it's output into std::out
in main
. You might as well pass in an output stream into language
so you could reuse it with a different output stream to std::out
. Also, main
has to return an int
. Standard practice is to return 0
unless there's an error.
#include <iostream>
std::ostream& language(std::ostream& os) {
os << "1.English\n2.中文";
return os;
}
int main() {
std::cout << language(std::cout) << std::endl;
return 0;
}
Upvotes: 1
Reputation: 14510
In your program, you print your string first and after you print the return value of the function... That's why you have a number after the string.
Just try :
// I don't think your language function need to return something
// So make it void
void language() {
std::cout << "1.English\n2.中文";
}
int main() {
language(); // Here no need to print the value returned by the language function
return 0; // Main return an int, 0 is for success
}
Just to explain a bit more :
The fact that your language()
function return an int
value and that you are not returning anything leads to Undefined behaviour. That's why you get a value like 4683872
. And it is also why you have a warning at the compilation.
Upvotes: 3
Reputation: 20726
The std::cout << language();
is printing an integer for you since language()
returns an int
. If you don't want anything to print after your text, then remove the std::cout <<
in your main
.
Upvotes: 0
Reputation: 409166
You have two problems in your code: The first is that you declare language
as returning an int
but then not actually returning anything. This is what the warning is about. The other problem is based on the first one, and that you actually use this "returned value" even though there is none, which leads to undefined behavior.
You can solve it in two ways: Either return a valid value from the function, or declare it as returning void
and don't use the function in an expression.
Upvotes: 2
Reputation: 1336
#include <iostream>
void language() {
std::cout << "1.English\n2.中文";
}
int main() {
language();
return 0;
}
Upvotes: 0