Hi I am working on some C++ code in Xcode and I get this error Control may reach end of non-void function. I am also using the Unreal engine standard for code so that is where the FText and int32 comes from. Here is my code:
do {
// get a guess from the player
int32 CurrentTry = BCGame.GetCurrentTry();
std::cout << std::endl;
std::cout << "Try " << CurrentTry << ". Enter your guess: ";
FText Guess = "";
std::getline(std::cin,Guess);
Status = BCGame.CheckGuessValidity(Guess);
switch (Status)
{
case EGuessStatus::Wrong_Length:
std::cout << "Please enter a " << BCGame.GetHiddenWordLength() << " letter word.\n";
break;
case EGuessStatus::Not_Isogram:
std::cout << "Please enter a word without repeating letters.\n";
break;
case EGuessStatus::Not_Lowercase:
std::cout << "Please enter all lowercase letters.\n";
break;
default:
return Guess;
}
} while (Status != EGuessStatus::OK);
}
Please someone help.
You are not showing your complete function, but obviously it is declared to return something; and the default path in the switch also does return Guess.
However, the other three paths in the switch do not return anything, the flow goes behind the switch and there is nothing more. So you have a function that returns nothing, but it needs to return something - just as the message said.
Related
I am attempting to debug a graph function that starts at one vertex and ends at a destination vertex upon finding it via a FS (First Search) algo. Upon calling the function during debug mode, the debugger opens stl_map.h (I assume it does this because the graph (vertices and edges) have been implemented within a map.
The parameters of the function are (map, startPerson, endPerson). I pass a map, and the two people parameters as follows (name of the function is bfsFacebook):
for (;;) {
cout << endl << "Enter the name of the starting person:\n";
getline(cin, startingPerson);
if (IS_QUIT(startingPerson))
break;
if (everyone.count(startingPerson) == 0) {
cout << "Invalid starting name.";
continue;
}
cout << endl << "Enter the name of the ending person:\n";
getline(cin, endingPerson);
if (IS_QUIT(endingPerson))
break;
if (everyone.count(endingPerson) == 0) {
cout << "Invalid ending name.";
continue;
}
//Breath-First search from starting node to end node (starting vertext to end vertex)
if (bfsFacebook(everyone, everyone[startingPerson], everyone[endingPerson]) == true) {
cout << "Path found between " << startingPerson << " and " << endingPerson << endl;
} else {
cout << "Path Not Found between " << startingPerson << " and " << endingPerson << endl;
}
}
cout << "Exiting..." << endl;
Here is the actual function:
bool bfsFacebook(map<string, Person> everyone, Person &startingPerson, Person &endingPerson) {
startingPerson.visited = true;
if (startingPerson.name == endingPerson.name && startingPerson.visited == true)
return true;
for (string somePerson : startingPerson.friends) {
if (everyone[somePerson].visited == false && bfsFacebook(everyone, everyone[somePerson], endingPerson)) {
return true;
}
}
return false;
}
Is there any particular reason why stepping into the function during debugging does not send me to the actual function?
If my question is not detailed enough, please let me know.
The call line:
bfsFacebook(everyone, everyone[startingPerson], everyone[endingPerson])
Starts by resolving the arguments before calling bfsFacebook with them.
You have two calls to map<string, Person>::operator[](const string&) which shall be done to resolve everyone[startingPerson] and everyone[endingPerson], so the next executed line in your program is in std::map
The map<Key, Value>::operator[](const Key&) may be optimized depending on the build, but in debug it's more likely to be present in the code.
As the std::map has been instantiated for <string, Person> by your own code, the debugger has no reason not to step in a function which existence is your responsibility.
I have been doing some exercises to learn c++ and decided to integrate them into R since ultimately I want to write c++ backends for R functions.
I am having trouble finding a solution to retrieve user input from the R console. While there is Rcpp::Rcout for printing and returning output, there doesn't seem to be a similar funciton for std::cin....
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::String cola() {
Rcpp::Rcout << "Pick a drink:" << std::endl << "1 - Espresso" << std::endl << "2 - Americano" << std::endl << "3 - Latte" << std::endl << "4 - Cafe dopio" <<
std::endl << "5 - Tea" << std::endl;
int drink;
std::cin >> drink;
std::string out;
switch(drink) {
case 1: out = "Here is your Espresso";
case 2: out = "Here is your Americano";
case 3: out = "Here is your Latte";
case 4: out = "Here is your Cafe dopio";
case 5: out = "Here is your Tea";
case 0: out = "Error. Choice was not valid, here is your money back.";
break;
default:
if(drink > 5) {out = "Error. Choice was not valid, here is your money back.";}
}
return out;
}
Even without Rcpp in the mix, std::cin is unsuitable for interactive input.
To use the R console with Rcpp, you need to use R functions (in particular, readline) instead of C++ functionality. Luckily you can pull R objects into your C++ code:
Environment base = Environment("package:base");
Function readline = base["readline"];
Function as_numeric = base["as.numeric"];
And then you can use them:
int drink = as<int>(as_numeric(readline("> ")));
Beware that there's another error in your code: your cases are all fall-through since you are missing the break; furthermore, there’s no reason to have a case 0, and there’s no reason at all for the if in the default case.
Oh, and finally, don’t use std::endl unless you actually need to flush the output (and you only need to do this once here, at the end); use '\n' instead.
In my c++ class, we have been tasked to keep building different aspects into this code. I am currently getting 2 errors and am stuck where I don't know what I am doing wrong. The program takes a private car or string for a name and a private integer to be input into the game checking for divisibility by 3, 5, and both 3 & 5. I am to use a get function and a put function within the class taking the input values and outputting them. I have essentially figured out the program but it will not compile and I am really unsure why. Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
using std::istream;
// declare the max size the username input can be
const int MAX = 14;
enum FIZZBUZZ { ABORT = 0, FIZZBUZZ, FIZZ, BUZZ };
class CFizzbuzz // Class definition at global scope
{
// make sure our constructor, destructor, plus member functions are
// all public and available from outside of the class.
public:
CFizzbuzz() {} // Default constructor definition
~CFizzbuzz() {} // Default destructor definition
// function members that are public
// get the user's name and their value from the console and
// store those results into the member variables.
void getFizzbuzz()
{
cout << "Please enter your name: " << endl;
cin >> m_myName;
cout << "Please enter your number for the FizzBuzz game: " << endl;
cin >> m_myNum;
}
// return the user's number type entered
int putFizzBuzz()
{
return m_myNum;
}
char* getName()
{
return m_myName;
}
// logic to check to see if the user's number is 0, fizz, buzz, or fizzbuz
int getRecord(int num)
{
if (num == 0)
{
return ABORT;
}
else if (num % 5 == 0 && num % 3 == 0) // fizzbuzz number
{
return FIZZBUZZ;
}
else if (num % 5 == 0) // buzz number
{
return BUZZ;
}
else if (num % 3 == 0) // fizz number
{
return FIZZ;
}
else
return num;
}
// private data members only available inside the class
private:
int m_myNum;
char m_myName[MAX];
};
int main()
{
CFizzbuzz myClass;
cout << "Welcome to my Fizzbuzz game, you are to guess the location of a "
<< "number which if is divisible by 5 and 3 you will win with "
<< "the output of Fizzbuzz. " << endl;
cout << "Please enter an integer value between 0 and 3 "
<< "representing the row location of the number for the game, "
<< "then press the Enter key: " << endl;
for (;;)
{
myClass.getFizzbuzz();
int num = myClass.putFizzBuzz();
switch (myClass.getRecord(num))
{
case ABORT:
cout << myClass.getName() << "\nThank you for playing\n";
system("PAUSE");
return 0; // exit program
case FIZZ:
cout << "Sorry, " << myClass.getName() << ", number is a Fizz, please try again.\n";
break;
case BUZZ:
cout << "Sorry, " << myClass.getName() << ", number is a Buzz, please try again.\n";
break;
case FIZZBUZZ:
cout << "You win you got FizzBuzz!!!" << endl;
break;
default:
cout << "Sorry, " << myClass.getName() << ", number is a not a Fizz, Buzz, or Fizzbuzz\nPlease try again.\n";
break;
}
}
}
These are errors I'm getting:
LNK2019, LNK1120
Based on the errors you mentioned in comments (Unresolved external symbol _WinMain#16) I'd say that you created a Win32 project (a GUI project) in Visual Studio but your code is meant to be a console application.
You need to change the type of your project from Win32 application to Console application by either re-recreating it or changing the Subsystem from Windows to Console in project settings. See the following link for more information on the latter:
https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx
I would be suspicious of the else return num. What happens when you enter 1 or 2? Clearly the modulus doesn't provide a fizz or buzz but based on your enum values the function getRecord() returns that it does. I would have a NONE enum value set to -1 to indicate that it is neither a fizz or buzz.
The thing to remember about an enum value is that it resolves to an actual number when compiled. So when you input 1, and the modulus doesn't prove a fizz, buzz, or a fizzbuzz AND you return 1, the switch case statement will resolve to fizzbuzz even though this is not the case (pun intended).
As far as you commenting that it's not working as expected, please input more details.
I am test out a question/answer program and relatively new to c++. I am trying to create a question/answer and so far using the while loop to check if the answer is true it will retuen the problem is if the question is wrong if the here is my code:
while(input1 == answer1)
{
cout << "Your answer is correct!\n";
answer_correct += 1;
break;
}
while(answer_correct = 0 )
{
cout << "Correct Answer is:\n" << answer1 << "\n";
break;
}
for ZETA
original code:
if (input3 == answer3)
{
cout << "Your answer is correct!";
answer_correct += 1;
}
else
{
cout << "Correct Answer is:\n" << answer3;
}
all if statements below this one are return false and display the answers below when input == to answer
Now I need to make it not show the correct answer using if statements. I am probably confused would someone educate me on a proper loop to use?
Is there any particular reason why you don't use a if-else statement?
if(input1 == answer1)
{
cout << "Your answer is correct!\n";
}
else
{
cout << "Correct Answer is:\n" << answer1 << "\n";
}
Explanation
The block/statement following the if(condition) will only be executed if and only if the condition is true. An else following an if will only be executed if and only if the preceding if failed.
See also:
CPP: Control Structures
Below is the section I am having trouble with
if(transition == *(vec.end()-1)) { //vec contains the ASCI code
vec.pop_back();
--vec_index;
result.append(vec[vec_index]);
} else if(colors.find(modified.substr(1)) == colors.end()) {
cout << result << endl;
cout << "\033[0;37m";
cerr << "**ERROR: INVALID CLOSING COLOR TAG ON LINE: "
//cerr << "**ERROR: OVERLAPPING CLOSING COLOR TAG ON LINE: "
<< nlines << endl;
exit(0);
} else {
cout << result << endl;
cout << "\033[0;37m";
cerr << "**ERROR: INVALID CLOSING COLOR TAG ON LINE: "
//cerr << "**ERROR: OVERLAPPING CLOSING COLOR TAG ON LINE: "
<< nlines << endl;
exit(0);
}
In the above code, my else-if statement does not seem to execute at all. What I am trying to do is when modified.substr(1) is not found in the map is print the first error otherwise (the color string is in the map but it's not the same one as the last one in the vector) print the "else" error. However, my else case is always printing. The reason why it's modified.substr(1) is because modified (in this case) starts with a /.
transition = colors[modified.substr(1)];
will create an entry in map<> color; if it's not existing and value initialize it. That's why your else if condition will never be true. Use map::find instead of operator [] to find the entry. Something like,
map<string,string>::const_iterator it = colors.find(modified.substr(1)];
And use it instead of transition. You can de-reference it (as it->first for key and it->second for string-value) when needed to get the underlying string.
In your code vec is empty, so
if(transition == *(vec.end()-1))
leads to undefined behavior.