Function must return a value - c++

I am trying to make a text based RPG and i'm fairly new to c++. I understand that I need to return a value, but when I try and return CharacterName or CharacterRace it comes up with unresolved externals errors. I'd really appreciate the help guys, thanks :)
CharacterCreation.h
#include <string>
#include <iostream>
void petc(), ConsoleClear(), petc(), EnterClear();
std::string CharacterName, CharacterRace;
Main.cpp
#include <iostream>
#include <limits>
#include <string>
#include <string.h>
#include "CharacterCreation.h"
std::string CharacterCreation();
int main()
{
CharacterCreation();
}
std::string CharacterCreation(int RaceChoice, int RaceChoiceLoop)
{
RaceChoiceLoop = 0;
std::cout << "Welcome to the character creation V 1.0.0" << std::endl;
EnterClear();
std::cout << "Choose a name: ";
std::cin >> CharacterName;
std::cout << CharacterName << std::endl;
EnterClear();
while (RaceChoiceLoop == 0)
{
std::cout << "(1) Human - Human's race perks: + 5 to Magic | + 1 to Sword Skill" << std::endl;
std::cout << "(2) Elf - Elve's race perks: + 5 to Archery | + 1 to Magic" << std::endl;
std::cout << "(3) Dwarf - Dwarven race perks: + 5 to Strength | + 1 to Archery" << std::endl;
std::cout << "Choose a race, " << CharacterName << ": ";
std::cin >> RaceChoice;
if (RaceChoice == 1)
{
RaceChoiceLoop = 1;
CharacterRace = "Human";
}
else if (RaceChoice == 2)
{
RaceChoiceLoop = 1;
CharacterRace = "Elf";
}
else if (RaceChoice == 3)
{
RaceChoiceLoop = 1;
CharacterRace = "Dwarf";
}
else
{
std::cout << "Invalid Option";
EnterClear();
RaceChoiceLoop = 0;
}
}
}
void petc()
{
std::cout << "Press Enter To Continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
void EnterClear()
{
std::cout << "Press Enter To Continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
system("cls");
}
void ConsoleClear()
{
system("cls");
}

A declared std::string function should return a string and this is not the same as printing it on the screen, use return "something" inside the function otherwise declare it void.

The "unresolved externals" message isn't directly caused by your returning a value.
It's a linker error, and only occurs because compilation succeeded.
The cause is that you're declaring, and calling, this parameter-less function:
std::string CharacterCreation();
but you're defining this function with two parameters:
std::string CharacterCreation(int RaceChoice, int RaceChoiceLoop)
The declaration and the definition must match.
From the looks of it, you don't actually want the parameters and should use local variables instead:
std::string CharacterCreation()
{
int RaceChoice = 0;
int RaceChoiceLoop = 0;
// ...

Problem is that the function CharacterCreation() (taking no arguments) is never defined, and thus the linker cannot find it.
Try substituting in the following:
std::string CharacterCreation(int, int);
int main()
{
CharacterCreation(1,1);
}
This will call the CharacterCreation function you have implemented below the main function. Doing this I can compile (and link) your code :)

As I have pointed in my comment before, your CharacterCreation method does not return any value, although you have defined a string as an expected one.
What you most likely want to do is either change CharacterCreation signature to:
void CharacterCreation(int RaceChoice, int RaceChoiceLoop)
and keep the current implementation
or pack all your console output in a string and return it at the end of the method.
Then in main()
string result = CharacterCreation();
can retrieve this value and you can print it in main

Related

First day at C++

I need to ask for the user's Initials and have it return their input. If they input any int I need it to loop back and ask them again.
int main()
{
// Program 1-1
std::cout<<"Hello, can you please enter your initials?\n";
char initials[50];
std::cin.getline(initials, 50);
while (std::cin != int)
{
std::cout << "Thank you. " << initials << std::endl;
}
else (std::cin.fail())
{
std::cin.clear();
}
}
#include <cctype>
#include <iostream>
#include <string>
int main() {
std::string tmp;
bool bogusInput;
do {
std::cout << "Initials: ";
std::getline(std::cin, tmp);
bogusInput = false;
for (auto i : tmp) {
if (std::isdigit(i)) {
bogusInput = true;
break;
}
}
if (bogusInput) {
std::cout << "Are you Elon's son? Please enter letters only.\n";
}
} while (bogusInput);
std::cout << "Thank you, " << tmp << ".\n";
return 0;
}
This code does the job you describe. I chose a do/while loop since I always need to get initials at least one time.
The variable bogusInput is a flag that I use to know if something hasn't worked out properly. It is declared outside of the loop for the sake of the while condition. It is always reset before performing the check, this allows the case of multiple attempts to work.
Finally, "valid" input gets us out of the loop and we say thank you and end.

Double function returning as an integer

I would like to know why does my double function returns as an integer instead of a decimal. I gave a value of 0.01 to my ic4 to go into the function and expect a return of 0.384615 but instead i get a return of 1.
#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
using namespace std;
double vt = 0.026;
double ic4;
double gm7(double IC7);
int main ()
{
while(true)
{
printf("ao (in dB): ");
cin >> ao;
if (ao >= 80)
{
printf("IC7 (in Amps): ");
cin >> ic7;
cout << "IC7: " << ic7 << endl;
gm7(ic7);
cout <<"gm7: " << gm7 << endl;
}
else
{
printf("Choose another ao!\n");
}
}
}
double gm7 (double IC7)
{
return IC7 / vt;
}
gm7 is a function. You are inserting a function into the character stream.
Character streams do no have an insertion operator overloads for functions. However, they do have an overload for bool and function pointers implicitly convert to bool and function implicitly converts to a function pointer. Since the function pointer is not null, it converts to true. true is printed as 1.
P.S. The example program is ill-formed because it uses undeclared names.
just to clarify the comments
you should do this
int main ()
{
while(true)
{
printf("ao (in dB): ");
cin >> ao;
if (ao >= 80)
{
printf("IC7 (in Amps): ");
cin >> ic7;
cout << "IC7: " << ic7 << endl;
double gm7res = gm7(ic7); <======
cout <<"gm7: " << gm7res << endl; <<======
}
else
{
printf("Choose another ao!\n");
}
}
}
note you also need to declare a0

simple c++ input function

i am new to c++ from other languages, and looking at examples this code looked like it should work
#include <iostream>
using namespace std;
main()
{
string input = "";
cout << "in: ";
getline(cin, input);
input_recv(input);
}
input_recv(input)
{
if (input == "hello"){
cout << "derp" << endl;
}
}
it will not let me use the function input_recv. it gives me several errors in my IDE. one being `input_recv' undeclared (first use this function). basically what i am trying to do for this is make it respond to input using a function.
EDIT:
#include <iostream>
#include <string>
using namespace std;
void input_recv(string);
int main()
{
while (1 == 1){
string input = "";
cout << "in: ";
getline(cin, input);
input_recv(input);
cin.get();
}
}
void input_recv(string input){
if (input == "hello"){
cout << "derp" << endl;
}
}
thanks
C++ requires the function to be declared before it's used, so if you move the input_recv definition above the main function, it will work. Otherwise, you can leave the program the way it is and add a forward declaration above main like this:
void input_recv(string);
int main()
{
...
}
void input_recv(string input)
{
...
}
Edit:
There are a few other errors here as well as other comments pointed out. One, functions should have a return type and parameter types specified. Also, before using the string type, you need to
#include <string>.
Declare the function first, and use a correct function prototype, here there is not type for input, no return type ... Example below,
#include <iostream>
#include <string>
void input_recv(const std::string& input);
int main()
{
std::string input = "";
std::cout << "in: ";
std::getline(std::cin, input);
input_recv(input);
return 0;
}
void input_recv(const std::string& input)
{
if (input == "hello"){
cout << "derp" << endl;
}
}
C++ is a strongly-typed language. You must declare your variables and your functions with explicit types:
// forward declare your function
void input_recv(std::string input);
// alternatively
void input_recv_better(const std::string& input);
int main()
{
std::string input;
std::cout << "In: ";
std::getline(std::cin, input);
input_recv(input);
input_recv_better(input);
return 0;
}
void input_recv(std::string input)
{
if (input == "hello")
{
std::cout << "derp" << std::endl;
}
}
void input_recv_better(const std::string& input)
{
if (input == "hello")
{
std::cout << "derp!" << std::endl;
}
}
There's a few things definitely wrong with this snippet, I will correct them all so you can observe the difference:
#include <iostream>
using namespace std;
void input_recv(string input);
int main()
{
string input = "";
cout << "in: ";
getline(cin, input);
input_recv(input);
}
void input_recv(string input)
{
if (input == "hello"){
cout << "derp" << endl;
}
}
I have added return types to your functions, data types to your parameters, and a forward declaration of the input_recv function so that the main function knows it exists.
You will definitely want to pick up a book like C++ Primer (the latest edition revised for the C++11 standard) before learning bad practice by trying to forgo some sort of standard training.
#include <iostream>
using namespace std;
void input_recv(string input)
{
if (input == "hello"){
cout << "derp" << endl;
}
}
int main()
{
string input = "";
cout << "in: ";
getline(cin, input);
input_recv(input);
return 0;
}

How to add a time limit for user to input something or else it goes on?

Since very long time, I am trying to put a timer in C++ which actually gives a limited time to input anything like for example :-
if I type
cout<<"Enter the name :-
cin>>name;
cout<<"Enter Phonenoe :- ";
cin>>phoneno;
So in this How can I add time say for 5 secs to input name, and if user don't input anything in 5 secs, programs should go to input phonenoe.
Give the full code, I am a beginner.
Fine, I am overruled.
The best google I found for this is here. In a nutshell, this is very difficult to do in C++ and I believe impossible to do in a portable way. In assembler, I would poll the lowest level keyboard interrupt (9h I think) to see what is coming in, but that was in DOS days and I'm not sure if that works anymore.
just for fun. only works in windows.
#include <windows.h>
#include <iostream>
#include <string>
bool wait_for_key(int timeout_milliseconds, char& ch) {
HANDLE tui_handle = GetStdHandle(STD_INPUT_HANDLE);
DWORD tui_evtc = 0;
DWORD deadline = GetTickCount() + timeout_milliseconds;
INPUT_RECORD tui_inrec = { 0 };
DWORD tui_numread = 0;
while (GetTickCount() < deadline) {
if (tui_evtc > 0) {
ReadConsoleInput(tui_handle, &tui_inrec, 1, &tui_numread);
if (tui_inrec.EventType == KEY_EVENT) {
if (tui_inrec.Event.KeyEvent.bKeyDown) {
ch = tui_inrec.Event.KeyEvent.uChar.AsciiChar;
return true;
}
}
}
YieldProcessor();
GetNumberOfConsoleInputEvents(tui_handle, &tui_evtc);
}
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE tui_handle = GetStdHandle(STD_INPUT_HANDLE);
std::string name;
std::string other;
std::cout << "name: ";
char ch;
if (wait_for_key(5000, ch)) {
std::cout << ch;
std::getline(std::cin, name);
name = ch + name;
std::cout << "name is '" << name.c_str() << "'" << std::endl;
} else {
std::cout << std::endl << "other: ";
std::getline(std::cin, other);
std::cout << "other is '" << other.c_str() << "'" << std::endl;
}
return 0;
}

Can't return value from a function call to main() correctly

I am trying to test this simple function, but Opt.status, Opt.Year values are not returned back to main(). Why? Please help as I am new to C++.I am using visual c++ to execute these codes.This is in my .cpp file
#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "stdio.h"
using namespace std;
int main()
{
Easy_Task obj_EasyTask;
TimeDateMonthOptions whatOptions=DATE;
TOptions Opt;
Opt.status=FALSE;
Opt.Year=1970;
printf("Enter code\n");
scanf("%d",&obj_EasyTask.code);
cout << "the code entered is: " << obj_EasyTask.code;
obj_EasyTask.display2(obj_EasyTask.code);
cout << "\nOutput: " << obj_EasyTask.show();
printf("\nEnter the options that you prefer\n");
scanf("%d",&whatOptions);
obj_EasyTask.display3(whatOptions, Opt);
cout << "\nOpt.Year: " << Opt.Year;
if(Opt.status)
{
obj_EasyTask.x=(Opt.Year)& 0x00FF;
obj_EasyTask.y=((Opt.Year)& 0xFF00)>>8;
cout << "\nX: " << obj_EasyTask.x;
cout << "\nY: " << obj_EasyTask.y;
obj_EasyTask.Result=(obj_EasyTask.x)*(obj_EasyTask.y);
}
char holdWindow;
std::cin >> holdWindow;
return 0;
}
uint16_t Easy_Task::display2(uint16_t code)
{
if(code==1)
{
c = 7;
}
else
{
c = 9;
}
return c;
}
uint16_t Easy_Task::display3(TimeDateMonthOptions whtOptions, TOptions Opt)
{
switch(whtOptions)
{
case 0:
case 1:
case 2:
case 3:
Opt.status=TRUE;
cout << "\nStatus1: " << Opt.status;
Opt.Year=1991;
cout << "\nYear1: " << Opt.Year;
break;
case 7:
Opt.status=FALSE;
cout << "\nStatus2: " << Opt.status;
Opt.Year=2013;
cout << "\nYear2: " << Opt.Year;
break;
default:
Opt.status=FALSE;
cout << "\nStatus3: " << Opt.status;
Opt.Year=2010;
cout << "\nYear3: " << Opt.Year;
break;
}
return Opt.status, Opt.Year;
}
In my .h file I have the class defined as follows:
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
typedef unsigned short uint16_t;
#define TRUE 1;
#define FALSE 0;
typedef struct TOptions
{
bool status;
uint16_t Year;
};
typedef enum
{
YEAR,
MONTH,
DATE,
HOURS,
MINUTES,
SECONDS,
HUNDRETHS,
UNDEFINED
}TimeDateMonthOptions;
class Easy_Task
{
public:
uint16_t code, c, x,y, Result;
uint16_t display2(uint16_t code);
uint16_t show()
{
return c;
};
uint16_t display3(TimeDateMonthOptions whatOptions, TOptions Opt);
};
The problem I have is line:
if(Opt.status)
Where it doesn't return the value of 1 but instead in takes the default value which was defined earlier. Why is this happening?
You need to pass the argument by reference:
uint16_t Easy_Task::display3(TimeDateMonthOptions whtOptions, TOptions& Opt)
//^
Otherwise, a copy of Opt is made and modified in the function and the caller will never see the changes.
Note that:
return Opt.status, Opt.Year;
does not somehow return two values. This is using the comma operator and will return the value Opt.Year. However, if you pass Opt by reference a return value is unrequired.
Better still define a function to return TOptions
TOptions Easy_Task::display3(...); and return a structure.
Remember that you can only ever return ONE SINGLE return value from the function.