C++: Undefined reference to function error [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So I have a program whose purpose is to calculate the total bill for medical expenses using overloaded functions. There is a problem though when I try to call the function which is inside the if/else statement block.
Before I compile it there's really no indicator that lets me know there's an issue and I'm stuck, I'd appreciate some help. This is the complete error message I get: In function main': main.cpp:(.text+0x14d): undefined reference to bill(float, float)'
main.cpp:(.text+0x25b): undefined reference to `bill(float, float, float)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
compiler exit status 1
here's the code:
#include <iostream>
#include <iomanip>
using namespace std;
float choice, service_charge, test_charge, medicine_charge;
float bill(float, float);
float bill(float, float, float);
int main()
{
cout << "Please input 1 if you are a member of"
<< " the dental plan" << ", Input any other number if you are not: " << endl;
cin >> choice;
if (choice == 1)
{
cout << "Please input the service charge: " << endl;
cin >> service_charge;
cout << "Please input the test charge: " << endl;
cin >> test_charge;
bill(service_charge, test_charge);
}
else
{
cout << "Please input the service charge: " << endl;
cin >> service_charge;
cout << "Please input the test charges: " << endl;
cin >> test_charge;
cout << "Please input the medicine charges: " << endl;
cin >> medicine_charge;
bill(service_charge, test_charge, medicine_charge);
}
return 0;
}
float bill(float &refservice, float &reftest)
{
cout << "The total bill is: $" << endl;
return refservice + reftest;
}
float bill(float &refservice, float &reftest, float &refmed)
{
cout << "The total bill is: $" << endl;
return refservice + reftest + refmed;
}

The signature of the prototype, float bill(float, float);, is not equivalent to the actual function definition's signature, which is float bill(float &refservice, float &reftest). Your other prototype and function have the same issue. Thus, the compiler doesn't recognize that you've already defined that function. You have to change the signature of your prototype to match. Your prototypes in that case would look like:
float bill(float&, float&);
float bill(float&, float&, float&);
One thing to note is that it's not clear why you have to pass those floats by reference, since you're not modifying them in any way.

Related

how to get more than 1 word input from cin [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am currently trying to learn C++ and I'm working on my first project.
It is supposed to ask questions and get the user to provide the input for the answer then write the input to a file with some formatting. however, I keep getting an error with the description line input, it takes the first word in a sentence and nothing else.
I've tried several things to get it to work and all of them throw a similar error or don't allow me to give any input...
cin >> description; allows me to give input of 1 word.
std::getline and getline dont allow me to give any input.
I just seem to get these errors:
Error (active) E0304 no instance of overloaded function "std::basic_istream<_Elem, _Traits>::getline [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list
or this error:
std::basic_istream<char,std::char_traits<char>>::getline': non-standard syntax; use '&' to create a pointer to member
My current code looks like this:
int main()
{
ofstream myfile;
myfile.open("Output.txt");
if (myfile.is_open())
{
cout << "Enter kit name\n";
cin >> kit;
myfile << " \"" << kit << "\":{\n";
cout << "Default Amount?\n";
cin >> defaultamount;
myfile << " \"DefaultAmount\":" << defaultamount << ",\n";
cout << "Price\n";
cin >> price;
myfile << " \"Price\":" << price << ",\n";
cout << "Enter a Description for the kit\n";
cin >> description;
myfile << " \"Description\":\"" << description << "\",\n";
myfile.close();
cout << "output.txt has been updated with your results\n";
system("pause");
}
else
cout << "Unable to create or update text file";
return 0;
}
Assuming that you need to initialize variables and include some headers, you should include something like:
#include<fstream>
using std::ofstream;
#include<iostream>
using std::cout;
using std::cin;
#include<string>
using std::string;
string kit;
int defaultamount;
float price;
string description;
if description will have several words, you must change:
cin >> description;
for:
cin.ignore();
getline(cin,description);

Semantic issue invalid operands to binary expression ('double' and 'double')

I`m trying to divide any number in .5, if dividing the number in .5 and my remain is 0 the number will raises up to the next. But if not, it will down to the next.
When I try to do that I get a issue in the line 39. Somebody can help me.
Thanks
//
// main.cpp
// Promedio
//
// Created by Oscar Espinosa on 3/27/15.
// Copyright (c) 2015 IPN ESIME Ticoman. All rights reserved.
//
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int ed, cv, qa, em, h, poo;
float prom, red;
string nom, ape;
cout << " Introduce tus datos" << endl
<<"Nombre: ";
getline(cin, nom);
cout << "Apellidos: ";
getline(cin, ape);
cout << "Introduce las siguientes calificaciones" << endl
<< "Ecuaciones Diferenciales: ";
cin >> ed ;
cout << "Calculo vectorial: ";
cin >> cv ;
cout << "Quimica apilcada: ";
cin >> qa ;
cout << "Electricidad y magnetismo: ";
cin >> em;
cout << "Humanidades II: ";
cin >> h;
cout << "Programacion orientada a objetos: ";
cin >> poo ;
prom = (ed+cv+qa+em+h+poo)/6.00;
if (prom%.5 == 0) // Semantic issue invalid operands to binary expression ('double' and 'double')
{
ceil(prom);
red = ceil(prom);
}
else
{
floor(prom);
red = floor(prom);
}
cout << nom << " " << ape << " tu promedio es " << prom << " y se redondea a " << red;
return 0;
}
#Oscar Espinosa you cannot use %(modulus) operator with double values So its showing the error invalid operands in expression.. try using the fmod(x,y) function..it will work
Modulo (%) can only be used with integer values.
You can use fmod but if you meant to work on integer values, perhaps such a trick can help:
if (10*static_cast< int >(prom)%5 == 0)
The issue is pretty clear: you don't define var before the if(var==0). In C++, you need to define the variable before its first use. Pay attention to what the compiler is telling you. In case of g++:
error: 'var' was not declared in this scope
which I think it's pretty explicit!
PS: don't modify the code after an answer, as you end up confusing everyone. In the actual code, you are passing 0.5 as the modulus operator argument. That operator takes an int. You need other way of testing whether a float is a multiple of 0.5. In particular, you should also pay attention to roundoff errors.
Hint: a float x is a multiple of 0.5 if and only if 2*x is an integer. So 2*x - floor(2*x) must be close to zero, i.e. abs(2*x - floor(2*x)) < 1e-12 to avoid floating point errors.
See related question: Checking if float is an integer

What is wrong with this C++ code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm new to programming and I've decided to try and make a Calculator that can do stuff other than simple Arithmetic. I have not finished yet, I was just testing to see if it was working so far. As I ran it, and went through Arithmetic by pressing 1 it just stops. Can someone please tell me what Ive done wrong? Thank you.
#include <iostream>
using namespace std;
int main()
{
int frsnum
int secnum
int arithchoice;
int answer;
int x;
cout << "Welcome to the advanced calculator!" << endl;
cout << "What are you trying to calculate: Simple Arithmetic < 1 >" << endl;
cout << " Systems of Equations < 2 >" << endl;
cout << " Matrices < 3 >" << endl;
cin >> x;
if(x == 1)
{
cout << "Add <1>|Subtract <2>|Multiply <3>|Divide <4>";
cin << arithchoice;
}
if(arithchoice == 1)
{
cout << "Whats the first number: "
cin >> frsnum;
cout << "And the second number: "
cin >> secnum;
answer = frsnum + secnum;
cout << "That would be: " answer << endl
}
system("PAUSE");
return 0;
}
The arrows in this statement are incorrect.
cin << arithchoice;
should be replaced by this statement
cin>> arithchoice;
Update
The best way to remember which arrows to use with Cin and Cout is that with when inputing value you are pointing from outside to the computer.
Similarly for cout you throw values from the computer to outside world.
So now if you want to pass values from real world to computer you which arrow will you use >> cin
Similarly for giving results from computer to Real world(user) "<<"
----------------
| |
Real world | <--- computer |
|_______________|
The first thing I've noticed is that in the (x==1) if block, the arrows of the cin are the wrong way round.

undefined reference to WinMain, [Error] Id returned 1 exit status

What have I screwed up here? There can be no hard coded values in the code which is why all my prompts are constants. We also have to call a user defined function to verify input.
Im getting the following error when I compile -- undefined reference to WinMain, [Error] Id returned 1 exit status I'm using Dev C++ as an IDE
#include <iostream> //for I/O
#include <iomanip> //for formatting output
using namespace std;
const string PROGRAM_DESCRIPTION = "Program will calculate the amount "
"accumulated every month you save, \nuntil you reach your goal. ";
const string ENTER_DOLLAR_AMOUNT_MONTHLY = "Enter the dollar amount to be "
"saved each month: ";
int main()
{
double dollarSavedPerMonth;
//displays program description
cout << PROGRAM_DESCRIPTION << endl << endl;
//Prompts user to enter dollar amount to be saved monthly, will validate
//input by calling VerifyDollar
dollarSavedPerMonth = VerifyDollar(ENTER_DOLLAR_AMOUNT_MONTHLY);
cout << endl;
return 0;
}
double VerifyDollar (string Prompt)
{
const string INVALID_DOLLAR_AMOUNT = "Invalid amount, re-enter monthly "
"savings amount.";
double dollarSaved;
cout << Prompt;
cin >> dollarSaved;
while (dollarSaved < 5 || dollarSaved > 5000)
{
cout << INVALID_DOLLAR_AMOUNT;
cout << endl;
cout << Prompt;
cin >> dollarSaved;
}
return dollarSaved;
}
You do indeed lack a WinMain() function anywhere in that code.
If memory serves me well, WinMain() is the entry point for a Win32 GUI app. I am assuming your IDE asked you for a "project type" of some sort, and you asked for a Windows app instead of a Console one.
Under that assumption, something in your project is configured to call WinMain(), which you did not define, hence the linker error.

Why is exit(0); giving me a std:string... error?

I'm new to C++. I decided to not watch the next tutorial and put my skills to use, by making a funny Mind Reader application. I'm pleased with myself, however, even though I've ironed out most bugs, I still have one concerning the exit function. I read the C++ documentation for it, and I'm not sure what I did wrong. I did exit(0);. I have a very weird error, which is:
no match for call to '(std::string {aka std::basic_string<char>}) (int)
I have searched online, however I am still unaware of what the problem is. My error is on line 59 (marked in the code):
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
//declaring variables to be used later
string name;
string country;
int age;
//header goes below
cout << "#######################################";
" ############ MIND READER ############"
"#######################################\n\n";
//asks if the user would like to continue and in not, terminates
cout << "Would like you to have your mind read? Enter y for yes and n for no." << endl;
cout << "If you do not choose to proceed, this program will terminate." << endl;
string exitOrNot;
//receives user's input
cin >> exitOrNot;
//deals with input if it is 'y'
if (exitOrNot == "y"){
cout << "Okay, first you will need to sync your mind with this program. You will have to answer the following questions to synchronise.\n\n";
//asks questions
cout << "Firstly, please enter your full name, with correct capitalisation:\n\n";
cin >> name;
cout << "Now please enter the country you are in at the moment:\n\n";
cin >> country;
cout << "This will be the final question; please provide your age:\n\n";
cin >> age;
//asks the user to start the sync
cout << "There is enough information to start synchronisation. Enter p to start the sync...\n\n";
string proceed;
cin >> proceed;
//checks to see if to proceed and does so
if (proceed == "p"){
//provides results of mind read
cout << "Sync complete." << endl;
cout << "Your mind has been synced and read.\n\n";
cout << "However, due to too much interference, only limited data was aquired from your mind." << endl;
cout << "Here is what was read from your mind:\n\n";
//puts variables in sentence
cout << "Your name is " << name << " and you are " << age << " years old. You are based in " << country << "." << endl << "\n\n";
cout << "Thanks for using Mind Reader, have a nice day. Enter e to exit." << endl;
//terminates the program the program
string exit;
cin >> exit;
if (exit == "e"){
exit(0); // <------------- LINE 59
}
}
}
//terminates the program if the input is 'n'
if (exitOrNot == "n"){
exit(0);
}
return 0;
}
Thanks
The local variable exit shadows other identifiers from outer scopes with the same name.
To illustrate with a smaller example:
int main()
{
int i;
{
int i;
i = 0; // assign to the "nearest" i
// the other i cannot be reached from this scope
}
}
Since the only exit visible is an object of type std::string, the compiler sees exit(0) as a call to operator()(int) and throws a hissy fit when it doesn't find one among std::string members.
You can either qualify the name (std::exit(0);) or rename the variable. And since all of your code is in main you can simply say return 0; instead.
Try using return 0; or return EXIT_SUCCESS;. It's the exact same thing. Also, you can only input one word into a cin. Instead, use getline(cin, string name); If it still doesn't work, add a cin.ignore(); before your getline(cin, string name);, like this:
//stuff
string country;
cout << "Now please enter the country you are in at the moment:\n\n";
cin.ignore();
getline(cin, country);
//stuff
return 0;
The problem is arrising because you declared a standard keyword as the name of a local variable.
Now as the local variable is of type sting it is not able to take it as its value.